#!/bin/sh
CF_KARTE=/.cf-karte
CF_PATH=/dev/ide/host1/bus0/target0/lun0/disc
REBOOT_TIMEOUT=5
MULTIBOOTKIT=multiboot04.tar

case "$1" in
   "reboot")
      echo "------------------------------------------"
      echo "Rebooting in " $REBOOT_TIMEOUT "seconds"  
      echo "Press CTRL + C for interrupting"
      echo "------------------------------------------"
      sleep $REBOOT_TIMEOUT
      echo "Rebooting NOW"
      echo "------------------------------------------"
      shutdown -r now
      exit 0
   ;;
   "shutdown")
      echo "------------------------------------------"
      echo "Shutdown in " $REBOOT_TIMEOUT "seconds"  
      echo "Press CTRL + C for interrupting"
      echo "------------------------------------------"
      sleep $REBOOT_TIMEOUT
      echo "Shutting down NOW"
      echo "------------------------------------------"
      shutdown -h now
      exit 0
   ;;
   "show")
echo "Mounting all 3 Partitions - ignore error if already mounted"
mkdir /media/cf1
mkdir /media/cf2
mkdir /media/cf
mount /dev/ide/host1/bus0/target0/lun0/part2 /media/cf1
mount /dev/ide/host1/bus0/target0/lun0/part3 /media/cf2
mount /dev/ide/host1/bus0/target0/lun0/part4 /media/cf3
      echo "Showing available Partitions"
      cat /media/cf1/.cf-karte
      cat /media/cf2/.cf-karte
      cat /media/cf3/.cf-karte
      echo "------------------------------------------"
      echo "You are bootet from"
      if  [ ! -f $CF_KARTE ]; then
        echo "Flash !"
      else
        cat /.cf-karte
      fi
      echo "------------------------------------------"
echo "Linux Version"
uname -a
echo "Image Version"
cat /etc/image-version
cat /etc/version
 exit 0
   ;;
   "enable")
         echo "Initializing CF card for Multiboot ..."
         if  [ ! -f $CF_PATH ]; then
             echo "CF Card available"
         else
             echo "----------------------------------------------------"
             echo "CF Card not available - cann't do anything - EXITING !"
             echo "Try Rebooting with CF card inserted"
             exit 1
             echo "----------------------------------------------------"
         fi
         umount /media/cf
         umount /media/cf1
         umount /media/cf2
         umount /media/cf3
         fdisk -v
         fdisk <init.cmd /dev/ide/host1/bus0/target0/lun0/disc
         ls /dev/ide/host1/bus0/target0/lun0
         echo "Installing dos tools ..."
         ipkg install dosfstools_2[1].10-r0_mipsel.ipk
         echo "Making Filesystems ..."
         mkdosfs -F 16 /dev/ide/host1/bus0/target0/lun0/part1
         mkfs.ext3 /dev/ide/host1/bus0/target0/lun0/part2
         mkfs.ext3 /dev/ide/host1/bus0/target0/lun0/part3
         mkfs.ext3 /dev/ide/host1/bus0/target0/lun0/part4
         echo "Making directories ..."
         mkdir /media/cf
         mkdir /media/cf1
         mkdir /media/cf2
         mkdir /media/cf3
         echo "Mounting new Partitions ..."
         mount /dev/ide/host1/bus0/target0/lun0/part1 /media/cf
         mount /dev/ide/host1/bus0/target0/lun0/part2 /media/cf1
         mount /dev/ide/host1/bus0/target0/lun0/part3 /media/cf2
         mount /dev/ide/host1/bus0/target0/lun0/part4 /media/cf3
         df -k
         echo "Filling Boot Partition on CF with files from istall directory  ..."
         cp /tmp/$MULTIBOOTKIT /media/cf
         cd /media/cf
         tar -xvf $MULTIBOOTKIT
         cd /boot
         tar cf - * | ( cd /media/cf && tar xvf - )
        cd / media/cf  
	;;
   "disable")
         echo "Erasing CF card ...."
         if  [ ! -f $CF_PATH ]; then
             echo "CF Card available"
         else
             echo "----------------------------------------------------"
             echo "CF Card not available - cann't do anything - EXITING !"
             echo "Try Rebooting with CF card inserted"
             exit 1
             echo "----------------------------------------------------"
         fi
         umount /media/cf
         umount /media/cf1
         umount /media/cf2
         umount /media/cf3
         fdisk -v
         fdisk <erase.cmd /dev/ide/host1/bus0/target0/lun0/disc
         echo "Making Filesystem ..."
         mkfs.ext3 /dev/ide/host1/bus0/target0/lun0/disc
         mkdir /media/cf
         mount /dev/ide/host1/bus0/target0/lun0/disc /media/cf
         rmdir /media/cf1
         rmdir /media/cf2
         rmdir /media/cf3
	;;
   "copy")
         if  [ ! -f $CF_PATH ]; then
             echo "CF Card available"
         else
             echo "----------------------------------------------------"
             echo "CF Card not available - cann't do anything - EXITING !"
             echo "Try Rebooting with CF card inserted"
             exit 1
             echo "----------------------------------------------------"
         fi
