#!/bin/sh
# Change system graphics based on display resolution.
#

RESOLUTION_FILE="/sys/class/graphics/fb0/mode"
QLX_BOOT_DIR="/home/qlx/boot"
NODE_PUBLIC_DIR="/var/node/public"
BOOT_7="boot_7inch"
BOOT_12="boot_12inch"

status_grep_1280x800=99
status_grep_800x480=99
catch_all=""

case "$1" in
  start)
    echo "Starting Graphics Settings..."

    if [ ! -d "${QLX_BOOT_DIR}/${BOOT_12}" ]
    then
        echo " Alternate Graphics Directories Not Available "
        exit 0
    fi

    # Check screen resolution at startup.
    # 7 inch grpahic files are the default.
    # If 12 inch display is detected,
    # Create links to 12 inch graphic files for default use.
    catch_all=$(/bin/grep "1280x800" ${RESOLUTION_FILE})
    status_grep_1280x800=$?

    if [ $status_grep_1280x800 -eq 0 ]
    then
        # Resolution = 1280x800
    	if [ ! -e "${QLX_BOOT_DIR}/12" ]
    	then
        	echo ""
        	echo "SCREEN RESOLUTION = 1280 X 800"
        	echo "12 Inch Display"
        	echo ${catch_all}
        	echo "Using 12 inch Graphics Files"
        	echo ""
        	echo "Creating 12 inch graphic links......"
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_12}/12in_Collision_SplashScreen_1.png ${QLX_BOOT_DIR}/boot20.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_12}/12in_Collision_SplashScreen_2.png ${QLX_BOOT_DIR}/boot30.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_12}/12in_Collision_SplashScreen_3.png ${QLX_BOOT_DIR}/boot40.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_12}/12in_Collision_SplashScreen_4.png ${QLX_BOOT_DIR}/boot50.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_12}/12in_Collision_SplashScreen_5.png ${QLX_BOOT_DIR}/boot60.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_12}/12in_Collision_SplashScreen_6.png ${QLX_BOOT_DIR}/boot70.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_12}/12in_Collision_SplashScreen_7.png ${QLX_BOOT_DIR}/boot80.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_12}/12in_Collision_SplashScreen_Final.png ${QLX_BOOT_DIR}/../bin/logo.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_12}/12in_Version_Fail.png ${QLX_BOOT_DIR}/Version_Fail.png
        	echo "12" > "${QLX_BOOT_DIR}/12"
        	/bin/rm -fv "${QLX_BOOT_DIR}/07"
        	/bin/rmm -fv "/etc/pointercal"
        	/bin/sync
	else
        	echo ""
        	echo "SCREEN RESOLUTION = 1280 X 800"
        	echo "12 Inch Display"
        	echo ${catch_all}
        	echo "Using 12 inch Graphics Files"
        	echo ""
        	echo "Graphic links already created......"
	fi
	
        if [ ! -e "${NODE_PUBLIC_DIR}/12" ]
        then
		# Remove existing files and links for a clean slate
		rm -rf ${NODE_PUBLIC_DIR}/images/*
		rm -rf ${NODE_PUBLIC_DIR}/stylesheets/*
		/bin/sync
		echo ""
		echo "Creating 12 inch node link......"
		cd ${NODE_PUBLIC_DIR}/images
		ln -fsv ${NODE_PUBLIC_DIR}/images12/* .
		/bin/sync
		cd ${NODE_PUBLIC_DIR}/stylesheets
		ln -fsv ${NODE_PUBLIC_DIR}/stylesheets12/* .
		/bin/sync
		echo "12" > "${NODE_PUBLIC_DIR}/12"
		/bin/rm -fv "${NODE_PUBLIC_DIR}/07"
		/bin/sync
		echo "Node 12 inch link created......"
		echo ""
        else
		ls -l ${NODE_PUBLIC_DIR}
		echo "Node 12 inch link already created......"
		echo ""
        fi
        
        echo ""
        echo "Creating 12 inch qlxuihost link......"
        ln -fsv /home/qlx/bin/qlxuihost12in /home/qlx/bin/qlxuihost
        ls -l /home/qlx/bin/qlxuihost
        echo "Qlxuihost 12 inch link created......"
        echo ""
        /bin/sync
    else
	# Resolution = 800x480
    	catch_all=$(/bin/grep "800x480" ${RESOLUTION_FILE})
    	status_grep_800x480=$?
	if [ ! -e "${QLX_BOOT_DIR}/07" ]
    	then
        	echo ""
        	echo "SCREEN RESOLUTION = 800 X 480"
        	echo "7 Inch Display"
        	echo ${catch_all}
        	echo "Using 7 inch Graphics Files"
        	echo ""
        	echo "Creating 7 inch graphic links......"
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_7}/boot20.png ${QLX_BOOT_DIR}/boot20.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_7}/boot30.png ${QLX_BOOT_DIR}/boot30.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_7}/boot40.png ${QLX_BOOT_DIR}/boot40.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_7}/boot50.png ${QLX_BOOT_DIR}/boot50.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_7}/boot60.png ${QLX_BOOT_DIR}/boot60.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_7}/boot70.png ${QLX_BOOT_DIR}/boot70.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_7}/boot80.png ${QLX_BOOT_DIR}/boot80.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_7}/logo.png ${QLX_BOOT_DIR}/../bin/logo.png
        	ln -fsv ${QLX_BOOT_DIR}/${BOOT_7}/Version_Fail.png ${QLX_BOOT_DIR}/Version_Fail.png
        	echo "07" > "${QLX_BOOT_DIR}/07"
		/bin/rm -fv "${QLX_BOOT_DIR}/12"
        	/bin/rm -fv "/etc/pointercal"
        	/bin/sync
	else
        	echo ""
        	echo "SCREEN RESOLUTION = 800 X 480"
        	echo "7 Inch Display"
        	echo ${catch_all}
        	echo "Using 7 inch Graphics Files"
        	echo ""
        	echo "Graphic links already created......"
	fi
	
        if [ ! -e "${NODE_PUBLIC_DIR}/07" ]
        then
		# Remove existing files and links for a clean slate
		rm -rf ${NODE_PUBLIC_DIR}/images/*
		rm -rf ${NODE_PUBLIC_DIR}/stylesheets/*
		/bin/sync
		echo ""
		echo "Creating 7 inch node link......"
		cd ${NODE_PUBLIC_DIR}/images
		ln -fsv ${NODE_PUBLIC_DIR}/images7/* .
		/bin/sync
		cd ${NODE_PUBLIC_DIR}/stylesheets
		ln -fsv ${NODE_PUBLIC_DIR}/stylesheets7/* .
		/bin/sync
		echo "07" > "${NODE_PUBLIC_DIR}/07"
		/bin/rm -fv "${NODE_PUBLIC_DIR}/12"
		/bin/sync
		echo "Node 7 inch link created......"
		echo ""
        else
		ls -l ${NODE_PUBLIC_DIR}
		echo "Node 7 inch link already created......"
		echo ""
        fi
        
        echo ""
        echo "Creating 7 inch qlxuihost link......"
        ln -fsv /home/qlx/bin/qlxuihost7in /home/qlx/bin/qlxuihost
        ls -l /home/qlx/bin/qlxuihost
        echo "Qlxuihost 7 inch link created......"
        echo ""
        /bin/sync
    fi
    # Make sure node forever log directory is empty
    rm -rf ${NODE_PUBLIC_DIR}/../.forever
    /bin/sync
    /bin/cat ${QLX_BOOT_DIR}/[0-9][0-9]
	echo ""
	;;
  restart|reload)
	"$0" start
	;;
  *)
	echo "Usage: $0 {start|restart}"
	exit 1
esac

exit $?

