#!/bin/bash

# Wrapper around run-emulator.sh that sets up a reasonable development
# environment.

# Allocates more physical memory, adds a 2 gigabyte ext3 image (hdb.img)
# mounted on /home to provide persistent writeable space, and sets up and
# distcc acceleration (if both the the cross compiler and distccd
# are available in the host's $PATH).

# The following environment variables affect the behavior of this script:

# HDA - Image file to use for -hda on /usr/hda (spliced into / with cp -s)
# HDB - Image file to use for -hdb on /home (creates a new hdb.img if blank)
# HDBMEGS - Size (in decimal megabytes) when creating hdb.img
# HDC - Image file to use for -hdc on /mnt (none of blank)
# QEMU_MEMORY - number of megabytes of memory for qemu (defaults to 256)

INCLUDE unique-port.sh
INCLUDE make-hdb.sh

source ./run-emulator.sh --norun || exit 1

test -z $QEMU_MEMORY && setvar QEMU_MEMORY = '256'
setvar QEMU_EXTRA = ""-m $QEMU_MEMORY $QEMU_EXTRA""

test -z $HDA && setvar HDA = 'toolchain.sqf'

# Should we set up an ext3 image as a second virtual hard drive for /home?

if test $HDBMEGS != "0"
{
  test -z $HDB && setvar HDB = 'hdb.img'
  if test ! -e $HDB
  {

    # If we don't already have an hdb image, should we set up a sparse file and
    # format it ext3?

    test -z $HDBMEGS && setvar HDBMEGS = '2048'

    make_hdb
  }
}

# Setup distcc

# If the cross compiler isn't in the $PATH, look for it in the current
# directory, the parent directory, and the user's home directory.

setvar DISTCC_PATH = "$(which $ARCH-cc 2>/dev/null | sed 's@\(.*\)/.*@\1@')"

if test -z $DISTCC_PATH
{
  for i in {"$(pwd)/","$(pwd)/../","$HOME"/}{,simple-}cross-compiler-"$ARCH"/bin
  {
    test -f "$i/$ARCH-cc" && setvar DISTCC_PATH = "$i" && break
  }
}

test -z $(which distccd) && test -e ../host/distccd &&
  setvar PATH = ""$PATH:$(pwd)/../host""

if test -z $(which distccd)
{
  echo 'No distccd in $PATH, acceleration disabled.'
} elif test -z $DISTCC_PATH
{
  echo "No $ARCH-cc in "'$PATH'", acceleration disabled."
} else {

  # Populate a directory full of symlinks to the cross compiler using the
  # unprefixed names distccd will try to use.

  mkdir -p "distcc_links" &&
  for i in $(cd "$DISTCC_PATH"; ls $ARCH-* | sed "s/^$ARCH-//" )
  {
    ln -sf "$DISTCC_PATH/$ARCH-$i" "distcc_links/$i"
  }
  if test -e "$DISTCC_PATH/$ARCH-rawgcc"
  {
    for i in cc gcc g++ c++
    {
      ln -sf "$DISTCC_PATH/$ARCH-rawgcc" distcc_links/$i
    }
  }

  # Run the distcc daemon on the host system with $PATH restricted to the
  # cross compiler binaries.

  # Note that we tell it --no-detach and background it ourselves so jobs -p can
  # find it later to kill it after the emulator exits.

  setvar PORT = $(unique_port)
  if test -z $CPUS
  {
    # Current parallelism limits include:
    #   - memory available to emulator (most targets max at 256 megs, which
    #     gives about 80 megs/instance).
    #   - speed of preprocessor (tcc -E would be faster than gcc -E)
    #   - speed of virtual network (switch to virtual gigabit cards).
    #
    # CPUS=$(($(echo /sys/devices/system/cpu/cpu[0-9]* | wc -w)*2))
    setvar CPUS = '3'
  }"
  PATH="$(pwd)/distcc_links"" $(which distccd) --no-detach --daemon \
    --listen 127.0.0.1 -a 127.0.0.1 -p $PORT --jobs $CPUS \
    --log-stderr --verbose 2>distccd.log &

  setvar DISTCC_PID = "$(jobs -p)"
  # Clean up afterwards: Kill child processes we started (I.E. distccd).
  trap "kill $DISTCC_PID" EXIT

  # When background processes die, they should do so silently.
  disown $DISTCC_PID

  # Let the QEMU launch know we're using distcc.

  echo "distccd pid $DISTCC_PID port $PORT"
  setvar KERNEL_EXTRA = ""DISTCC_HOSTS=10.0.2.2:$PORT/$CPUS $KERNEL_EXTRA""
}

test -z $CPUS && setvar CPUS = '1'
setvar KERNEL_EXTRA = ""CPUS=$CPUS $KERNEL_EXTRA""

# Kill our child processes on exit.

trap "pkill -P$$" EXIT

# The actual emulator invocation command gets appended here by system-image.sh

test ! -z $HDC && setvar QEMU_EXTRA = ""-hdc $HDC $QEMU_EXTRA""
test ! -z $HDB && setvar QEMU_EXTRA = ""-hdb $HDB $QEMU_EXTRA""
test ! -z $HDA && setvar QEMU_EXTRA = ""-hda $HDA $QEMU_EXTRA""

run_emulator