#!/bin/sh
#
# /etc/init.d/sharedconf
#
SVNVERSION='1.0'
version()
{
	echo "$0 Version $SVNVERSION tim.jaacks@garz-fricke.com"
}

usage()
{
	echo "Usage: $0 {start | list | <setting> [value] }" >&2
	echo "   Call without [value] to read a setting, call with [value] to write it." >&2
	echo "Available settings:" >&2
	echo "   serialdiag  switch serial debug console on or off" >&2
	echo "   dhcp        switch DHCP on or off" >&2
	echo "   ip          set IP address" >&2
	echo "   mask        set subnet mask" >&2
	echo "   gateway     set standard network gateway" >&2
	echo "   mac         set MAC address" >&2
	echo "   name        set device name" >&2
	echo "   serial      set serial number (affects MAC address and device name)" >&2
	echo "   rotation    set display rotation" >&2
	echo "If a 2.Ethernet is present, it may be configured via serial2, mac2, etc." >&2
	exit 1
}

guf_xml_setting()
{
	if [ -z "$2" ]; then
		if [ -e /sys/class/misc/guf_xml/configurationFile/variables/setting_$1/value ]; then
			cat /sys/class/misc/guf_xml/configurationFile/variables/setting_$1/value
		else
			echo
			return 1
		fi
	else
		if [ -e /sys/class/misc/guf_xml/configurationFile/variables/setting_$1/value ]; then
			echo $2 > /sys/class/misc/guf_xml/configurationFile/variables/setting_$1/value
		else
			echo "Setting $1 does not exists"
			return 1
		fi
	fi
	return 0
}

guf_xml_rotation()
{
	if [ -z "$1" ]; then
		cat /sys/class/misc/guf_xml/configurationFile/variables/display/rotation/value
	else
		case "$1" in
			0|90|180|270)
				echo $1 > /sys/class/misc/guf_xml/configurationFile/variables/display/rotation/value
				if [ -x /etc/X11/Xsession.d/25custom_xrandr-settings.sh ]
				then
					/etc/X11/Xsession.d/25custom_xrandr-settings.sh 2>/dev/null
				fi
				;;
			*)
				usage;;
		esac
	fi
}

guf_xml_write_settings()
{
	find /sys/class/misc/guf_xml/configurationFile/variables/ -iname setting* | while read settingpath
	do
		key=""
		linuxpath=""
		value=""
		if [ -r ${settingpath}/key ]; then
			key=$(cat ${settingpath}/key)
		fi
		if [ -r ${settingpath}/linuxpath ]; then
			linuxpath=$(cat ${settingpath}/linuxpath 2>/dev/null)
		fi
		if [ ! -z "$linuxpath" ]; then
			if [ -r ${settingpath}/value ]; then
				value=$(cat ${settingpath}/value)
			fi
			if [ -r ${settingpath}/update ]; then
				update=$(cat ${settingpath}/update)
			fi
			if [ -r ${settingpath}/owner ]; then
				owner=$(cat ${settingpath}/owner)
			fi
			if [ -r ${settingpath}/mode ]; then
				mode=$(cat ${settingpath}/mode)
			fi
			#echo ${settingpath} ${setting} ${key} ${linuxpath} ${update}
            if [[ "$update" != "once" ||  ! -e $linuxpath ]]
            then
				echo "Writing XML setting ${key} to ${linuxpath}"
				mkdir -p `dirname "${linuxpath}"`
				cat ${settingpath}/value | sed -e 's/\([^\\]\)\\n/\1\n/g'  -e 's/\\\\/\\/g' > "${linuxpath}"
				if [[ "$owner" != "" ]]
				then
					chown "$owner" "${linuxpath}"
				fi
				if [[ "$mode" != "" ]]
				then
					chmod "$mode" "${linuxpath}"
				fi
			fi
		fi
	done
}

