#!/bin/bash

NUM_OF_UART=""
NUM_OF_UART_MODE=4
FILE_PATH="/usr/sbin/mx-uart-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_uart <num_of_ports>"
        exit 1
fi

NUM_OF_UART=$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 Port format
bash $FILE_PATH -p 00 >/dev/null 2>&1
test_fail_result $? "Test 2: Invalid Port format"

# Test 3: Invalid Port
bash $FILE_PATH -p $NUM_OF_UART >/dev/null 2>&1
test_fail_result $? "Test 3: Invalid Invalid Port"

# Test 4: Invalid Mode format
bash $FILE_PATH -p 0 -m 00 >/dev/null 2>&1
test_fail_result $? "Test 4: Invalid Mode format"

# Test 5: Invalid Mode
bash $FILE_PATH -p 0 -m $NUM_OF_UART_MODE >/dev/null 2>&1
test_fail_result $? "Test 5: Invalid Invalid Mode"

# Test 6: Only pass mode parameter
bash $FILE_PATH -m 0 >/dev/null 2>&1
test_fail_result $? "Test 6: Only pass mode parameter"

# Test 7: Get all port mode
for ((i = 0; i < $NUM_OF_UART; ++i)); do
        result=$(bash $FILE_PATH -p $i)
        test_pass_result $(
                [[ "$result" == *"RS-232"* ]] ||
                        [[ "$result" == *"RS-485-2W"* ]] ||
                        [[ "$result" == *"RS-422"* ]] ||
                        [[ "$result" == *"RS-485-4W"* ]] ||
                        [[ "$result" == *"RS-422/RS-485-4W"* ]]
                echo $?
        ) "Test 7: Get port $i mode"
done

# Test 8: Set all port mode RS-232
for ((i = 0; i < $NUM_OF_UART; ++i)); do
        bash $FILE_PATH -p $i -m 0 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 8: Set port $i mode RS-232"
                continue
        fi

        result=$(bash $FILE_PATH -p $i)
        test_pass_result $(
                [[ "$result" == *"RS-232"* ]]
                echo $?
        ) "Test 8: Set port $i mode RS-232"
done

# Test 9: Set all port mode RS-485-2W
for ((i = 0; i < $NUM_OF_UART; ++i)); do
        bash $FILE_PATH -p $i -m 1 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 9: Set port $i mode RS-485-2W"
                continue
        fi

        result=$(bash $FILE_PATH -p $i)
        test_pass_result $(
                [[ "$result" == *"RS-485-2W"* ]]
                echo $?
        ) "Test 9: Set port $i mode RS-485-2W"
done

# Test 10: Set all port mode RS-422
for ((i = 0; i < $NUM_OF_UART; ++i)); do
        bash $FILE_PATH -p $i -m 2 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 10: Set port $i mode RS-422"
                continue
        fi

        result=$(bash $FILE_PATH -p $i)
        test_pass_result $(
                [[ "$result" == *"RS-422"* ]] || [[ "$result" == *"RS-422/RS-485-4W"* ]]
                echo $?
        ) "Test 10: Set port $i mode RS-422"
done

# Test 11: Set all port mode RS-485-4W
for ((i = 0; i < $NUM_OF_UART; ++i)); do
        bash $FILE_PATH -p $i -m 3 >/dev/null 2>&1
        if [[ $? -ne 0 ]]; then
                test_pass_result 1 "Test 11: Set port $i mode RS-485-4W"
                continue
        fi

        result=$(bash $FILE_PATH -p $i)
        test_pass_result $(
                [[ "$result" == *"RS-485-4W"* ]] || [[ "$result" == *"RS-422/RS-485-4W"* ]]
                echo $?
        ) "Test 11: Set port $i mode RS-485-4W"
done
