#!/bin/sh
### BEGIN INIT INFO
# Provides:						detectbigflash
# Required-Start:			$local_fs
# Required-Stop:			$local_fs $named
# Default-Start:			2 3 4 5
# Default-Stop:				0 1 6
# Short-Description:	Detect and mount USB drive
### END INIT INFO
echo Detect Large Flash Storage /etc/init.d/boottime/detectbigflash.sh > /var/tmp/dbf.log
# ---------------------------------------------------------------------------------------
# Terminal colors
NORMAL='\033[0m'
GOOD='\033[32;01m'
WARN='\033[33;01m'
BAD='\033[31;01m'
BRACKET='\033[34;01m'

#
MNTFLASH='/var/sdcard'
USBFLASH='/mnt/3/local'
USBDRIVE1='/mnt/3'
USBDRIVE2='/mnt/4'
USBDRIVE3='/mnt/5'
USBDRIVE4='/mnt/6'
MNTLOG='/var/sdcardlog'
USBLOG='/mnt/5'
USELOG='/var/sqllog'

# Terminal cursor positions.
# It's tempting to use absolute column positions (Esc [ n G sequences), but
# some terminal emulators can't handle them so let's use newline followed by
# relative up and right movement instead.
OKPOS='\n\033[2A\033[72C'
FAILPOS='\n\033[2A\033[71C'
INFOPOS='\n\033[A\033[4C'
# ---------------------------------------------------------------------------------------
information() {
	echo -e "${INFOPOS}${GOOD}+${NORMAL} $*"
}
# ---------------------------------------------------------------------------------------
begin() {
	echo -e " ${GOOD}*${NORMAL} $*... "
}
# ----------------------------------------------------------------------end does not exit.
end() {
	if [ "$1" = 0 ]; then
		echo -e "${OKPOS}${BRACKET}[ ${GOOD}ok${BRACKET} ]${NORMAL}"
	else
		shift
		if [ -n "$1" ]; then
			echo -e "${INFOPOS}${BAD}!${NORMAL} $*"
		fi
		echo -e "${FAILPOS}${BRACKET}[ ${BAD}fail${BRACKET} ]${NORMAL}"
	fi
}
# ---------------------------------------------------------------------------------------
mount_data(){
  mount -t ext4 $DISKAT1 $USBDRIVE1 
  mount -t ext4 $DISKAT2 $USBDRIVE2  
  mount -t ext4 $DISKAT3 $USBDRIVE3  
  mount -t ext4 $DISKAT4 $USBDRIVE4  
}
# ---------------------------------------------------------------------------------------
check_disk(){
#TIMECLOCK   /usr/local/bin/sendchild "Starting Clock" "Checking Data" "" ""
#JOBCLOCK   /usr/local/bin/sendchild -l 16 "Starting Clock" "Checking Data" "" ""
  /usr/local/bin/sendchild "Starting Clock" "Checking Data" "" ""
  begin "Check USB Drive Integrity -p $1"
	e2fsck -p $1
	if [ "$?" -eq "0" ]; 
	then
	  end 0
		return 0
	else
	  end 0 
		begin "Check USB Drive Integrity -y $1"
		e2fsck -y $1
		if [ "$?" -eq "0" ]; 
		then
			end 0
			return 0
		else	
			if [ "$?" -eq "1" ]; 
			then
				end 0
				return 0
			else
				echo all yours bruce $?
#TIMECLOCK 				/usr/local/bin/sendchild "Starting Clock" "Problem 7 Detected" "Contact Support" "www.cstimeclocks.com" 
#JOBCLOCK 				/usr/local/bin/sendchild -l 16 "Problem 7" "Contact Support" "" ""
				/usr/local/bin/sendchild "Starting Clock" "Problem 7 Detected" "Contact Support" "www.cstimeclocks.com" 
				end 1
				return 1
			fi	
		fi	
	fi
#TIMECLOCK 	/usr/local/bin/sendchild "Starting Clock" "" "" ""
#JOBCLOCK 	/usr/local/bin/sendchild -l 16 "Starting Clock" "" "" ""
	/usr/local/bin/sendchild "Starting Clock" "" "" ""
	return 0
}
# ---------------------------------------------------------------------------------------
find_linux(){
	STORAGEAT=""
	result=`fdisk -l | grep Linux | grep /dev/sda1`
	if [ -n "$result" ];
	then
		STORAGEAT="/dev/sda"
		DISKAT1="/dev/sda1"
		result=`fdisk -l | grep Linux | grep /dev/sda2`
		if [ -n "$result" ];
		then
		  DISKAT2="/dev/sda2"
			DISKAT3="/dev/sda3"
			DISKAT4="/dev/sda4"
		fi
	else
		result=`fdisk -l | grep Linux | grep /dev/sdb1`
		if [ -n "$result" ];
		then
			STORAGEAT="/dev/sdb"
			DISKAT1="/dev/sdb1"
			result=`fdisk -l | grep Linux | grep /dev/sdb2`
			if [ -n "$result" ];
			then
				DISKAT2="/dev/sdb2"
				DISKAT3="/dev/sdb3"
				DISKAT4="/dev/sdb4"
			fi
		fi
	fi
}


