#!/bin/bash 
#
#    Copyright (C) MOXA Inc. All rights reserved.
#
#    This software is distributed under the terms of the
#    MOXA License.  See the file COPYING-MOXA for details.
#
#    /sbin/mx_igt
#
#    Moxa power ignition utility
#	
#    2017-02-10  Harry YJ Jhou
#	 new release
#
IGT_BUS="1"
IGT_ADDR="0x30"
DELAY_POWER_ON_ADDR="0x10"
DELAY_POWER_OFF_ADDR="0x12"
IGT_STATUS_ADDR="0x00"
IGT_SIGNAL_ADDR="0x01"
IGT_SHUTDOWN_CMD_ADDR="0x02"
IGT_SHUTDOWN_CMD_VAL="0x5A"
IGT_SIGNAL_OFF_VAL="0x00"
IGT_SIGNAL_ON_VAL="0x01"

ENABLE_VAL="0x07"
DISABLE_VAL="0x00"

# define delay time of power function
power_func=""
delay_time=""

usage(){
    echo ""
    echo "Moxa power ignition utility"
    echo ""
    echo "Usage:"
    echo "      $0 [Options]"
    echo ""
    echo "Options:"
    echo " -l 		, list power ignition configuration"
    echo " -s [on|off]	, setting power on/off function"
    echo " -t <time>	, setting delay time(seconds) of power on/off"
    echo " -e 		, enable power ignition"
    echo " -d 		, disable power ignition"
    echo ""
    echo "Example:"
    echo "	mx_igt -l		, power ignition configuration state"
    echo "	mx_igt -s on -t 10	, set 10 seconds delay time for power on "
    echo "	mx_igt -d 		, disable power ignition function"

    exit 1
}

set_delay_time(){
	local power_func="$1"
	local delay_time="$2"

	# check delay time
	if [ x"$delay_time" == x"default" ]; then
		delay_time="30"
	fi

	if [ $delay_time -lt 0 -o $delay_time -gt 1023 ]; then
		usage
	fi

	# transfer delay time to hex
	delay_time=0x"$(printf "%x" "$delay_time")"
	
	if [ x"$power_func" == x"on" ]; then
		i2cset -f -y $IGT_BUS $IGT_ADDR $DELAY_POWER_ON_ADDR $delay_time w
	elif [ x"$power_func" == x"off" ]; then
		i2cset -f -y $IGT_BUS $IGT_ADDR $DELAY_POWER_OFF_ADDR $delay_time w
	else
		usage
	fi
}

config_list(){
	local dt_pwr_on
	local dt_pwr_off
	local status="unknown"
	local status_val
	local signal="unknown"
	local signal_val

	# delay time when power on
	dt_pwr_on="$((16#$(i2cget -f -y $IGT_BUS $IGT_ADDR $DELAY_POWER_ON_ADDR w | cut -c 3-6)))"

	# delay time when power off
	dt_pwr_off="$((16#$(i2cget -f -y $IGT_BUS $IGT_ADDR $DELAY_POWER_OFF_ADDR w | cut -c 3-6)))"

	# IGT status value
	status_val="$(i2cget -f -y $IGT_BUS $IGT_ADDR $IGT_STATUS_ADDR )"

	# IGT status 
	status="Unknown"
	[ x"${status_val}" == x"${ENABLE_VAL}" ] && status="Enable"
	[ x"${status_val}" == x"${DISABLE_VAL}" ] && status="Disable"

	# IGT signal value
	signal_val="$(i2cget -f -y $IGT_BUS $IGT_ADDR $IGT_SIGNAL_ADDR )"

	# IGT signal 
	signal="Unknown"
	[ x"${signal_val}" == x"${IGT_SIGNAL_ON_VAL}" ] && signal="ON"
	[ x"${signal_val}" == x"${IGT_SIGNAL_OFF_VAL}" ] && signal="OFF"

	echo "Power ignition configuration:"
	echo "---------------------------------------------"
	echo "Status : $status (val=$status_val)"
	echo "Signal : $signal (val=$signal_val)"
	echo "Delay time of power on : $dt_pwr_on (sec)"
	echo "Delay time of power off : $dt_pwr_off (sec)"
}

while getopts "ls:t:deh" o; do
    case "${o}" in
	l)
	    config_list
	    exit 0
	    ;;
	s)
	    power_func="${OPTARG}"
	    ;;
	d)
	    #i2cset -f -y $IGT_BUS $IGT_ADDR $IGT_STATUS_ADDR $DISABLE_VAL
	    i2cset -f -y $IGT_BUS $IGT_ADDR $IGT_STATUS_ADDR $DISABLE_VAL
	    echo "Ignition function is DISABLE"
	    exit 0
	    ;;
	e)
	    #i2cset -f -y $IGT_BUS $IGT_ADDR $IGT_STATUS_ADDR $ENABLE_VAL
	    i2cset -f -y $IGT_BUS $IGT_ADDR $IGT_STATUS_ADDR $ENABLE_VAL
	    echo "Ignition function is ENABLE"
	    exit 0
	    ;;
	t)
	    delay_time="${OPTARG}"
	    set_delay_time $power_func $delay_time
	    exit 0
	    ;;
	h|*)
	    usage
	    ;;
    esac
done
