#!/bin/sh
# 
# UFS-910 Barry Allen bainit by gutemine
#
BAVERSION=4.0.1_GOst_BootMenu
#

TEST=false

if [ -f /config/.badirectory ]; then
   BAROOTM=`cat /config/.badirectory`
else 
   BAROOTM="/tmp/usb/BA (1)"  
fi

BAROOT="/data/baroot"
BAHOME="$BAROOT/ba"

if [ -f /config/.badevice ]; then
   USB_DEVICE=`cat /config/.badevice` 
else
   USB_DEVICE=/dev/sda1  
fi

#HDROOT="/tmp/usb/Harddisk (1)"
#if [ -f /config/.hddirectory ]; then
#   HDROOT=`cat /config/.hddirectory`
#fi
#HD_DEVICE=/dev/dummy
#if [ -f /config/.hddevice ]; then
#   HD_DEVICE=`cat /config/.hddevice` 
#fi

if [ $# -eq 0 ]; then
   if [ ! -f "/.bainfo" ]; then
      swapoff -a > /dev/null 2>&1
      # we are in Flash and try to mount USB device to boot 
      if [ ! -f "$BAHOME/ba.sh" ]; then
         mount -t usbfs none /proc/bus/usb/ 
         sleep 5
         mkdir /tmp/usb > /dev/null 2>&1
         #mkdir /tmp/usb/0 > /dev/null 2>&1
         mkdir "$BAROOT" > /dev/null 2>&1
         if [ `tune2fs -l /dev/sda1 | grep BA | wc -l` -gt 0 ]; then
           USB_DEVICE="/dev/sda1"
           echo "$USB_DEVICE" > /config/.badevice
           echo "/tmp/usb/BA (1)" > /config/.badirectory
           TEST=true
         else  
           if [ `tune2fs -l /dev/sda | grep BA | wc -l` -gt 0 ]; then
             USB_DEVICE="/dev/sda"
             echo "$USB_DEVICE" > /config/.badevice
             echo "/tmp/usb/BA (1)" > /config/.badirectory
             TEST=true
           else
             if [ `tune2fs -l /dev/sdb1 | grep BA | wc -l` -gt 0 ]; then
               USB_DEVICE="/dev/sdb1"
               echo "$USB_DEVICE"
               echo "$USB_DEVICE" > /config/.badevice
               echo "/tmp/usb/BA (2)" > /config/.badirectory
               TEST=true
             else
               if [ `tune2fs -l /dev/sdb | grep BA | wc -l` -gt 0 ]; then
                 USB_DEVICE="/dev/sdb"
                 echo "$USB_DEVICE"
                 echo "$USB_DEVICE" > /config/.badevice
                 echo "/tmp/usb/BA (2)" > /config/.badirectory
                 TEST=true   
               fi
             fi    
           fi
         fi
         
         if [ $TEST = "false" ]; then
           if [ `df | grep "BA" | wc -l` -gt 0 ]; then
             USB_DEVICE=`df | grep /tmp/usb/BA | cut -d " " -f 1`
             USB_ROOT=`df | grep $USB_DEVICE | cut -d % -f 2- | cut -c 2-`
             echo "$USB_DEVICE" > /config/.badevice
             echo "$USB_ROOT" > /config/.badirectory
           fi
         fi    

     
         mount $USB_DEVICE "$BAROOT"
         #if [ -f $HD_DEVICE ]; then
         #   mkdir "$HDROOT" > /dev/null 2>&1
         #  mount $HD_DEVICE "$HDROOT"
         #fi
      fi
      if [ ! -f "$BAHOME/ba.sh" ]; then
         umount "$BAROOT"
         echo "Barry Allen not (yet) installed on USB device, or not available"
         echo "Continue booting Flash ..."
         exit 0
      fi
   fi
   
   if [ ! -f "/.bainfo" ]; then
      ln -s "$BAHOME" /tmp/ba
      /tmp/ba/barryallen.sh
      sync
      if [ ! -f "$BAHOME/.baboot" ]; then
         TARGET="Flash"
      else
         TARGET=`cat "$BAHOME/.baboot"`
      fi
      if [ -d "$BAHOME/ba/$TARGET" ]; then
         echo "Booting now Image $TARGET from $BAHOME/ba/$TARGET"
         if [ `mount | grep "$TARGET/proc" | wc -l` -lt 1 ]; then
            mount -o bind /proc "$BAHOME/ba/$TARGET/proc"
         fi
         if [ `mount | grep "$TARGET/dev" | wc -l` -lt 1 ]; then
            mount -o bind /dev  "$BAHOME/ba/$TARGET/dev"
         fi
         #if [ `mount | grep "$TARGET/dev_org" | wc -l` -lt 1 ]; then
         #   mount -o bind /dev_org  "$BAHOME/ba/$TARGET/dev_org"
         #fi
         if [ `mount | grep "$TARGET/var" | wc -l` -lt 1 ]; then
            mount -o bind /var  "$BAHOME/ba/$TARGET/var"
         fi
         if [ `mount | grep "$TARGET/tmp" | wc -l` -lt 1 ]; then
            mount -o bind /tmp  "$BAHOME/ba/$TARGET/tmp"
         fi
         if [ `mount | grep "$TARGET/ramdisk" | wc -l` -lt 1 ]; then
            mount -o bind /ramdisk  "$BAHOME/ba/$TARGET/ramdisk"
         fi
         #if [ `mount | grep "$TARGET/$BAROOT" | wc -l` -lt 1 ]; then
         #   mount -o bind "$BAROOT" "$BAHOME/ba/$TARGET/$BAROOT"
         #   echo "mount bind"
         #fi
         #if [ -f $HD_DEVICE ]; then
         #   if [ `mount | grep "$TARGET/$HDROOT" | wc -l` -lt 1 ]; then
         #      mount -o bind "$HDROOT" "$BAHOME/ba/$TARGET/$HDROOT"
         #   fi
         #fi
         # make sure that latest bainit and device is used from image when started
         cp /config/bainit "$BAHOME/ba/$TARGET/etc/init.d/bainit"
         cp /config/bainit "$BAHOME/ba/$TARGET/config/bainit"
         cp /config/.badirectory "$BAHOME/ba/$TARGET/config/.badirectory"
         cp /config/.badevice "$BAHOME/ba/$TARGET/config/.badevice"
         #cp /config/.hddirectory "$BAHOME/ba/$TARGET/config/.hddirectory"
         # now start image in chroot environment with bainit
         exec chroot "$BAHOME/ba/$TARGET" /etc/init.d/bainit
         exit 0
      else
         umount "$BAROOT"
         #BAROOT="/tmp/usb/BA (1)"
         #if [ -f /config/.badirectory ]; then
         #   BAROOT=`cat /config/.badirectory`
         #fi
         #mount $USB_DEVICE "$BAROOT"
         echo "Target Image directory $BAHOME/ba/$TARGET not found, continue booting Flash"
         exit 0
      fi
   else
      # here comes the startup for the chroot environment
      if [ -f /etc/init.d/rcS ]; then
         /etc/init.d/rcS > /dev/null 2>&1
      fi
      if [ -f /etc/init.d/rc ]; then
         /etc/init.d/rc S > /dev/null 2>&1
      fi
      /data/checkhdmount.sh > /tmp/checkhdmount.log &
      # restart gui 
      if [ -f /config/start.sh ]; then
         /config/start.sh
      else
         /app/start.sh
      fi
      sync
      sleep 3
      
   fi
else
   # emulate init by trying to start runlevel that was passed to script
   # Funktionen fr Stickimage
      echo "$1"
      case $1 in
      10)
        echo "bainit: unmounting BA device ..."
        umount "$BAROOTM"
      ;;
      11)
        echo "bainit: mounting BA deivce ..."
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM"
        #if [ -f `mount $USB_DEVICE "$BAROOTM"` ]; then
        #  echo "OK --> mount as $BAROOTM"
        #else
        #  echo "NOTOK"
        #fi  
      ;;
      12)
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM" > /dev/null 2>&1
        echo "bainit: starting FTPD"
        "$BAROOTM/ba/tools/startproftpd.sh" 
      ;;
      default)
        if [ -f /etc/init.d/rc ]; then
          exec /etc/init.d/rc $1
        else
         $ECHO " busybox init doesn't support run levels, sorry !"
        $CR
       fi
      ;;
      esac
      exit 0 
fi
exit 0