cd /media/cf
echo "Mounting all 3 Partitions - ignore error if already mounted"
mkdir /media/cf1
mkdir /media/cf2
mkdir /media/cf3
mount /dev/ide/host1/bus0/target0/lun0/part2 /media/cf1
mount /dev/ide/host1/bus0/target0/lun0/part3 /media/cf2
mount /dev/ide/host1/bus0/target0/lun0/part4 /media/cf3
echo "Starting work ----------"
case "$2" in
   "1")
 	 echo "Copying Flash to Partition" $2
         cd /media/cf$2
         echo "Cleaning directory..."
         pwd
         echo "Removing links..."
         rm mnt share hdd tmp
         echo "Removing all Files ..."
         rm -r *
         cd /
         echo "Copying Flash now ..."
         tar -cf - bin etc home lib sbin usr var | (cd /media/cf$2 && tar xvf - )
         cd /media/cf$2
         mkdir boot dev proc sys media
         cd /media/cf$2/media
         mkdir card cf cf1 cf2 cf3 cifs dvd hdd mmc1 net nfs ram realroot union usb
         echo "Labeling Partition ..."
         cd /media/cf$2
         echo "Partition " $2 > .cf-karte
         echo "Creating missing Links ..."
         ln -s /media/hdd hdd
         ln -s /var/tmp tmp
         ln -s /media mnt
         ln -s /usr/share share
         echo "Copying kernel to Bootpartition ..."
         cp /boot/vmlinux.gz /media/cf/vmlinux.gz$2
         echo "Extending /etc/fstab of CF Partiton" $2
         echo "/dev/ide/host1/bus0/target0/lun0/part2 /media/cf1 auto defaults 0 0" >> /media/cf1/etc/fstab
         echo "/dev/ide/host1/bus0/target0/lun0/part3 /media/cf2 auto defaults 0 0" >> /media/cf1/etc/fstab
         echo "/dev/ide/host1/bus0/target0/lun0/part4 /media/cf3 auto defaults 0 0" >> /media/cf1/etc/fstab
         echo "Finished copying Flash to Partition " $2
	;;
   "2")
 	 echo "Copying Flash to Partition" $2
         cd /media/cf$2
         echo "Cleaning directory..."
         pwd
         echo "Removing links..."
         rm mnt share hdd tmp
         echo "Removing all Files ..."
         rm -r *
         cd /
         echo "Copying Flash now ..."
         tar -cf - bin etc home lib sbin usr var | (cd /media/cf$2 && tar xvf - )
         cd /media/cf$2
         mkdir boot dev proc sys media
         cd /media/cf$2/media
         mkdir card cf cf1 cf2 cf3 cifs dvd hdd mmc1 net nfs ram realroot union usb
         echo "Labeling Partition ..."
         cd /media/cf$2
         echo "Partition " $2 > .cf-karte
         echo "Creating missing Links ..."
         ln -s /media/hdd hdd
         ln -s /var/tmp tmp
         ln -s /media mnt
         ln -s /usr/share share
         echo "Copying kernel to Bootpartition ..."
         cp /boot/vmlinux.gz /media/cf/vmlinux.gz$2
         echo "Extending /etc/fstab of CF Partiton" $2
         echo "/dev/ide/host1/bus0/target0/lun0/part2 /media/cf1 auto defaults 0 0" >> /media/cf2/etc/fstab
         echo "/dev/ide/host1/bus0/target0/lun0/part3 /media/cf2 auto defaults 0 0" >> /media/cf2/etc/fstab
         echo "/dev/ide/host1/bus0/target0/lun0/part4 /media/cf3 auto defaults 0 0" >> /media/cf2/etc/fstab
         echo "Finished copying Flash to Partition " $2
	;;
   "3")
 	 echo "Copying Flash to Partition" $2
         cd /media/cf$2
         echo "Cleaning directory..."
         pwd
         echo "Removing links..."
         rm mnt share hdd tmp
         echo "Removing all Files ..."
         rm -r *
         cd /
         echo "Copying Flash now ..."
         tar -cf - bin etc home lib sbin usr var | (cd /media/cf$2 && tar xvf - )
         cd /media/cf$2
         mkdir boot dev proc sys media
         cd /media/cf$2/media
         mkdir card cf cf1 cf2 cf3 cifs dvd hdd mmc1 net nfs ram realroot union usb
         echo "Labeling Partition ..."
         cd /media/cf$2
         echo "Partition " $2 > .cf-karte
         echo "Creating missing Links ..."
         ln -s /media/hdd hdd
         ln -s /var/tmp tmp
         ln -s /media mnt
         ln -s /usr/share share
         echo "Copying kernel to Bootpartition ..."
         cp /boot/vmlinux.gz /media/cf/vmlinux.gz$2
         echo "Extending /etc/fstab of CF Partiton" $2
         echo "/dev/ide/host1/bus0/target0/lun0/part2 /media/cf1 auto defaults 0 0" >> /media/cf3/etc/fstab
         echo "/dev/ide/host1/bus0/target0/lun0/part3 /media/cf2 auto defaults 0 0" >> /media/cf3/etc/fstab
         echo "/dev/ide/host1/bus0/target0/lun0/part4 /media/cf3 auto defaults 0 0" >> /media/cf3/etc/fstab
         echo "Finished copying Flash to Partition " $2
	;;
    "0")
 	 echo "Copying Flash Kernel to Boot Partition" $2
         cp /boot/vmlinux.gz /media/cf/vmlinux.gz$2

         echo "Rest stays in Flash !"
	;;
    *)
	echo "Usage: $0  copy {0|1|2|3}"
	exit 1
	;;
