#!/bin/sh 

/bin/flashup	

/sbin/insmod /mai/mae-driver.ko mae_phys_address=0x6C00000 mae_bpp=32 mae_xsize=640 mae_ysize=480

/sbin/insmod /mnt/hd/system/usr/glib_driver/gpio.ko
/sbin/insmod /mnt/hd/system/usr/glib_driver/detect_dev.ko

# insert onme mpre script line 
/sbin/modprobe ac97_plugin_wm97xx

# for Divx Drm
/etc/secserv

# IVOS Auto Run

CLEARBUFFER=1

SYSPATH="/mnt/hd/system"
PWPATH="/usr/sbin"
V43BIN="v43.bin"
SKBIN="pmp2000s.bin"
EDUBIN="edu.bin"
BLUBIN="bmp1420.bin"
JNCBIN="m100.bin"

NAVIBIN="navi.bin"
SDMBBIN="sdmb.bin"
SDMBTEST="cdmapp.bin"
USERBIN="user.bin"
TDMBBIN="tdmb.bin"
IVOSBIN=$V43BIN

IVOS_CODE=64
NAVI_CODE=65
SDMB_CODE0=66
SDMB_CODE1=67
SDMB_CODE2=68
SDMB_TEST=69
USER_CODE=70
TDMB_CODE=71


if [ -f "$SYSPATH/$V43BIN" ]
then
	IVOSBIN=$V43BIN	
elif [ -f "$SYSPATH/$SKBIN" ]
then
	IVOSBIN=$SKBIN
elif [ -f "$SYSPATH/$EDUBIN" ]
then
	IVOSBIN=$EDUBIN
elif [ -f "$SYSPATH/$BLUBIN" ]
then
	IVOSBIN=$BLUBIN
else
	IVOSBIN=$JNCBIN
fi
		
if [ -e /mnt/hd/system/user.bin -a -e /mnt/hd/user/Launcher -a -e /mnt/hd/user/lib ]; then
ret=$USER_CODE
else
ret=$IVOS_CODE
fi

if [ -s "/$IVOSBIN" ]; then
	ORSIZE=$(/bin/ls -l /$IVOSBIN | /usr/bin/awk '{print $5}')
	CLSIZE=$(/bin/ls -l $SYSPATH/$IVOSBIN | /usr/bin/awk '{print $5}')
	if [ $ORSIZE -ne $CLSIZE ] || [ "$SYSPATH/$IVOSBIN" -nt "/$IVOSBIN" ] || [ "/$IVOSBIN" -ot "$SYSPATH/$IVOSBIN" ]; then
		echo "different files!!!"
		/bin/cp $SYSPATH/$IVOSBIN /$IVOSBIN
		/bin/sync	
		/bin/touch $SYSPATH/$IVOSBIN /$IVOSBIN
		/bin/sync	
	else
		echo "same files!!!"
	fi
else
		echo "No file!!!"
		/bin/cp $SYSPATH/$IVOSBIN /$IVOSBIN
		/bin/sync	
		/bin/touch $SYSPATH/$IVOSBIN /$IVOSBIN		
		/bin/sync
fi

do_excute()
{
	/bin/sync
	echo $1
	echo $2

	echo "$1 excute....."
	
	if [ -s "$SYSPATH/$1" ]; then
		 if	[ "$1" = "v43pwroff.bin" ]; then
				 "$PWPATH/$1" $2
		 else 
				 "$SYSPATH/$1" $2
		 fi
	elif [ -s "$PWPATH/$1" ]; then
		"$PWPATH/$1" $2
	else
		echo "no $1 excute....."
	fi
}

app_excute()
{
	/bin/sync
	echo $1
	echo $2

	echo "$1 excute....."
	
	if [ -s "/$1" ]; then
				 "/$1" $2
	fi
}


# infinite loop for switch program
# donot clear framebuffer image...
while true
do
	echo "infinite loop $ret"
	
	case $ret in
	$IVOS_CODE)
		app_excute $IVOSBIN $CLEARBUFFER
		ret=$?
		echo "$IVOSBIN return $ret"
		CLEARBUFFER=0
		;;
	$NAVI_CODE) 
		do_excute $NAVIBIN 0
		ret=$?
		echo "$NAVIBIN return $ret"
	  sleep 3
		;;
	$SDMB_CODE0)
	  insmod /sdmb/cdmdrv.ko
		do_excute $SDMBBIN 0
		ret=$?
		echo "$SDMBBIN return $ret"
		rmmod cdmdrv
		;;	
	$SDMB_CODE1)
	  insmod /sdmb/cdmdrv.ko
		do_excute $SDMBBIN 1
		ret=$?
		echo "$SDMBBIN return $ret"
		rmmod cdmdrv
		;;
	$SDMB_CODE2)
	  insmod /sdmb/cdmdrv.ko
		do_excute $SDMBBIN 2
		ret=$?
		echo "$SDMBBIN return $ret"
		rmmod cdmdrv
		;;
	$SDMB_TEST)
	  insmod /sdmb/cdmdrv.ko
		do_excute $SDMBTEST 0
		ret=$?
		echo "$SDMBTEST return $ret"
		rmmod cdmdrv
		;;
	$USER_CODE)
		do_excute $USERBIN 0
		ret=$?
		echo "$USERBIN return $ret"
	  sleep 3
		;;
	$TDMB_CODE)
		rm -rf /dev/usb
		/sbin/insmod /lib/modules/2.6.11-r000049/kernel/drivers/usb/core/usbcore.ko
		/sbin/insmod /lib/modules/2.6.11-r000049/kernel/drivers/usb/host/ehci-hcd.ko
		/sbin/insmod /lib/modules/2.6.11-r000049/kernel/drivers/usb/host/ohci-hcd.ko
		/sbin/insmod /mnt/hd/system/usr/glib_driver/dabusb.ko
		do_excute $TDMBBIN 0
		ret=$?
		echo "$TDMBBIN return $ret"
		CLEARBUFFER=1
		/sbin/rmmod dabusb
		/sbin/rmmod ohci-hcd
		/sbin/rmmod ehci-hcd
		/sbin/rmmod usbcore
		;;
	*)
		echo "power off!"
		do_excute v43pwroff.bin 0
		break
		;;	
	esac
done	
