#!/bin/bash
cd "$(dirname "$(readlink -f "$0")")"

function help() {
    show_sn
    show_ver
    echo -e "Usage: \n\t$(basename "$0")  program_name [program_name ...] \n"
    echo -e "Description:"
    echo -e "\tCheck the input file(s) version info (EXE/LIB build info)"
    echo -e "Example:"
    echo -e "\t$(basename "$0") manage  /usr/poweroadlib/libprpublicmemory.so"
}

function show_ver() {
    echo -e "$(basename "$0") version 1.0.0"
    echo "------------------------------------------"
}

function show_sn() {
    # show SN
    echo "__________________________________________"

   # 检查并显示EMS-SN
    if [ -f /usr/poweroadlib/.prsn ]; then
        echo "EMS-SN:     $(cat /usr/poweroadlib/.prsn| tr -d '\0')"
    else
        echo "EMS-SN:     "
    fi

    # 检查并显示Product-SN
    if [ -f /usr/poweroad/.productsn ]; then
        echo "Product-SN: $(cat /usr/poweroad/.productsn| tr -d '\0')"
    else
        echo "Product-SN: "
    fi

    echo "------------------------------------------"


}

# Check if correct number of arguments is passed：
if [ "$#" -lt 1 ]; then
    echo "Error: No arguments provided!"
    help
    exit 1
fi


for fileName in "${@}"
do
    if [ -e "$fileName" ]; then
        #echo -e "\n>>>> FileName: $fileName  $(md5sum $fileName | awk '{print $1}')"
		echo -ne "\nMD5: "; md5sum $fileName
        exe_build_info=$(strings "$fileName" | grep 'EXE: build ')
        if [ -n "$exe_build_info" ]; then
            echo "$exe_build_info"
            continue
        fi

        lib_build_info=$(strings "$fileName" | grep 'LIB: build ')
        if [ -n "$lib_build_info" ]; then
            echo "$lib_build_info"
            continue
        fi

        echo "❌ Not a PR version file (no EXE/LIB build info found)"

    else
        if [ "$fileName" == "-v" ] || [ "$fileName" == "-sn" ]; then
             show_sn
             if [ "$fileName" == "-v" ]; then
                 show_ver
             fi
             break
        else
            echo -e "\n❌ Not found: $fileName"
        fi
    fi
done