#!/bin/sh
#
# Toshiba_elotone4sle       This shell script takes care of starting and stopping \
#               the Toshiba_elotone4sle program.
#
# chkconfig: 2345 85 15
# description: The Toshiba_elotone4sle program works with the elo touch screen.
#             
### BEGIN INIT INFO
# Provides:          Toshiba_elotone4sle
# Required-Start:	hotplug boot.loadmodules	  
# X-UnitedLinux-Should-Start: setup232 cron
# Required-Stop: 
# X-UnitedLinux-Should-Stop:
# Default-Start:     B 0 6
# Default-Stop:      1 2 
# Short-Description: provides sounds to elo touch panel
# Description:      
### END INIT INFO

. /etc/rc.status
rc_reset

USB=`/usr/sbin/lsusb | grep -c 04e7:0030`
TOSHIBASERIAL=`/sbin/lspci -n | grep -c 0700`
MONITOR_YAMI_ANAK=`/usr/sbin/lsusb | grep 04b3:4675`
if [ -e /sys/class/dmi/id/product_name ]; then
ModelType=`/bin/cat /sys/class/dmi/id/product_name`
#echo "Modeltype = " $ModelType
if [ -z $ModelType ]; then
ModelType=""
fi
else
ModelType=""
fi
#SurePOS 500
#defect 11184
if [ $ModelType = "4846545" ] \
|| [ $ModelType = "4846565" ]; then
SERIALELO0="T"; PORT0="ttyS4"
fi

if [ $ModelType = "4835153" ] \
|| [ $ModelType = "4840563" ] \
|| [ $ModelType = "4840533" ] \
|| [ $ModelType = "4840543" ] \
|| [ $ModelType = "4840553" ]; then
SERIALELO0="T"; PORT0="ttyS4"
fi

if [ $ModelType = "4840573" ]; then
SERIALELO0="T"; PORT0="ttyS4"
fi

if [ $ModelType = "4836132" ] \
|| [ $ModelType = "4836135" ]; then
SERIALELO0="T"; PORT0="ttyS1"
fi

if [ $ModelType = "4838132" ] \
|| [ $ModelType = "4800783" ]; then
SERIALELO0="T"; PORT0="ttyS0"
fi


if [ $USB != "0" ]; then
PORT=usb
if [ -e /proc/bus/usb/devices ]; then
USBMOUNT="T"
else
mount -t usbfs usbfs /proc/bus/usb
fi
elif [ "$SERIALELO0" = "T" ]; then
PORT=/dev/$PORT0
else
echo "Cannot determine serial port."
echo "Set PORT manually in /etc/init.d/Toshiba_elotone4sle"
rc_failed 1
fi
#PORT=/dev/ttyS0


