#!/bin/sh

trident=0     # replace 0 with the trident serial number

export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=:/lib:/usr/lib:/usr/local/qt-3.3.2/lib:/usr/local/kde:/usr/local/kde/lib
export GC_INITIAL_HEAP_SIZE=33554432  # 32MB
export GC_MAXIMUM_HEAP_SIZE=100663296 # 96MB

# ---------------------------------------------------------------------------
exitWithErrorMsg()
{
    echo
    echo $1
    exit 1
}

# ---------------------------------------------------------------------------
selectPartition()
{
  d_apollo=`cat /home/taurus/.defaultApollo`
  a_apollo=`cat /home/taurus/.activeApollo`

  if [ -f "/home/taurus/.runNonDefault" ]; then
    APOLLO="/home/$a_apollo"
    rm /home/taurus/.runNonDefault
    testCode="y"
  else
    if [ $a_apollo != $d_apollo ]; then
        # ----------------------------------------------------------------------------
        # If we're running the DEFAULT version and it is not also the ACTIVE one, then
        # we must've rebooted before committing the new load ... revert back to the
        # previous versions of the arm and power manager loads.
        # ----------------------------------------------------------------------------
        echo "$d_apollo" > /home/taurus/.activeApollo
        echo "Invalid state: normal run mode, but DEFAULT is not the ACTIVE partition!"
        echo "Reverting entire system to DEFAULT partitions ..."
        curl -0 -v -d "Reboot" 1.0.0.2/pwm/reboot > /dev/null 2>&1
        exit 1
    fi
  fi
}

# ---------------------------------------------------------------------------
# Check for the existence of two validly named partitions
# ---------------------------------------------------------------------------
if [ -f /home/taurus/.activeApollo -a -f /home/taurus/.defaultApollo ]; then
  a_apollo=`cat /home/taurus/.activeApollo`
  d_apollo=`cat /home/taurus/.defaultApollo`
  if [ "$a_apollo" != "taurus_A" -a "$a_apollo" != "taurus_B" ]; then
    exitWithErrorMsg "Bad system state - unable to determine ACTIVE parition"
  fi
  if [ "$d_apollo" != "taurus_A" -a "$d_apollo" != "taurus_B" ]; then
    exitWithErrorMsg "Bad system state - unable to determine DEFAULT parition"
  fi
else
  exitWithErrorMsg "Incomplete installation - can't find partition info"
fi

# ---------------------------------------------------------------------------
# So far so good: set up logging to the right place and determine which
# partition to run
# ---------------------------------------------------------------------------
testCode=""
selectPartition

# ---------------------------------------------------------------------------
# Handle case of running the non-default load
# ---------------------------------------------------------------------------
if [ "$testCode" = "y" ] ; then
  cp ppcFirmwareInfo.txt $APOLLO > /dev/null 2>&1
  cd "$APOLLO"
fi

echo 1 > /proc/sys/net/ipv4/ip_forward
ifconfig nmx0 netmask 255.0.0.0
octet3=`expr ${trident} / 256`
octet4=`expr ${trident} % 256`
trident_ip="1.13.${octet3}.${octet4}"
curl -v -0 --data-binary "" http://1.0.0.2/status/power?nmxbus=on > /dev/null 2>&1
/sbin/redir --lport=8089 --cport=80 --caddr=${trident_ip} &
/sbin/redir --lport=8889 --cport=8888 --caddr=${trident_ip} --timeout=60 &

chmod 777 apollo
./apollo trident=${trident} > /dev/null
exit $?