guf_xml_touchconfig()
{
	echo -n "Configuring touch..."
	# Check if touch configuration data is present
	if [ ! -d /sys/class/misc/guf_xml/configurationFile/variables/touch ]
	then
		echo "no touch configuration found in XML data, skipping configuration"
		return 1
	fi
	# Get touch driver information
	driver_name=`cat /sys/class/misc/guf_xml/configurationFile/variables/touch/driver_info/linux_driver_name/value 2>/dev/null`
	if [ -z "$driver_name" ]
	then
		echo "missing tag 'linux_driver_name' in XML data, skipping configuration"
		return 1
	fi
	if [ ! "$driver_name" = "hidraw" ]
	then
		# Look for driver in sysfs
		for name in `find /sys/class/input/event*/device/name \
						 /sys/class/input/event*/device/device/name 2>/dev/null`; do
			if [ "$driver_name" = "`cat $name`" ]
			then
				driver_sysfs=`dirname $name`
			fi
		done
		if [ -z "$driver_sysfs" ]
		then
			echo "touch driver '$driver_name' not loaded, skipping configuration";
			return 1
		fi
		# Write configuration
		for config in `find /sys/class/misc/guf_xml/configurationFile/variables/touch/settings/* \
						! -iname "regkey" -exec basename {} \; 2>/dev/null`; do
            config_node=$(find -L  "$driver_sysfs/" -maxdepth 3   -name "$config")
			if [[ "$config_node" != "" ]] 
			then
				(cat /sys/class/misc/guf_xml/configurationFile/variables/touch/settings/$config \
					> $driver_sysfs/$config) 2>/dev/null
			else
				echo "touch driver: Config node '$config_node' does not exists, can't apply $config."
			fi
		done
	fi
	# Link touch configuration from XML to /etc/ts.conf
	if [ -e /sys/class/misc/guf_xml/configurationFile/variables/touch/driver_info/linux/ts_conf ]; then
		mount --bind /sys/class/misc/guf_xml/configurationFile/variables/touch/driver_info/linux/ts_conf /etc/ts.conf
	fi
	# Copy touch calibration from XML to /etc/pointercal if it does not exist
	if [ -e /sys/class/misc/guf_xml/configurationFile/variables/touch/driver_info/linux/pointercal ] && [ ! -e /etc/pointercal ]; then
		cat /sys/class/misc/guf_xml/configurationFile/variables/touch/driver_info/linux/pointercal > /etc/pointercal
	fi
	# Run touch calibration if no calibration file exists
	disable_touchcal=`cat /sys/class/misc/guf_xml/configurationFile/variables/setting_disable_touchcal/value 2>/dev/null`
	driver_name=`xconfig getattribute -p /variables/touch -n name`
	if [ ! "$disable_touchcal" = "true" ] && [ ! -e /etc/pointercal ]
	then
		if [ "$driver_name" = "STMPE" ] || [ "$driver_name" = "DA9052" ] || [ "$driver_name" = "UCB1400" ] || [ "$driver_name" = "WM9705" ] ] || [ "$driver_name" = "SX8654" ]
		then
			setsid cttyhack ts_calibrate
			if [ $? -eq 0 ]; then
				sync
			else
				# Clear screen if ts_calibrate was aborted
				cat /dev/zero > /dev/fb0 2> /dev/null
			fi
		fi
	fi

	xserver_touchconfig

	echo "done"
	return 0
}

xconfig_setting()
{
	if [ -z "$2" ]; then
		xconfig getattribute -p /variables/setting[@key=$1] -n value
	else
		xconfig addattribute -y -p /variables/setting[@key=$1] -n value -v $2 >/dev/null
	fi
}

xconfig_rotation()
{
	if [ -z "$1" ]; then
		xconfig getattribute -p /variables/display/rotation -n value
	else
		case "$1" in
			0|90|180|270)
				xconfig addattribute -y -p /variables/display/rotation -n value -v $1 >/dev/null
				if [ -x /etc/X11/Xsession.d/25custom_xrandr-settings.sh ]
				then
					/etc/X11/Xsession.d/25custom_xrandr-settings.sh 2>/dev/null
				fi
				;;
			*)
				usage;;
		esac
	fi
}