RETVAL=0
start () {
    echo -n "Starting Toshiba_elotone4sle on $PORT: "
    if [ "$PORT" != "" ]; then

        # set beep using Toshiba_elotone4sle to cater to normal crystal
        # monitors. Thenafter, if a Yami or Anak Monitor is detected, 
        # use the eloconfig utility to configure the beep settings

        /usr/sbin/Toshiba_elotone4sle -s DOWN -d $PORT -f 1000 -t 50
    
    	KERNEL_DEFAULT=`uname -r | grep default`
    	
    	if [ "$KERNEL_DEFAULT" ]; then
    	    ELOFOUSB_KMP=`rpm -qa | grep elofousb-kmp-default | wc -l`
    	else
    	    ELOFOUSB_KMP=`rpm -qa | grep elofousb-kmp-pae | wc -l`
    	fi 

    	ELO_CONFIG_LOG="/var/log/Toshiba_elotone4sle.log"

    	ELO_CONFIG_SH_DETECTED=true
    	ELOFOUSB_KMP_DETECTED=true
    	ELO_CONFIG_ERROR=false
	
	   # check if Yami or Anak monitor connected

        if [ "$MONITOR_YAMI_ANAK" ]; then

            # check if elofousb kmp is installed
            # 
            if (( "$ELOFOUSB_KMP" > 0  )); then

        	    ELO_CONFIG_FILE="/opt/toshiba/elotone4sle/elotone4sle.conf"
        	    ELO_CONFIG_SH="/opt/toshiba/elotone4sle/EloConfig.sh"
        	    ELO_CONFIG_BIN="/usr/bin/eloconfig"

        	    # check if EloConfig.sh and eloconfig binary exists
        	
        	    if [ -f $ELO_CONFIG_SH ] && [ -f $ELO_CONFIG_BIN  ]; then
        	        
                    if [ -f $ELO_CONFIG_FILE ]; then
            		    BEEP_MODE=`grep "^BeepMode" $ELO_CONFIG_FILE | awk -F "=" '{print $2}'`
             		    BEEP_FREQ=`grep "^BeepFreq" $ELO_CONFIG_FILE | awk -F "=" '{print $2}'`
            		    BEEP_TIME=`grep "^BeepTime" $ELO_CONFIG_FILE | awk -F "=" '{print $2}'`
            		    UIT=`grep "^UIT" $ELO_CONFIG_FILE | awk -F "=" '{print $2}'`
            		    TONE_MODE=`grep "^ToneMode" $ELO_CONFIG_FILE | awk -F "=" '{print $2}'`

            		    # read config file for values and then set the config as necessary
            	    
            		    rmmod elousb
            		    $ELO_CONFIG_SH -select=all -set_beep_mode=$BEEP_MODE > /dev/null 2>&1
              	        $ELO_CONFIG_SH -select=all -set_beep_frequency=$BEEP_FREQ > /dev/null 2>&1
            		    $ELO_CONFIG_SH -select=all -set_beep_duration=$BEEP_TIME > /dev/null 2>&1
            	    	$ELO_CONFIG_SH -select=all -set_uit_level=$UIT > /dev/null 2>&1
            	    	$ELO_CONFIG_SH -select=all -set_tone_mode=$TONE_MODE > /dev/null 2>&1
            		    modprobe elousb
            		else
            		
            		    # no config file found. set default values

            		    rmmod elousb
            		    $ELO_CONFIG_SH -select=all -set_beep_mode=ENTRY > /dev/null 2>&1
              	        $ELO_CONFIG_SH -select=all -set_beep_frequency=1000 > /dev/null 2>&1
            		    $ELO_CONFIG_SH -select=all -set_beep_duration=50 > /dev/null 2>&1
            	    	$ELO_CONFIG_SH -select=all -set_uit_level=DISABLED > /dev/null 2>&1
            	    	$ELO_CONFIG_SH -select=all -set_tone_mode=NOTONE > /dev/null 2>&1
            		    modprobe elousb
            		fi
        	    else

        		ELO_CONFIG_SH_DETECTED=false
        		ELO_CONFIG_ERROR=true
        	    
        	    fi

        	else
        	
        		ELOFOUSB_KMP_DETECTED=false
        		ELO_CONFIG_ERROR=true
            fi

        fi


	# write errors to log file if errors are detected
	
	if $ELO_CONFIG_ERROR; then

	    echo `date` >> $ELO_CONFIG_LOG
	    echo "" >> $ELO_CONFIG_LOG

	    if ! $ELO_CONFIG_SH_DETECTED; then
	        
		echo "4820-x1x or 4820-xLx monitor was detected, but" \
		     "either eloconfig binary or EloConfig.sh was not found. " >> $ELO_CONFIG_LOG

	    fi

	    if ! $ELOFOUSB_KMP_DETECTED; then
		
		echo "4820-x1x or 4820-xLx monitor was detected, but" \
		     "elofousb kernel module was not installed. " >> $ELO_CONFIG_LOG

	    fi

	    echo "" >> $ELO_CONFIG_LOG
	    echo "Using configuration for Toshiba_elotone4sle" >> $ELO_CONFIG_LOG
	    echo "" >> $ELO_CONFIG_LOG

	fi
    RETVAL=$?
    fi
    rc_status -v
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/Toshiba_elotone4sle
}

stop () {
    echo -n "Stopping Toshiba_elotone4sle: "
    if [ "$PORT" != "" ]; then
    /usr/sbin/Toshiba_elotone4sle -s OFF -d $PORT 
    RETVAL=$?
    fi
    rc_status -v
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/Toshiba_elotone4sle
}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart)
	stop
	start
	;;
    *)
        echo "Usage: Toshiba_elotone4sle {start|stop|restart}"
        RETVAL=0
esac

exit $RETVAL