case "$1" in
  start)
		# --------------------------------------------main code here--------------------------------------
		# it's important to _mount_ the internal usb to $USBDRIVE1 even if it's not being used
		# otherwise it will interfere with backup / sync / restore usb functionality
#TIMECLOCK 		/usr/local/bin/sendchild "Starting Clock" "" ""
#JOBCLOCK 		/usr/local/bin/sendchild -l 16 "Starting Clock" "" ""
		/usr/local/bin/sendchild "Starting Clock" "" ""

		# stop programs
#		/etc/init.d/clock stop

		# unmount any drives attached to /mnt/1 or $USBDRIVE1
		result=` mount | grep /mnt/1`
		if [ -n "$result" ];
		then
			umount /mnt/1
		fi

		result=` mount | grep $USBDRIVE1`
		if [ -n "$result" ];
		then
			umount $USBDRIVE1
		fi

		result=` mount | grep $USBDRIVE2`
		if [ -n "$result" ];
		then
			umount $USBDRIVE2
		fi


		result=` mount | grep $USBDRIVE3`
		if [ -n "$result" ];
		then
			umount $USBDRIVE3
		fi

		result=` mount | grep $USBDRIVE4`
		if [ -n "$result" ];
		then
			umount $USBDRIVE4
		fi

		#   detect existing stuff
		find_linux
		if [ -n "$STORAGEAT" ];
		then
			information  "Linux partition found"
			if [ -n "$DISKAT2" ];
			then
				check_disk $DISKAT1
				check_disk $DISKAT2
				check_disk $DISKAT3
				check_disk $DISKAT4
			fi
			if [ "$?" -eq "0" ];
			then
				information "mounting drives"
				mount_data
				if [ "$?" -ne "0" ];
				then
					information "Unable to Mount USB drive."
					# possibly fall back on $MNTFLASH here?
					exit
				fi
			else
#TIMECLOCK 				/usr/local/bin/sendchild "Starting Clock" "Reboot Required" "Rebooting" ""
#JOBCLOCK 				/usr/local/bin/sendchild -l 16 "Reboot Required" "Rebooting" "" ""
				/usr/local/bin/sendchild "Starting Clock" "Reboot Required" "Rebooting" ""
				reboot
				exit
			fi
			else
#TIMECLOCK 			/usr/local/bin/sendchild "Internal Storage" "Not Found" "Contact support" "www.cstimeclocks.com"	
#JOBCLOCK 			/usr/local/bin/sendchild -l 16 "Internal Storage" "Not Found" "" ""	
			/usr/local/bin/sendchild "Internal Storage" "Not Found" "Contact support" "www.cstimeclocks.com"	
			exit
		fi
		information "Check file attributes"
		#----- some start-up maintainence
		chmod 0777 /var/tmp
		chmod 0777 /etc
		chmod 0777 /etc/init.d
		chmod 0777 /usr/local/bin
		chmod 0777 /usr/local/lib

