#!/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_igtd
#
#    Moxa power ignition daemon
#    Detecting power ignition event then shutdown device
#	
#    2017-02-13  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"

# ignition signal
igt_signal_val=""
igt_status_val=""

usage(){
    echo usage
    exit 1
}

while((1))
do
    igt_signal_val="$(i2cget -f -y $IGT_BUS $IGT_ADDR $IGT_SIGNAL_ADDR)"
    igt_status_val="$(i2cget -f -y $IGT_BUS $IGT_ADDR $IGT_STATUS_ADDR)"

    if [[ ( x"$igt_signal_val" == x"$IGT_SIGNAL_OFF_VAL" ) && \
	( x"$igt_status_val" == x"${ENABLE_VAL}" ) ]]; then
	i2cset -f -y $IGT_BUS $IGT_ADDR $IGT_SHUTDOWN_CMD_ADDR $IGT_SHUTDOWN_CMD_VAL
	shutdown -h now
	exit 0
    fi
    sleep 1
done