xconfig_update_settings()
{
	echo -e "$(xconfig list -l  -v --node "setting" --attribute linuxpath -p /variables)" "\n/variables" | while read line; 
	do 
		if [[ "$line" == "/variables"* ]]; then
			if [ ! -z $path ]; then
				if [ -z $key ]; then echo "Failed to $1 setting, key is missing"; fi
				if [ -z $linuxpath ]; then echo "Failed to $1 setting, linuxpath is missing"; fi

				if [ "$1" == "write" ]; 
				then
					if [[ "$update" != "once" ||  ! -e $linuxpath ]]
					then
						echo "Writing XML setting ${key} to ${linuxpath}"
						mkdir -p `dirname "${linuxpath}"`
						# Write data to file, remove trailing whitespace 	
						xconfig getattribute -p $path[@key=\'$key\'] -n value | sed -e 's/\s*$//'   > "${linuxpath}"
						if [[ "$owner" != "" ]]
						then
							chown "$owner" "${linuxpath}"
						fi
						if [[ "$mode" != "" ]]
						then
							chmod "$mode" "${linuxpath}"
						fi
					fi
				else
					if [[ -e $linuxpath  && ! -z $update ]]
					then
						md5xml=$(xconfig getattribute -p $path[@key=\'$key\'] -n value | sed -e 's/\s*$//' | md5sum | awk '{print $1}' )
						md5file=$(md5sum "$linuxpath" | awk '{print $1}' )
						if [[ "$md5xml" != "$md5file" ]]
						then
							echo "$linuxpath has changed: $md5file $md5xml"
							echo "Writing XML setting ${key} from ${linuxpath} to xml"
							xconfig addattribute -y -p $path[@key=\'$key\'] -n value -v "$(cat "$linuxpath" )"
						fi
					fi
				fi
			fi
			path="$line"
			key=""; linuxpath=""; update=""; owner=""; mode="";
		fi
		if [[ "$line" == "key"* ]]; then
			key=${line#key=}; key=${key#\"}; key=${key%\"}
		fi
		if [[ "$line" == "linuxpath"* ]]; then
			linuxpath=${line#linuxpath=}; linuxpath=${linuxpath#\"}; linuxpath=${linuxpath%\"}
		fi
		if [[ "$line" == "update"* ]]; then
			update=${line#update=}; update=${update#\"}; update=${update%\"}
		fi
		if [[ "$line" == "owner"* ]]; then
			owner=${line#owner=}; owner=${owner#\"}; owner=${owner%\"}
		fi
		if [[ "$line" == "mode"* ]]; then
			mode=${line#mode=}; mode=${mode#\"}; mode=${mode%\"}
		fi
	done
}

xconfig_store_settings()
{
	xconfig_update_settings store
}
xconfig_write_settings()
{
	xconfig_update_settings write
}

xserver_touchconfig()
{
	# Only do this if we have an Xserver
	if [ ! -e /etc/X11/xorg.conf ]
	then
		return 0
	fi

	echo "for X"
	xorgconf=/etc/X11/xorg.conf.d/10_sharedconf.conf
	mkdir -p /etc/X11/xorg.conf.d

	t="\n"
	t=$t"# ---------------------------------------------------------\n"
	t=$t"# This file is generated by the shared config script\n"
	t=$t"# It will be overwritten on next boot\n"
	t=$t"# ---------------------------------------------------------\n"
	t=$t"\n"

	disable_touchcal=`xconfig getattribute -p /variables/setting[@key=disable_touchcal] -n value`
	driver_name=`xconfig getattribute -p /variables/touch -n name`

	if [ "$driver_name" = "STMPE" ] || [ "$driver_name" = "DA9052" ] || [ "$driver_name" = "UCB1400" ] || [ "$driver_name" = "WM9705" ] || [ "$driver_name" = "SX8654" ]
	then
		# Add tslib as input driver for X
		# Workaround until the filtering and calibration in X works
		t=$t"\n"
		t=$t"Section \"InputDevice\"\n"
		t=$t"       Identifier \"tslib\"\n"
		t=$t"       Driver \"tslib\"\n"
		t=$t"       Option \"Device\" \"/dev/input/touchscreen0\"\n"
		t=$t"       Option \"CorePointer\"\n"
		t=$t"EndSection\n"
		t=$t"\n"
		t=$t"Section \"InputClass\"\n"
		t=$t"        Identifier \"disable non tslib access\"\n"
		t=$t"        Driver \"evdev\"\n"
		t=$t"        MatchIsTouchscreen \"on\"\n"
		t=$t"        Option \"Ignore\" \"on\"\n"
		t=$t"EndSection\n"
		t=$t"\n"
	fi

	if [ "$driver_name" = "EETI USB" ]
	then
		t=$t"Section \"InputClass\"\n"
		t=$t"        Identifier \"Only one egalax device\"\n"
		t=$t"        MatchProduct \"eGalaxTouch Virtual Device for Single\"\n"
		t=$t"        Option \"Ignore\" \"on\"\n"
		t=$t"EndSection\n"
		t=$t"\n"

		#only in the 3.10.x kernel those devices exist
		if uname -a  | grep -q  3.10.
		then
			t=$t"Section \"InputClass\"\n"
			t=$t"        Identifier \"Only one egalax device\"\n"
			t=$t"        MatchProduct \"eGalaxTouch Virtual Device for Multi\"\n"
			t=$t"        Option \"Ignore\" \"on\"\n"
			t=$t"EndSection\n"
			t=$t"\n"
			t=$t"Section \"InputClass\"\n"
			t=$t"        Identifier \"Only one egalax device\"\n"
			t=$t"        MatchProduct \"eGalax Inc. eGalaxTouch EXC7200-7680v1.000           Pen\"\n"
			t=$t"        Option \"Ignore\" \"on\"\n"
			t=$t"EndSection\n"
			t=$t"\n"
		fi
	fi

	# RedBoot-based systems do not have GPU support and need special rotation handling
	if [ "$CONFIG_TYPE" = "guf_xml" ]
	then
		ROTATION=$($ROTATION_FUNCTION)
		case "${ROTATION}" in
			90)  ORIENTATION="CCW" ;;
			180) ORIENTATION="UD"  ;;
			270) ORIENTATION="CW"  ;;
			*)   ORIENTATION="off" ;;
		esac
		t=$t"Section \"Device\"\n"
		t=$t"        Identifier  \"Framebuffer Device\"\n"
		t=$t"        Driver      \"fbdev\"\n"
		t=$t"        Option      \"Rotate\""	"\"$ORIENTATION\"\n"
		t=$t"EndSection\n"
		t=$t"\n"
	fi
	echo -e "$t" > $xorgconf
}

xconfig_touchconfig()
{
	echo -n "Configuring touch..."
	# Check if touch configuration data is present
	xconfig list -p /configurationFile/variables/touch >/dev/null
	if [ $? -ne 0 ]
	then
		echo "no touch configuration found in XML data, skipping configuration"
		return 1
	fi
	# Get touch driver information
	driver_name=`xconfig getattribute -p /variables/touch/driver_info/linux_driver_name -n value`
	if [ -z "$driver_name" ]
	then
		echo "missing tag 'linux_driver_name' in XML data, skipping configuration"
		return 1
	fi
	if [ ! "$driver_name" = "hidraw" ]
	then
		# Look for driver in sysfs
		for name in `find /sys/class/input/event*/device/name \
						 /sys/class/input/event*/device/device/name 2>/dev/null`; do
			if [ "$driver_name" = "`cat $name`" ]
			then
				driver_sysfs=`dirname $name`
			fi
		done
		if [ -z "$driver_sysfs" ]
		then
			echo "touch driver '$driver_name' not loaded, skipping configuration";
			return 1
		fi
		# Write configuration
		for config in `xconfig listattributes -p /variables/touch/settings | grep -v regKey`; do
            config_node=$(find -L  "$driver_sysfs/" -maxdepth 3   -name "$config" | head -n 1 )
			if [[ "$config_node" != "" ]] 
			then
		        ( xconfig getattribute -p /variables/touch/settings -n $config > $config_node) 2>/dev/null
			else
				echo "touch driver: Config node '$config_node' does not exists, can't apply $config."
			fi
		done
	fi
	# Link touch configuration from config partition to /etc/ts.conf
	if [ -e /etc/shared/ts.conf ]; then
		mount --bind /etc/shared/ts.conf /etc/ts.conf
	fi
	# Copy touch calibration from config partition to /etc/pointercal if it does not exist
	if [ -e /etc/shared/pointercal ] && [ ! -e /etc/pointercal ]; then
		cp /etc/shared/pointercal /etc/pointercal
	fi
	# Run touch calibration if no calibration file exists
	disable_touchcal=`xconfig getattribute -p /variables/setting[@key=disable_touchcal] -n value`
	driver_name=`xconfig getattribute -p /variables/touch -n name`
	if [ ! "$disable_touchcal" = "true" ] && [ ! -e /etc/pointercal ]
	then
		if [ "$driver_name" = "STMPE" ] || [ "$driver_name" = "DA9052" ] || [ "$driver_name" = "UCB1400" ] || [ "$driver_name" = "WM9705" ] || [ "$driver_name" = "SX8654" ]
		then
			setsid cttyhack ts_calibrate
			if [ $? -eq 0 ]; then
				sync
			else
				# Clear screen if ts_calibrate was aborted
				cat /dev/zero > /dev/fb0 2> /dev/null
			fi
		fi
	fi

	xserver_touchconfig

	echo "done"
	return 0
}

bool_setting()
{
	if [ -z "$2" ]; then
		SETTING=$($SETTING_FUNCTION $1)
		if [ -z "$SETTING" ]; then
			return 1
		fi
		if [ $SETTING == "true" ]; then
			echo "on"
		else
			echo "off"
		fi
	else
		case "$2" in
			on)
				$SETTING_FUNCTION $1 true;;
			off)
				$SETTING_FUNCTION $1 false;;
			*)
				usage;;
		esac
	fi
}

serial_number()
{
	if [ -z "$2" ]; then
		mac=$($SETTING_FUNCTION ${1}_esa_data | tr : ' ')
		if [ -z "$mac" ]; then
			return 1
		fi
		byte3=$(printf "%d" $(echo $mac | awk '{print $4}'))
		byte2=$(printf "%d" $(echo $mac | awk '{print $5}'))
		byte1=$(printf "%d" $(echo $mac | awk '{print $6}'))
		printf "%08d\n" $(((byte3 * 65536) + (byte2 * 256) + byte1))
	else
		# Remove leading zeros to work around number being interpreted as octal
		number=$(expr $2 + 0)
		devicename=$(printf "GFMM%08d" $number)
		echo $devicename
		hostname_config $devicename
		byte3=$(printf "0x%x" $((number / 65536)))
		byte2=$(printf "0x%x" $(((number % 65536) / 256)))
		byte1=$(printf "0x%x" $(((number % 65536) % 256)))
		mac=`printf "0x00:0x07:0x8e:0x%02x:0x%02x:0x%02x" $byte3 $byte2 $byte1 | tr [abcdef] [ABCDEF]`
		echo $mac
		ifconfig $1 down
		ifconfig $1 hw ether $(echo $mac | sed 's/0x//g')
		ifconfig $1 up
		$SETTING_FUNCTION ${1}_esa_data $mac || return 1
	fi
}

hostname_config()
{
	$SETTING_FUNCTION	eth0_name $1

	if [ -z "$1" ]; then
		# Set hostname
		HOSTNAME=$($SETTING_FUNCTION eth0_name)
		hostname $HOSTNAME
	fi
}

generate_network_interfaces()
{
	interfaces_conf=/etc/network/interfaces
	echo "Generating $interfaces_conf"

	mac2=$($SETTING_FUNCTION eth1_esa_data)

	BOOTP=$($SETTING_FUNCTION bootp || echo "true")
	IP=$($SETTING_FUNCTION bootp_my_ip)
	MASK=$($SETTING_FUNCTION bootp_my_ip_mask)
	GATEWAY=$($SETTING_FUNCTION bootp_my_gateway_ip)

	BOOTP2=$($SETTING_FUNCTION bootp1 || echo "true")
	IP2=$($SETTING_FUNCTION bootp_my_ip1)
	MASK2=$($SETTING_FUNCTION bootp_my_ip_mask1)
	GATEWAY2=$($SETTING_FUNCTION bootp_my_gateway_ip1)

	/bin/mkdir -p /etc/network

	t="# This file was auto-generated by /etc/init.d/sharedconf.\n"
	t=$t"# Please do not modify it by hand, it will be overwritten\n"
	t=$t"# on the next boot. If you want to change the network\n"
	t=$t"# configuration, use the sconfig command to do this.\n"
	t=$t"\n"

	t=$t"auto lo\n"
	t=$t"iface lo inet loopback\n"
	t=$t"\n"

	t=$t"auto eth0\n"
	if [ $BOOTP = true ]; then
		t=$t"iface eth0 inet dhcp\n"
	else
		t=$t"iface eth0 inet static\n"
		t=$t"	address $IP\n"
		t=$t"	netmask $MASK\n"
		t=$t"	gateway $GATEWAY\n"
	fi
	t=$t"	up /etc/init.d/ntpd start\n"
	t=$t"\n"

	if [ -z "$mac2" ]; then
		t=$t"allow-hotplug eth1\n"
		t=$t"auto eth1\n"
		t=$t"iface eth1 inet dhcp\n"
	else		
		ifconfig eth1 hw ether $(echo $mac2 | sed 's/0x//g')
		t=$t"auto eth1\n"
		if [ $BOOTP2 = true ]; then
			t=$t"iface eth1 inet dhcp\n"
		else
			t=$t"iface eth1 inet static\n"
			t=$t"	address $IP2\n"
			t=$t"	netmask $MASK2\n"
			t=$t"	gateway $GATEWAY2\n"
		fi
	fi		
	t=$t"	up /etc/init.d/ntpd start\n"

	t=$t"\n"
	if [ -f /usr/sbin/wpa_supplicant ]; then
		t=$t"\n"
		t=$t"allow-hotplug wlan0\n"
		t=$t"auto wlan0\n"
		t=$t"iface wlan0 inet dhcp\n"
		t=$t"	pre-up /usr/sbin/wpa_supplicant -B -iwlan0 -c/etc/wpa_supplicant.conf\n"
		t=$t"	post-down killall wpa_supplicant\n"
		t=$t"	up /etc/init.d/ntpd start\n"
		t=$t"\n"
	fi

	echo -e "$t" > $interfaces_conf

	# Set hostname
	HOSTNAME=$($SETTING_FUNCTION eth0_name)
	hostname $HOSTNAME
}

get_config_type()
{
	# Check if guf_xml driver is present
	if [ -d /sys/class/misc/guf_xml ]; then
		echo "guf_xml"
		return 0
	fi
	# Check if we already have a mounted config partition
	bootpart=$(basename $(mount | grep /etc/shared | awk '{ print $1 }') 2>/dev/null)
	if [ ! -z "$bootpart" ]; then
		echo "$bootpart"
		return 0
	fi
	# Check if there is an eMMC boot partition with a FAT
	# filesystem labeled "config"
	mkdir -p /etc/shared
	for bootpart_sys in `find /sys/class/mmc_host/mmc*/*/block/mmcblk*/mmcblk*boot? -maxdepth 0`; do
		bootpart=$(basename $bootpart_sys)
		# We are called from /etc/init.d/rcS in the first place, so udev has
		# not created the device nodes, yet. If it does not exist, create it.
		if [ ! -e /dev/$bootpart ]; then
			major=$(awk -F : '{print $1}' $bootpart_sys/dev)
			minor=$(awk -F : '{print $2}' $bootpart_sys/dev)
			mknod -m 660 /dev/$bootpart b $major $minor
		fi
		bootpart_label=`(dosfslabel /dev/$bootpart 2> /dev/null || echo "(none)") | tr [:upper:] [:lower:]`
		if [ ! -z $bootpart_label ] && [ $bootpart_label == "config" ]; then
			echo 0 > /sys/block/$bootpart/force_ro
			mount /dev/$bootpart /etc/shared -t vfat 2>/dev/null
			echo "$bootpart"
			return 0
		fi
	done
	return 1
}

list()
{
	echo "   serialdiag  $(bool_setting enable_serialdiag 2>/dev/null || echo N/A)"
	echo "   dhcp        $(bool_setting bootp 2>/dev/null || echo N/A)"
	echo "   ip          $($SETTING_FUNCTION bootp_my_ip 2>/dev/null || echo N/A)"
	echo "   mask        $($SETTING_FUNCTION bootp_my_ip_mask 2>/dev/null || echo N/A)"
	echo "   gateway     $($SETTING_FUNCTION bootp_my_gateway_ip 2>/dev/null || echo N/A)"
	echo "   mac         $($SETTING_FUNCTION eth0_esa_data 2>/dev/null || echo N/A)"
	echo "   name        $($SETTING_FUNCTION eth0_name 2>/dev/null || echo N/A)"
	echo "   serial      $(serial_number eth0 2>/dev/null || echo N/A)"

	MAC2=$($SETTING_FUNCTION eth1_esa_data)
	if [ ! -z "$MAC2" ]; then
		echo ""
		echo "   dhcp2       $(bool_setting bootp1 2>/dev/null || echo N/A)"
		echo "   ip2         $($SETTING_FUNCTION bootp_my_ip1 2>/dev/null || echo N/A)"
		echo "   mask2       $($SETTING_FUNCTION bootp_my_ip_mask1 2>/dev/null || echo N/A)"
		echo "   gateway2    $($SETTING_FUNCTION bootp_my_gateway_ip1 2>/dev/null || echo N/A)"
		echo "   mac2        $($SETTING_FUNCTION eth1_esa_data 2>/dev/null || echo N/A)"
		echo "   serial2     $(serial_number eth1 2>/dev/null || echo N/A)"
	fi

		echo ""
	echo "   rotation    $($ROTATION_FUNCTION 2>/dev/null || echo N/A)"
}


CONFIG_TYPE=$(get_config_type)

case $CONFIG_TYPE in
	guf_xml)
		SETTING_FUNCTION=guf_xml_setting
		ROTATION_FUNCTION=guf_xml_rotation
		WRITE_SETTINGS_FUNCTION=guf_xml_write_settings
		TOUCHCONFIG_FUNCTION=guf_xml_touchconfig
		HELLO="Getting shared configuration...using guf_xml data"
		;;
	mmcblk*)
		SETTING_FUNCTION=xconfig_setting
		ROTATION_FUNCTION=xconfig_rotation
		WRITE_SETTINGS_FUNCTION=xconfig_write_settings
		TOUCHCONFIG_FUNCTION=xconfig_touchconfig
		HELLO="Getting shared configuration...using FAT partition $CONFIG_TYPE"
		;;
	*)
		echo "No shared configuration found" >&2
		exit 1
		;;