#TIMECLOCK 			/usr/local/bin/sendchild "Starting Clock" "Checking Database" "Format" ""
#JOBCLOCK 			/usr/local/bin/sendchild -l 16 "Starting Clock" "Checking DB" "" ""
		/usr/local/bin/sendchild "Starting Clock" "Checking Database" "Format" ""
		information "Running alter.sql and altermenus.sql to make sure database is the right version"
		#/usr/local/bin/sqlite3shell /var/local/data/capeclock.db < /usr/local/bin/alter.sql > /dev/null
		/usr/local/bin/db_alter --quiet
    ;;
  stop)
    ;;
  init)
		# --------------------------------------------main code here--------------------------------------
		# it's important to _mount_ the internal usb to $USBDRIVE1 even if it's not being used
		# otherwise it will interfere with backup / sync / restore usb functionality
#TIMECLOCK 		/usr/local/bin/sendchild "Starting Clock" "" ""
#JOBCLOCK 		/usr/local/bin/sendchild -l 16 "Starting Clock" "" ""
		/usr/local/bin/sendchild "Starting Clock" "" ""

		# stop programs
		/etc/init.d/clock stop

		# unmount any drives attached to /mnt/1 or $USBDRIVE1
		result=` mount | grep /mnt/1`
		if [ -n "$result" ];
		then
			umount /mnt/1
		fi

		result=` mount | grep $USBDRIVE1`
		if [ -n "$result" ];
		then
			umount $USBDRIVE1
		fi

		result=` mount | grep $USBDRIVE2`
		if [ -n "$result" ];
		then
			umount $USBDRIVE2
		fi


		result=` mount | grep $USBDRIVE3`
		if [ -n "$result" ];
		then
			umount $USBDRIVE3
		fi

		result=` mount | grep $USBDRIVE4`
		if [ -n "$result" ];
		then
			umount $USBDRIVE4
		fi

		#   detect existing stuff
		find_linux
		if [ -n "$STORAGEAT" ];
		then
			information  "Linux partition found"
			if [ -n "$DISKAT2" ];
			then
				check_disk $DISKAT1
			fi	
			if [ "$?" -eq "0" ]; 
			then
				information "mounting drives"
				mount_data
				if [ "$?" -ne "0" ]; 
				then
					information Unable to Mount USB drive.
					# possibly fall back on $MNTFLASH here?
					exit
				fi
			else
#TIMECLOCK 				/usr/local/bin/sendchild "Starting Clock" "Reboot Required" "Rebooting" ""
#JOBCLOCK 				/usr/local/bin/sendchild -l 16 "Reboot Required" "Rebooting" "" ""
				/usr/local/bin/sendchild "Starting Clock" "Reboot Required" "Rebooting" ""
				reboot
				exit
			fi	
		else
#TIMECLOCK 			/usr/local/bin/sendchild "Internal Storage" "Not Found" "Contact support" "www.cstimeclocks.com"	
#JOBCLOCK 			/usr/local/bin/sendchild -l 16 "Internal Storage" "Not Found" "" ""	
			/usr/local/bin/sendchild "Internal Storage" "Not Found" "Contact support" "www.cstimeclocks.com"	
			exit
		fi
		information "Check file attributes"
		#----- some start-up maintainence
		chmod 0777 /var/tmp
		chmod 0777 /etc
		chmod 0777 /etc/init.d
		chmod 0777 /usr/local/bin
		chmod 0777 /usr/local/lib

#TIMECLOCK 		/usr/local/bin/sendchild "Starting Clock" "Checking Database" "Format" ""
#JOBCLOCK 		/usr/local/bin/sendchild -l 16 "Starting Clock" "Checking DB" "" ""
		/usr/local/bin/sendchild "Starting Clock" "Checking Database" "Format" ""
		information "Running alter.sql and altermenus.sql to make sure database is the right version"
		#/usr/local/bin/sqlite3shell /var/local/data/capeclock.db < /usr/local/bin/alter.sql > /var/null
		/usr/local/bin/db_alter --quiet
#		/etc/init.d/clock start
  	;;
  *)
    echo "Usage: /etc/init.d/detectbigflash {start|init|stop}"
    exit 1
    ;;
esac

exit 0
