#!/bin/sh
# 
# UFS-910 Barry Allen bainit by gutemine
#
BAVERSION=4.4_GOst
#

TEST=false

if [ -f /config/.badirectory ]; then
   BAROOTM=`cat /config/.badirectory`
else 
   BAROOTM="/tmp/usb/XBA (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
         echo "$BAVERSION"
         for i in 1 2 3 4 5 ; do      
         mount -t usbfs none /proc/bus/usb/ 
         sleep 2
         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/XBA (1)" > /config/.badirectory
           TEST=true
           break
         else  
           if [ `tune2fs -l /dev/sdb1 | grep BA | wc -l` -gt 0 ]; then
             USB_DEVICE="/dev/sdb1"
             echo "$USB_DEVICE" > /config/.badevice
             echo "/tmp/usb/XBA (2)" > /config/.badirectory
             TEST=true
             break 
           else
             if [ `tune2fs -l /dev/sdc1 | grep BA | wc -l` -gt 0 ]; then
               USB_DEVICE="/dev/sdc1"
               echo "$USB_DEVICE" > /config/.badevice
               echo "/tmp/usb/XBA (3)" > /config/.badirectory
               TEST=true
               break
             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/XBA (1)" > /config/.badirectory
                 TEST=true
                 break
               else
                 if [ `tune2fs -l /dev/sdb | grep BA | wc -l` -gt 0 ]; then
                   USB_DEVICE="/dev/sdb"
                   echo "$USB_DEVICE" > /config/.badevice
                   echo "/tmp/usb/XBA (2)" > /config/.badirectory
                   TEST=true
                   break 
                 else
                   tune2fs -l /dev/sda1 > /tmp/testt 2>&1
                   if [ `cat /tmp/testt | grep "is mounted" | wc -l` -gt 0 ]; then
                     USB_DEVICE="/dev/sda1"
                     echo "$USB_DEVICE" > /config/.badevice
                     echo "/tmp/usb/XBA (1)" > /config/.badirectory
                     TEST=true
                     break
                   else
                     tune2fs -l /dev/sdb1 > /tmp/testt 2>&1
                     if [ `cat /tmp/testt | grep "is mounted" | wc -l` -gt 0 ]; then
                       USB_DEVICE="/dev/sdb1"
                       echo "$USB_DEVICE" > /config/.badevice
                       echo "/tmp/usb/XBA (2)" > /config/.badirectory
                       TEST=true
                       break
                     fi
                   fi  
                 fi    
               fi  
             fi    
           fi
         fi
         done
       
        
         if [ $TEST = "false" ]; then
           if [ `df | grep "BA" | wc -l` -gt 0 ]; then
             USB_DEVICE=`df | grep /tmp/usb/XBA | 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
             TEST=true
           fi
         fi    
         
         if [ $TEST = "false" ]; then
           echo "BA Device not found --> use last BA Device --> $USB_DEVICE"
         else
           echo "BA Device found on $USB_DEVICE"  
         fi  
         
         BAROOTM=`cat /config/.badirectory`     
         
         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
      rm -r /dev/stapi
      /tmp/ba/barryallen.sh
      sync
      rm /tmp/ba
      echo "bootmenu ende \n"
      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
            echo "mount /proc"
            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"
            echo "mount /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 [ ! -f "$BAROOT/ba/ba/$TARGET/config/start-ext.sh" ]; then
           if [ `mount | grep "$TARGET/var" | wc -l` -lt 1 ]; then
              mount -o bind /var  "$BAHOME/ba/$TARGET/var"
           fi
         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
         
         ## mount Flash
         mkdir "$BAHOME/ba/$TARGET/flash" > /dev/null 2>&1
         mkdir "$BAHOME/ba/$TARGET/flash/data" > /dev/null 2>&1
         mkdir "$BAHOME/ba/$TARGET/flash/config" > /dev/null 2>&1
         mount -o bind /data  "$BAHOME/ba/$TARGET/flash/data"
         mount -o bind /config  "$BAHOME/ba/$TARGET/flash/config"
         
         ## Swapfile
         mkdir "$BAHOME/ba/$TARGET/swap" > /dev/null 2>&1
         #mkdir "$BAHOME/ba/$TARGET/$BAROOTM"
         #mount -o bind "$BAROOT" "$BAHOME/ba/$TARGET/$BAROOTM"
                  
         #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
         
         killall vsftpd > /dev/null 2>&1
         killall telnetd > /dev/null 2>&1
         
         echo "start chroot\n"
         
         exec chroot "$BAHOME/ba/$TARGET" /etc/init.d/bainit
         exit 0
      else
         umount "$BAROOT"
         #BAROOT="/tmp/usb/XBA (1)"
         #if [ -f /config/.badirectory ]; then
         #   BAROOT=`cat /config/.badirectory`
         #fi
         #mount $USB_DEVICE "$BAROOT"
         rmmod stmfb.ko
         rmmod stmcore-display-stx7100.ko
         rmmod cfbcopyarea.ko
         rmmod cfbfillrect.ko
         rmmod cfbimgblt.ko
         rmmod fb.ko
         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
      
      # start swap
      if [ -f /swap/.status ]; then
        if [ `cat /swap/.status | grep "on" | wc -l` -gt 0 ]; then
          swapon /swap/swapfile
        fi
      fi    
              
      /data/checkhdmount.sh > /tmp/checkhdmount.log &
      # restart gui 
      rmmod stmfb.ko
      rmmod stmcore-display-stx7100.ko
      rmmod cfbcopyarea.ko
      rmmod cfbfillrect.ko
      rmmod cfbimgblt.ko
      rmmod fb.ko
      if [ -f /config/start-ext.sh ]; then
        /config/start-ext.sh
      else  
        if [ -f /config/start.sh ]; then
           /config/start.sh
        else
           /app/start.sh
        fi
      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" 
      ;;
      13)
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM" > /dev/null 2>&1
        "$BAROOTM/ba/tools/e2label" $2 $3
        sync 
      ;;
      14)
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM" > /dev/null 2>&1
        echo "*** UBOOT Parameter begin"
        "$BAROOTM/ba/tools/fw_printenv" bootargs
        echo "***"
        "$BAROOTM/ba/tools/fw_printenv" bootcmd
        echo "*** UBOOT Parameter end"
      ;;
      20)
        echo "erstelle Swapfile --> Bitte Warten"
        mkdir "$BAROOTM"> /dev/null 2>&1
        mount $USB_DEVICE "$BAROOTM" > /dev/null 2>&1
        dd if=/dev/zero of=/swap/swapfile bs=1024 count=16384
        "$BAROOTM/ba/tools/mkswap" /swap/swapfile
        sync 
        echo "Swapfile wurde erstellt"
        echo "swapfile aktivieren mit /config/bainit 21"
        echo "swapfile deaktivieren mit /config/bainit 22"
      ;;
      21)
        if [ -f /swap/swapfile ]; then
          swapon /swap/swapfile
          echo "on" > /swap/.status
          sync 
          echo "Swapfile wurde gestartet"
        else
          echo "es mu erst mit /config/bainit 20 ein Swapfile erstellt werden"  
        fi
      ;;
      22)
        swapoff /swap/swapfile
        echo "off" > /swap/.status
        sync 
        echo "Swapfile wurde gestoppt"
      ;;
      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
