#!/bin/sh
# 
# UFS-910 Barry Allen bainit by gutemine
#
VERSION=0.1.0
#
#echo $0 $1
BAHOME=/tmp

if [ $# -eq 0 ]; then
   if [ ! -f /mnt/usb/ba/ba.sh ]; then
      mount /dev/sda1 /mnt/usb > /dev/null 2>&1
   fi
   if [ -f /mnt/usb/ba/ba.sh ]; then
      BAHOME=/mnt/usb/ba
   else
      echo "Barry Allen not (yet) installed on USB"
   fi
   if [ ! -f $BAHOME/.baboot ]; then
      echo Flash > $BAHOME/.baboot
   fi
 if [ ! -f /.bainfo ]; then
    TARGET=`cat $BAHOME/.baboot`
    if [ -d $BAHOME/ba/$TARGET ]; then
      
    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/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/mnt/usb | wc -l` -lt 1 ]; then
       mount -o bind /mnt/usb $BAHOME/ba/$TARGET/mnt/usb
    fi
    killall -9 XXXXX > /dev/null 2>&1
    # make sure that latest bainit is used from image
    cp $BAHOME/bainit $BAHOME/ba/$TARGET/etc/init.d/bainit
    # not start image in chroot environment with bainit
    exec chroot $BAHOME/ba/$TARGET /etc/init.d/bainit
    exit 0
  fi
else
  # restart new image
  /etc/init.d/rc S
  # restart telentd so that telnet logons will go to new image
  /sbin/telnetd -l /bin/sh &
  # restart gui 
  sh /app/start.sh &
fi
else
   exec /etc/init.d/rc $1
fi
exit 0