esac
;;
"boot")

         if  [ ! -f $CF_PATH ]; then
             echo "CF Card available"
         else
             echo "----------------------------------------------------"
             echo "CF Card not available - cann't do anything - EXITING !"
             echo "Try Rebooting with CF card inserted"
             exit 1
             echo "----------------------------------------------------"
         fi
cd /media/cf
case "$2" in
   "1")
 	 echo "Changing next CF Boot Partition to" $2
         cp autorun.bat1 autorun.bat
         cp vmlinux.gz1 vmlinux.gz
         cat /media/cf/autorun.bat
	;;
    "2")
 	 echo "Changing next CF Boot Partition to" $2
         cp autorun.bat2 autorun.bat
         cp vmlinux.gz2 vmlinux.gz
         cat /media/cf/autorun.bat
	;;
    "3")
 	 echo "Changing next CF Boot Partition to" $2
         cp autorun.bat3 autorun.bat
         cp vmlinux.gz3 vmlinux.gz
         cat /media/cf/autorun.bat
	;;
    "0")
 	 echo "Changing next Boot Partition to Flash"
         cp autorun.bat0 autorun.bat
         cp vmlinux.gz0 vmlinux.gz
         cat /media/cf/autorun.bat
	;;
    *)
	echo "Usage: $0 boot  {0|1|2|3}"
	exit 1
	;;
esac

;;
    *)
	echo "Usage: $0 {enable|disable|copy|boot|show|reboot|shutdown} {0|1|2|3}"
	exit 1
	;;
esac
