#!/bin/bash

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

GPC_NAME=""
SENSORS_BAT_LABEL=""
NORMAL_MESSAGE="Normal voltage"
WARN_MESSAGE="Low voltage, please replace the RTC battery soon"

function V2426D::profile() {
        V2000D::profile
}

function V2426D::init() {
        V2000D::init
}

function V2426D::get_rtc_battery_state() {
        V2000D::get_rtc_battery_state
}

function V2406D::profile() {
        V2000D::profile
}

function V2406D::init() {
        V2000D::init
}

function V2406D::get_rtc_battery_state() {
        V2000D::get_rtc_battery_state
}

function V2202D::profile() {
        V2000D::profile
}

function V2202D::init() {
        V2000D::init
}

function V2202D::get_rtc_battery_state() {
        V2000D::get_rtc_battery_state
}

function V2000D::profile() {
        SENSORS_BAT_LABEL="Vbat"
}

function V2000D::init() {
        if ! command -v sensors >/dev/null 2>&1; then
                echo "Package lm-sensors is NOT installed. Please install lm-sensors package."
                exit 1
        fi
}

function V2000D::get_rtc_battery_state() {
        if ! check_vbat; then
                echo $WARN_MESSAGE
                wall -n $WARN_MESSAGE
        else
                echo $NORMAL_MESSAGE
        fi
}

function V3400::profile() {
        SIO_BATDET=63
        SIO_BATLOW=64
        BATDET_ENABLE=1
        BATDET_DISABLE=0
}

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

function V3400::get_rtc_battery_state() {
        local rtc_battery_state

        # enable HW comparator
        gpio_set_value_sysfs $(gpc_it8786 $SIO_BATDET) $BATDET_ENABLE

        # get HW comparator output
        rtc_battery_state=$(gpio_get_value_sysfs $(gpc_it8786 $SIO_BATLOW))

        # disable HW comparator
        gpio_set_value_sysfs $(gpc_it8786 $SIO_BATDET) $BATDET_DISABLE

        if [ "${rtc_battery_state}" = "1" ]; then
                echo $NORMAL_MESSAGE
        else
                wall -n "Low voltage, please replace the RTC battery soon"
        fi
}

check_vbat() {
        # Returns 1 if Vbat < 2.4, otherwise returns 0
        sensors | awk -v label="$SENSORS_BAT_LABEL" '
        $0 ~ label {
                val = $2 + 0;
                unit = $3;
                if (unit == "mV") val = val / 1000;
                if (val < 2.4) exit 1;
        }'
}

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
        "${MODEL_NAME}"::get_rtc_battery_state
}

main $@
