#!/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
}

function test_skip_result {
        echo -e "[\e[33mSKIP\e[0m] $1"
}

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 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 10: Get slot $i power enable pin status"
        else
                test_pass_result $(
                        [[ "$result" == *"low"* ]] ||
                        [[ "$result" == *"high"* ]]
                        echo $?
                ) "Test 10: Get slot $i power enable pin status"
        fi
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 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 11: Get slot $i reset pin status"
        else
                test_pass_result $(
                        [[ "$result" == *"low"* ]] ||
                        [[ "$result" == *"high"* ]]
                        echo $?
                ) "Test 11: Get slot $i reset pin status"
        fi
done

# Test 12: Get all slot disable pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -d 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 12: Get slot $i disable pin status"
        else
                test_pass_result $(
                        [[ "$result" == *"low"* ]] ||
                        [[ "$result" == *"high"* ]]
                        echo $?
                ) "Test 12: Get slot $i disable pin status"
        fi
done

# Test 13: Get all slot turn-on pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -t 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 13: Get slot $i turn-on pin status"
        else
                test_pass_result $(
                        [[ "$result" == *"low"* ]] ||
                        [[ "$result" == *"high"* ]]
                        echo $?
                ) "Test 13: Get slot $i turn-on pin status"
        fi
done

# Test 14: Get all slot dip pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i --dip 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 14: Get slot $i dip pin status"
        else
                test_pass_result $(
                        [[ "$result" == *"off"* ]] ||
                        [[ "$result" == *"on"* ]] ||
                        [[ "$result" == *"low"* ]] ||
                        [[ "$result" == *"high"* ]]
                        echo $?
                ) "Test 14: Get slot $i dip pin status"
        fi
done

# Test 15: Get all slot module config pin status
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i --mod 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 15: Get slot $i module config pin status"
                continue
        fi
        if [[ "$result" == *"Please power on slot"* ]]; then
                bash $FILE_PATH -s $i -p high >/dev/null 2>&1
                result=$(bash $FILE_PATH -s $i --mod 2>&1)
        fi
        test_pass_result $(
                [[ "$result" == *"FN990"* ]] ||
                [[ "$result" == *"RM520N"* ]] ||
                [[ "$result" == *"LE910C4"* ]] ||
                [[ "$result" == *"active"* ]] ||
                [[ "$result" == *"inactive"* ]] ||
                [[ "$result" == *"unknown"* ]]
                echo $?
        ) "Test 15: Get slot $i module config pin status"
done


# Test 16: Set all slot power enable pin as high
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -p high 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 16: Set slot $i power enable pin as high"
                continue
        fi

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

# Test 17: Set all slot power enable pin as low
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -p low 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 17: Set slot $i power enable pin as low"
                continue
        fi

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

# Test 18: Set all slot reset pin as high
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -r high 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 18: Set slot $i reset pin as high"
                continue
        fi

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

# Test 19: Set all slot reset pin as low
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -r low 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 19: Set slot $i reset pin as low"
                continue
        fi

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

# Test 20: Set all slot disable pin as high
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -d high 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 20: Set slot $i disable pin as high"
                continue
        fi

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

# Test 21: Set all slot disable pin as low
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -d low 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 21: Set slot $i disable pin as low"
                continue
        fi

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

# Test 22: Set all slot turn-on pin as high
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -t high 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 22: Set slot $i turn-on pin as high"
                continue
        fi

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

# Test 23: Set all slot turn-on pin as low
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -t low 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 23: Set slot $i turn-on pin as low"
                continue
        fi

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

# Test 24: Set all slot sim select as 1
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -i 1 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 24: Set slot $i sim select as 1"
                continue
        fi

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

# Test 25: Set all slot sim select as 2
for ((i = 0; i < $NUM_OF_SLOT; ++i)); do
        result=$(bash $FILE_PATH -s $i -i 2 2>&1)
        if [[ "$result" == *"not supported"* || "$result" == *"not define"* ]]; then
                test_skip_result "Test 25: Set slot $i sim select as 2"
                continue
        fi

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