#!/bin/bash

NUM_OF_SLOT=""
FILE_PATH="/usr/sbin/mx-module-ctl"

function test_pass_result {
        if [ $1 -eq 0 ]; then
                echo -e "[\e[32mPASS\e[0m] $2"
        else
                echo -e "[\e[31mFAIL\e[0m] $2"
        fi
}

function test_fail_result {
        if [ $1 -eq 0 ]; then
                echo -e "[\e[31mFAIL\e[0m] $2"
        else
                echo -e "[\e[32mPASS\e[0m] $2"
        fi
}

if [[ $# -ne 1 ]]; then
        echo "test_module <num_of_slots>"
        exit 1
fi

NUM_OF_SLOT=$1

# Test 1: Check if tool is installed
test -e $FILE_PATH
test_pass_result $? "Test 1: Check if tool is installed"

# Test 2: Invalid slot format
bash $FILE_PATH -s 00 >/dev/null 2>&1
test_fail_result $? "Test 2: Invalid slot format"

# Test 3: Invalid slot number
bash $FILE_PATH -s $NUM_OF_SLOT >/dev/null 2>&1
test_fail_result $? "Test 3: Invalid slot number"

# Test 4: Invalid power enable option format
bash $FILE_PATH -s 0 -p 0 >/dev/null 2>&1
test_fail_result $? "Test 4: Invalid power enable option format"

# Test 5: Invalid reset option format
bash $FILE_PATH -s 0 -r 0  >/dev/null 2>&1
test_fail_result $? "Test 5: Invalid reset option format"

# Test 6: Invalid disable option format
bash $FILE_PATH -s 0 -d 0  >/dev/null 2>&1
test_fail_result $? "Test 6: Invalid disable option format"

# Test 7: Invalid turn-on option format
bash $FILE_PATH -s 0 -t 0  >/dev/null 2>&1
test_fail_result $? "Test 7: Invalid turn-on option format"

# Test 8: Invalid SIM select option format
bash $FILE_PATH -s 0 -i 0  >/dev/null 2>&1
test_fail_result $? "Test 8: Invalid SIM select option format"

# Test 9: Only pass parameter without slot num
bash $FILE_PATH -p low >/dev/null 2>&1
test_fail_result $? "Test 9: Only pass parameter without slot num"

# Test 10: Get all slot power enable pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -p)
        test_pass_result $(
                [[ "$result" == *"low"* ]] ||
                [[ "$result" == *"high"* ]]
                echo $?
        ) "Test 10: Get slot $i power enable pin status"
done

# Test 11: Get all slot reset pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -r)
        test_pass_result $(
                [[ "$result" == *"low"* ]] ||
                [[ "$result" == *"high"* ]]
                echo $?
        ) "Test 11: Get slot $i reset pin status"
done

# Test 12: Get all slot disale pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -d)
        test_pass_result $(
                [[ "$result" == *"low"* ]] ||
                [[ "$result" == *"high"* ]]
                echo $?
        ) "Test 12: Get slot $i disale pin status"
done

# Test 12: Get all slot turn-on pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -t)
        test_pass_result $(
                [[ "$result" == *"low"* ]] ||
                [[ "$result" == *"high"* ]] ||
                [[ "$result" == *"not supported"* ]]
                echo $?
        ) "Test 12: Get slot $i turn-on pin status"
done

# Test 13: Get all slot dip pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i --dip)
        test_pass_result $(
                [[ "$result" == *"low"* ]] ||
                [[ "$result" == *"high"* ]] ||
                [[ "$result" == *"not supported"* ]]
                echo $?
        ) "Test 13: Get slot $i dip pin status"
done

# Test 14: Get all slot module config pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i --mod)
        test_pass_result $(
                [[ "$result" == *"FN990"* ]] ||
                [[ "$result" == *"RM520N"* ]] ||
                [[ "$result" == *"LE910C4"* ]]
                echo $?
        ) "Test 14: Get slot $i module config pin status"
done


# Test 15: Set all slot power enable pin as high
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -p high >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 15: Set slot $i power enable pin as high"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -p)
        test_pass_result $(
                [[ "$result" == *"high"* ]]
                echo $?
        ) "Test 15: Set slot $i power enable pin as high"
done

# Test 16: Set all slot power enable pin as low
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -p low >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 16: Set slot $i power enable pin as low"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -p)
        test_pass_result $(
                [[ "$result" == *"low"* ]]
                echo $?
        ) "Test 16: Set slot $i power enable pin as low"
done

# Test 17: Set all slot reset pin as high
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -r high >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 17: Set slot $i reset pin as high"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -r)
        test_pass_result $(
                [[ "$result" == *"high"* ]]
                echo $?
        ) "Test 17: Set slot $i reset pin as high"
done

# Test 18: Set all slot reset pin as low
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -r low >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 18: Set slot $i reset pin as low"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -r)
        test_pass_result $(
                [[ "$result" == *"low"* ]]
                echo $?
        ) "Test 18: Set slot $i reset pin as low"
done

# Test 19: Set all slot disable pin as high
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -d high >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 19: Set slot $i disable pin as high"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -d)
        test_pass_result $(
                [[ "$result" == *"high"* ]]
                echo $?
        ) "Test 19: Set slot $i disable pin as high"
done

# Test 20: Set all slot disable pin as low
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -d low >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 20: Set slot $i disable pin as low"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -d)
        test_pass_result $(
                [[ "$result" == *"low"* ]]
                echo $?
        ) "Test 20: Set slot $i disable pin as low"
done

# Test 21: Set all slot turn-on pin as high
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -t high >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 21: Set slot $i turn-on pin as high"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -t)
        test_pass_result $(
                [[ "$result" == *"high"* ]] ||
                [[ "$result" == *"not supported"* ]]
                echo $?
        ) "Test 21: Set slot $i turn-on pin as high"
done

# Test 22: Set all slot turn-on pin as low
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -t low >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 22: Set slot $i turn-on pin as low"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -t)
        test_pass_result $(
                [[ "$result" == *"low"* ]] ||
                [[ "$result" == *"not supported"* ]]
                echo $?
        ) "Test 20: Set slot $i turn-on pin as low"
done

# Test 23: Set all slot sim select as 1
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -i 1 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 23: Set slot $i sim select as 1"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -i)
        test_pass_result $(
                [[ "$result" == *"1"* ]]
                echo $?
        ) "Test 23: Set slot $i sim select as 1"
done

# Test 24: Set all slot sim select as 2
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        bash $FILE_PATH -s $i -i 2 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 24: Set slot $i sim select as 2"
                continue
        fi

        result=$(bash $FILE_PATH -s $i -i)
        test_pass_result $(
                [[ "$result" == *"2"* ]]
                echo $?
        ) "Test 24: Set slot $i sim select as 2"
done