esac

case "$1" in
	start|restart|force-reload)
		echo $HELLO
		generate_network_interfaces
		$WRITE_SETTINGS_FUNCTION
		$TOUCHCONFIG_FUNCTION
		sync
		# Always return successful on init
		true
		;;

	stop)
		xconfig_store_settings
		sync
		;;

	list)
		list
		;;

	--version)
		version
		;;




	serialdiag)		bool_setting			enable_serialdiag		$2;;
	dhcp)			bool_setting			bootp					$2;;
	ip)				$SETTING_FUNCTION		bootp_my_ip				$2;;
	mask)			$SETTING_FUNCTION		bootp_my_ip_mask		$2;;
	gateway)		$SETTING_FUNCTION		bootp_my_gateway_ip		$2;;
	mac)			$SETTING_FUNCTION		eth0_esa_data			$2;;
	name)			hostname				                        $2;;
	hostname)		hostname				                        $2;;
	serial)			serial_number			eth0					$2;;
	dhcp2)			bool_setting			bootp1					$2;;
	ip2)			$SETTING_FUNCTION		bootp_my_ip1			$2;;
	mask2)			$SETTING_FUNCTION		bootp_my_ip_mask1		$2;;
	gateway2)		$SETTING_FUNCTION		bootp_my_gateway_ip1	$2;;
	mac2)			$SETTING_FUNCTION		eth1_esa_data			$2;;
	serial2)		serial_number			eth1					$2;;
	rotation)		$ROTATION_FUNCTION								$2;;

	*)
		usage;;
esac

exit $?
