#!/bin/sh
#
# Read 1 character from /dev/lpicp to get the h/w revision: we KNOW we're running
# on a D22. Under normal circumstances I'd use read -n1 but sadly the BusyBox
# implementation seems to be broken so I do it this way: copy 1 block of 1 byte
# from the lpicp driver to a temp file, then read that to get the h/w revision.
# This should be either A, or B.
#
dd if=/dev/lpicp of=/tmp/rev bs=1 count=1 >/dev/null 2>&1
HWREV=$(cat /tmp/rev)
rm /tmp/rev
case "$1" in
    start)
    ln -f -s /root /home/root
	echo -e '\033[9;0]' > /dev/fb0
	cd /root
	if [ $HWREV == "B" ]; then
		echo "Setting up ME910 power supply pins as inactive"
#
# REVB make bloody sure that ME910 modem is powered down by activating the
# HW_SHUTDOWN pin, presetting the GPRS_ON_OFF to a known state, and turning
# off the 3.8V power supply.
#
# PWR_3V8 -> 0
		echo 119 > /sys/class/gpio/export
		echo out > /sys/class/gpio/gpio119/direction
		echo 0 > /sys/class/gpio/gpio119/value
# /HW_SHUTDOWN -> 1 (goes through an inverter)
		echo 117 > /sys/class/gpio/export
		echo out > /sys/class/gpio/gpio117/direction
		echo 1 > /sys/class/gpio/gpio117/value
# /ON_OFF -> 0 (also through an inverter)
		echo 116 > /sys/class/gpio/export
		echo out > /sys/class/gpio/gpio116/direction
		echo 0 > /sys/class/gpio/gpio116/value
		sleep 1
# If the modem is still alive after this, we have zombie worries ...
	fi
#
#-In case there's an external 4G modem connected, make sure _it's_ well and
# truly shut off too.
#
# GPRS_4G_POWER -> 0
	echo 85 > /sys/class/gpio/export
	echo out > /sys/class/gpio/gpio85/direction
	echo 0 > /sys/class/gpio/gpio85/value
	
# Add TELIT GE910, ME910 to USB UARTs handled by PL2303 driver ...	
	echo 1bc7 1101 > /sys/bus/usb-serial/drivers/pl2303/new_id
	echo 1bc7 110a > /sys/bus/usb-serial/drivers/pl2303/new_id
	
	export TOKEN_PATH='/etc/vnc/vnc.access'
	export QWS_SIZE='640x272'
	export QWS_DEPTH='32'
	export QWS_VNC_SERVER='libvnc'
	export QWS_KEYBOARD='remotekbdhandler:/dev/null'
	export QWS_MOUSE_PROTO='remotemouhandler:/dev/null'
	if test -f /media/usbdisk/startup.sh
		then
		chmod 777 /media/usbdisk/startup.sh
		cd /media/usbdisk
		. startup.sh
		cd /root
	fi
	mkdir -p /media/mmc/SYNC/UP
	mkdir -p /media/mmc/SYNC/DOWN
	mkdir -p /root/RDX
	./BIN/R-Loader -qws -display "multi: LinuxFb: VNC::size=480x272" &
	;;
esac
exit 0
