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

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

LED_STATE=("off" "on")
OS_ID="$(grep -oP '(?<=^ID=).+' /etc/os-release | tr -d '"')"

function mount_disk() {
        local disk_dev=$1
        local mount_path=$2

        /bin/mkdir -p $mount_path
        /usr/bin/systemd-mount --no-block --collect $disk_dev $mount_path
        if [[ $? -ne 0 ]]; then
                # get disk index from disk0pn, disk1pn, ...
                index=$(echo ${mount_path:11:1})
                "${MODEL_NAME}"::set_led_state $index 1
        fi
}

function umount_disk() {
        local disk_dev=$1
        local mount_path=$2

        /usr/bin/systemd-umount $mount_path
        /bin/rmdir $mount_path
        if [[ $? -ne 0 ]]; then
                # get disk index from disk0pn, disk1pn, ...
                index=$(echo ${mount_path:11:1})
                "${MODEL_NAME}"::set_led_state $index 0
        fi
}

function check_disk() {
        local disk_dev=$1

        # check exist
        if [ ! -e "$disk_dev" ]; then
                echo "Block device $disk_dev does not exist."
                exit 1
        fi

        # check disk does not belong to the boot disk
        boot_disk_dev=$(lsblk -no PKNAME $(findmnt -n -o SOURCE /) 2>/dev/null)

        if [ "$disk_dev" == "$boot_disk_dev" ]; then
                echo "mount disk $disk_dev is part of the boot disk $boot_disk_dev"
                exit 1
        fi
}

function V3400::profile() {
        GPIO_BTN_PIN=(2 4)
        GPIO_LED_PIN=(3 5)
        GPIO_CHIP="gpiochip0"
        TIME_INTER_BTN_1=""
        TIME_INTER_BTN_2=""
        TIME_PRESSED_BTN_1=""
        TIME_PRESSED_BTN_2=""
        TIME_INTERVAL_REMOVE_DISK="3" # pressed over 3 sec to unmount and remove disk
}

function V3400::set_led_state() {
        local port
        local state
        port=${1}
        state=${2}

        set_cp2112_led_state_libgpiod "$port" "$state"
}

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 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
}

function set_cp2112_led_state_libgpiod() {
        local idx=$1
        local state=$2
        local check_state
        local ret

        gpio_set_value_libgpiod ${GPIO_LED_PIN[$idx]} $state $GPIO_CHIP
        [[ $? -ne 0 ]] && ret=1

        check_state=$(gpio_get_value_libgpiod ${GPIO_LED_PIN[$idx]} $GPIO_CHIP)
        [[ $? -ne 0 ]] && ret=1
        if [[ $ret -ne 0 || "$state" -ne "$check_state" ]]; then
                echo "Set State Failed"
                exit $ret
        fi

        echo "Set Led#$idx state: ${LED_STATE[$state]}"
}

function main() {
        local action=$1
        local disk_dev=$2
        local mount_path=$3

        # if moxa-disk-hotswapd service is not running, the script shall not be executed
        if ! systemctl is-active --quiet moxa-disk-hotswapd; then
                echo "moxa-disk-hotswapd service is NOT running"
                exit 0
        fi

        script_init

        if [[ -z "${action}" ]]; then
                echo "Unsupported or null action"
                exit 1
        fi

        if [[ "$action" == "mount" ]]; then
                check_disk $disk_dev
                mount_disk $disk_dev $mount_path
        elif [[ "$action" == "umount" ]]; then
                check_disk $disk_dev
                umount_disk $disk_dev $mount_path
        else
                echo "action function is not defined or unsupported"
                exit 1
        fi
}

main $@
