#!/bin/sh
#
# tecom
#
# start the tecom modules 
. /etc/init.d/rc.config
STATUS=/usr/sbin/upgd_status

# get phone model
PHONE_MODEL=`/usr/bin/CLISettings get CURRENT FAB_PHONE_MODEL`
MODULES_TECOM=/etc/modules.tecom.$PHONE_MODEL
#crms00453520 tingh+
TS_TYPE_FLAG=/tmp/modules.ts
#crms00453520 tingh-
HW=`CLISettings get CURRENT FAB_HW_VERSION`
platform_hard_ver=
# CRMS00402263
proc_delaytime_entry="/proc/evdev/delaytime"
# CRMS00402263 END

platform_hardware_version()
{
    if [ "$HW" = "3IN1_P1T1V3" ] ; then
        platform_hard_ver=0
    elif [ "$HW" = "3IN1_P1T1V4" ] ; then
        platform_hard_ver=1
    elif [ "$HW" = "3IN1_VHE_P1V5" ] ; then
        platform_hard_ver=2
    elif [ "$HW" = "3IN1_VHE_P1V6" ] ; then
        platform_hard_ver=3
    elif [ "$HW" = "3IN1_VHE_P2" ] ; then
        platform_hard_ver=4
    elif [ "$HW" = "3IN1_VHE_P2B0" ] ; then
        platform_hard_ver=5
    elif [ "$HW" = "3IN1_VHE_P2R7" ] ; then
        platform_hard_ver=6
    elif [ "$HW" = "3IN1_VHE_P3" ] ; then
        platform_hard_ver=7
    elif [ "$HW" = "3IN1_VHE_P3TF" ] ; then
        platform_hard_ver=8
    elif [ "$HW" = "3IN1_VHE_P3TF2" ] ; then
        platform_hard_ver=9
    elif [ "3IN1_VHE_PPR1" ] ; then
        platform_hard_ver=10
    fi
}

if [ ! -x /etc/init.d/rc.modules.file ]; then 
   echoerror \"Cannot start modules.file script $MODULES_TECOM \"; 
   exit 1
fi
if [ ! -f $MODULES_TECOM ]; then 
   echoerror \"Cannot find the file containing modules \"; 
   exit 1
fi

# do the required action
case "$1" in
    start|"")
	if [ "$PHONE_MODEL" = "VHE" ]; then
                echo "tcm_drv_ts timedelta=50000" >$TS_TYPE_FLAG
		platform_hardware_version
		if [[ -z $platform_hard_ver ]] || [[ $platform_hard_ver -ge 10 ]]; then
			echo "FORCE GPIO42 to LOW for KEYPAD and GPIO40 to HIGH for TS on VHE2"
			gpio output 42 0
			gpio output 40 1
#PR431002 tingh+
#Adapt Focaltech touchpanel
                        HW_TP=$(CLISettings get CURRENT FAB_HW_TOUCHPANEL 2>/dev/null)
                        if [ ! `echo $HW_TP | grep '^touchscreen_CY8CTMG120'` ]; then
		            echo "FORCE GPIO51 to HIGH"
                            gpio output 51 1
#crms00453520 tingh+
                            #touch touchpanel type flag
                            echo "ts_ft5x06 timedelta=50000" >$TS_TYPE_FLAG
#crms00453520 tingh-
                        fi
#PR431002 tingh-
                fi
	fi
	launch="yes"
	if [ -x $STATUS ]; then
		$STATUS >/dev/null 2>&1
		if [ $? -eq 1 ]; then 
			launch="no"
			echo "upgrade in progress : will not load tecom modules"
		fi	
	fi
	if [ "$launch" = "yes" ]; then
            # CRMS00402263
                # reset press delay duration
                value="`CLISettings get LOCAL ADMCFG_PRESS_DELAY_DURATION`"
                ret=`expr match $value "[0-9][0-9]*$"`
                if [[ $ret -gt 0 ]]; then
                    if [[ -f $proc_delaytime_entry ]]; then
                        echo "reset press delay duration value"
                        echo $value > $proc_delaytime_entry
                    fi
                fi
            # CRMS00402263 END

        	# Start the modules
               	/etc/init.d/rc.modules.file $MODULES_TECOM
                /etc/init.d/rc.modules.file $TS_TYPE_FLAG
                if [[ -z $platform_hard_ver ]] || [[ $platform_hard_ver -ge 10 ]]; then
                    /etc/init.d/rc.modules.file /etc/modules.tecom.VHE2
                fi
        fi
    ;; 
    stop)
        	# Stop the modules
               	/etc/init.d/rc.modules.file $MODULES_TECOM unload /etc/modules.no-unload
#crms00453520 tingh+                
                /etc/init.d/rc.modules.file $TS_TYPE_FLAG unload
#crms00453520 tingh-
    ;;
    *)
        echo "Usage $scriptname {start|stop}" >&2
        exit 1
    ;;
esac
