#!/bin/sh
export CONF_DIR=/etc/sysconfig
source ${CONF_DIR}/config
# need for load_module function
source ${CONF_DIR}/helpers/functions

make_devices()
{
    # we'll assume these all succeed
	mknod /dev/i2c-0 c 89 0 > /dev/null 2>&1
	mknod /dev/i2c-1 c 89 1 > /dev/null 2>&1
	mknod /dev/i2c-2 c 89 2 > /dev/null 2>&1
	mknod /dev/i2c-3 c 89 3 > /dev/null 2>&1
	mknod /dev/mem c 1 1 > /dev/null 2>&1
	mknod /dev/qcc c 254 0 > /dev/null 2>&1
}

load_modules()
{
	for module in $*
	do
		load_module ${module}
		if [ $? -ne 0 ]
		then
			echo -ne "ERROR:\nFailed to load module ${module}.\n"
			exit 1
		fi
	done
	sleep 1
}

make_devices
load_modules i2c-core i2c-dev i2c-mux dwapbi2c qcc

if [ "X${CONFIG_SPLASH_SCREEN_IN_FS}" == "Xy" ] && [ $# -eq 0 ]
then    
    # XXX NOT COMPLETE !!!!
	if [ ! -d /splash ]
	then
		/bin/mkdir -p /data;
		if [ "X$?" != "X0" ]
		then
			return 1
		fi
	fi	
	FILEPATH="/splash"

	source /etc/sysconfig/devices
	i=0
	OLD_IFS=${IFS}
	IFS=","
	for part in ${CFGDEVICES_PARTITION_MTDPARTS}
	do
		name=$(echo ${part} | sed -e 's/[^\(]*[\(]\([^\)]*\)[\)].*/\1/')
		if [  "X${name}" == "Xsplash" ]
		then
			break;
		else
			i=$((i+1))
		fi
	done
	IFS=${OLD_IFS};

    # we don't have to be fancy here, we know splash is and extra yaffs fs partition
	mknod /dev/mtdblock${i} b 31 ${i}
	/bin/mount /dev/mtdblock${i} /splash -t yaffs2 -o defaults,ro
	if [ $? -ne 0 ]
	then
		echo -ne "Failed to mount /splash partition\n";
	fi

else
	FILEPATH="/etc/sysconfig"
fi

# hack, debugging
while [ $# -ne 0 ]
do
	if [ $1 == "--verbose" ]
    then
		set -x
        shift;
    elif [ $1 == "--path" ]
    then
        FILEPATH=$2
        shift;shift
    elif [ $1 == "--format" ]
	then
        format=$2
        shift;shift
		CONFIG_OUTPUT_480I=n
		CONFIG_OUTPUT_576I=n
		CONFIG_OUTPUT_720P=n
		CONFIG_OUTPUT_1080I=n
		case ${format} in
			480*) CONFIG_OUTPUT_480I=y
				;;
			576*) CONFIG_OUTPUT_576I=y
				;;
			720*) CONFIG_OUTPUT_720p=y
				;;
			1080*) CONFIG_OUTPUT_1080I=y
				;;
				*) echo "Unrecognized format: $format"
				exit
				;;
		esac
	fi
done

if [ "X${CONFIG_OUTPUT_480I}" == "Xy" ]
then
	# setup expansion port for sd video
	/usr/bin/i2cexp --bus /dev/i2c-0 --dev 0x4e --set 7
	/usr/bin/setADV739x --bus /dev/i2c-3 --dev 0x54 --config 'ntsc_svideo'	
	FORMAT="480i"
elif [ "X${CONFIG_OUTPUT_576I}" == "Xy" ]
then
	# setup expansion port for sd video
	/usr/bin/i2cexp --bus /dev/i2c-0 --dev 0x4e --set 7
	/usr/bin/setADV739x --bus /dev/i2c-3 --dev 0x54 --config 'pal_svideo'
	FORMAT="576i"
elif [ "X${CONFIG_OUTPUT_720P}" == "Xy" ]
then
	/usr/bin/setADV739x --bus /dev/i2c-3 --dev 0x54 --config hd_720p_component
	/usr/bin/i2cexp --bus /dev/i2c-0 --dev 0x4e --clear 8
	/usr/bin/i2cexp --bus /dev/i2c-0 --dev 0x4e --set 9
	FORMAT="720p"
else
	/usr/bin/setAD9889 --bus /dev/i2c-3 --dev 0x7A --format 1080i --input YUV --output RGB --style 2 --656
	FORMAT="1080i"
fi

/usr/bin/splash --format ${FORMAT} \
	--yfile "${FILEPATH}/splash.y" \
	--uvfile "${FILEPATH}/splash.uv" \
	--memoffset ${CONFIG_LINUX_RAM_SIZE}

if [ "X${CONFIG_SPLASH_SCREEN_IN_FS}" == "Xy" ]
then    
	/bin/umount /splash
fi

exit 0
