#!/bin/bash
#
# Copyright (C) 2024 MOXA Inc. All rights reserved.
# This software is distributed under the terms of the MOXA SOFTWARE NOTICE.
# See the file LICENSE for details.
#
# Authors:
#       2024    Elvis Yao <ElvisCW.Yao@moxa.com>
# Description:
#       ITxPT daemon
#

source "/usr/lib/mx-gpio-lib"
source "/usr/lib/mx-common-lib"

GPC_NAME=""
FLAG_FILE="/etc/.mx-itxpt"
DELAY_SEC=1

function V3400::profile() {
        SIO_DI_0=24
        SIO_DI_1=25
        SIO_IGN=30

        # before FPGA revert (DI_0, DI_1, IGN)
        ITS_ECO_0=(0 0 0) 
        ITS_ECO_1=(1 0 0)
        ITS_ECO_2=(0 1 0)
        ITS_ECO_2_IGN_ON=(0 1 1)
        ITS_SLEEP=(1 1 1)
}

function V3400::init() {
        GPC_NAME="$(gpio_get_gpiochip_name gpio_it87)"
        touch "$FLAG_FILE"
}

function V3400::get_eco_state() {
        local di_0
        local di_1
        local ign
        local cur_state

        di_0=$(gpio_get_value_libgpiod $SIO_DI_0 $GPC_NAME)
        di_1=$(gpio_get_value_libgpiod $SIO_DI_1 $GPC_NAME)
        ign=$(gpio_get_value_libgpiod $SIO_IGN $GPC_NAME)

        # compare ITS mode
        cur_state=("$di_0" "$di_1" "$ign")

        if [[ ${cur_state[@]} == ${ITS_ECO_0[@]} ]]; then
                if ! grep -w -q "ECO_0" $FLAG_FILE; then 
                        echo "ECO_0" # custom script for ECO 0 event
                        # write flag file
                        echo "ECO_0" >$FLAG_FILE
                fi
        elif [[ ${cur_state[@]} == ${ITS_ECO_1[@]} ]]; then
                if ! grep -w -q "ECO_1" $FLAG_FILE; then 
                        echo "ECO_1" # custom script for ECO 1 event
                        # write flag file
                        echo "ECO_1" >$FLAG_FILE
                fi
        elif [[ ${cur_state[@]} == ${ITS_ECO_2[@]} ]]; then
                if ! grep -w -q "ECO_2" $FLAG_FILE; then 
                        echo "ECO_2" # custom script for ECO 2 event
                        # write flag file
                        echo "ECO_2" >$FLAG_FILE
                fi
        elif [[ ${cur_state[@]} == ${ITS_ECO_2_IGN_ON[@]} ]]; then
                if ! grep -w -q "ITS_ECO_2_IGN_ON" $FLAG_FILE; then 
                        echo "ITS_ECO_2_IGN_ON, exec shutdown (after 5 min)" # custom script for ECO 2 + IGN ON event
                        # write flag file
                        echo "ITS_ECO_2_IGN_ON" >$FLAG_FILE
                        shutdown -h now
                fi
        elif [[ ${cur_state[@]} == ${ITS_SLEEP[@]} ]]; then
                if ! grep -w -q "ITS_SLEEP" $FLAG_FILE; then 
                        echo "ITS_SLEEP, exec shutdown (after 2 min)" # custom script for SLEEP event
                        # write flag file
                        echo "ITS_SLEEP" >$FLAG_FILE
                        shutdown -h now
                fi
        else
                echo "get ITS mode error"
                printf 'cur_state: %s\n' "${cur_state[@]}"
        fi
}

function script_init() {
        load_model_name

        if [[ ! $(type -t "${MODEL_NAME}"::profile) == function ]]; then
                echo "${MODEL_NAME} profile function is not define"
                exit 1
        fi

        "${MODEL_NAME}"::profile

        if [[ ! $(type -t "${MODEL_NAME}"::init) == function ]]; then
                echo "${MODEL_NAME} init function is not define"
                exit 1
        fi

        "${MODEL_NAME}"::init
}

function load_model_name() {
        for name in $(get_model_name_from_dmi_type12); do
                if [[ "$(type -t "${name}::profile")" = 'function' ]]; then
                        MODEL_NAME="${name}"
                        break
                fi
        done

        if [[ -z "${MODEL_NAME}" ]]; then
                for name in $(get_model_name_from_dmi_type1); do
                        if [[ "$(type -t "${name}::profile")" = 'function' ]]; then
                                MODEL_NAME="${name}"
                                break
                        fi
                done
        fi

        if [[ -z "${MODEL_NAME}" ]]; then
                echo "Unsupported model"
                exit 38
        fi
}

function main() {
        script_init
        while true; do
                "${MODEL_NAME}"::get_eco_state
                sleep $DELAY_SEC
        done
}

main $@
