#!/bin/sh
# CTM-500 upgrade L26ADR module firmware
# Cypress Solutions (c) 2024
VER=1.1.0
REPOSITORY="https://demo.cypress.bc.ca/files/fw"
RSYSLOG_ID="gps"
RSYSLOG_LVL="notice"
RSYSLOG_FACILITY="local0"
WGET_ARGS="--tries=3"
IMG_SRC_DIR="/tmp"
FW_IMG_PATTERN="${IMG_SRC_DIR}/*.tar"
TMP_FW_IMG="${IMG_SRC_DIR}/upgradefw_tmp_img.tar"


cleanup_install_files()
{
    rm ${FW_IMG_PATTERN} &> /dev/null
    # resume normal led mode
    echo "0" > /sys/kernel/ctm_led/diag
}

err_cleanup_and_exit()
{
    cleanup_install_files
    logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} FW upgrade error:$1
    echo $1 > /var/run/FW_ERR_CODE
    exit 1
}


# strip everything until the last slash
fwimg=$(echo $1 | awk -F'/' '{print $NF}')
logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "Start (${fwimg})"
if [ -z ${fwimg} ];then
    logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "No firmware image specified"
    echo "No firmware image specified."
    err_cleanup_and_exit 107
fi

url=$2
if [ -z ${url} ];then
    echo "No repository URL specified, using default"
    logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "No repository URL specified, using default"
    url=${REPOSITORY}
fi

dload=1
is_local_upgrade=0
br=""
if [ "${url}" = "local-web" ];then
    echo "Installing firmware from local webif"
    is_local_upgrade=1
    dload=0
elif [ "${url}" = "localhost" ];then
    echo "Installing firmware from local console"
    is_local_upgrade=1
    dload=0
fi

# if only user is given, it's token for iot server
user=$3

# may be provided if target URL is a ftp server
pw=$4

/usr/bin/cmd pwr override 1

# download the firmware image .tar file
cd ${IMG_SRC_DIR}
if [ "${dload}" = "1" ];then
    echo "downloading L26ADR firmware image:" ${url}/${fwimg}
    logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "downloading L26ADR firmware image: ${url}/${fwimg}"
    if [ -n "$user" -a -n "$pw" ]; then
        action=$(/usr/bin/wget ${WGET_ARGS} --ftp-user=$user --ftp-password=$pw -q ${url}/${fwimg} -O ${TMP_FW_IMG})
    elif [ -z ${user} ];then
        action=$(/usr/bin/wget ${WGET_ARGS} -q ${url}/${fwimg} -O ${TMP_FW_IMG})
    else
        action=$(/usr/bin/wget --no-check-certificate ${WGET_ARGS} -q ${url}/${fwimg}?token=${user} -O ${TMP_FW_IMG})
    fi
    if [ $? -ne 0 ]; then
        echo "Error downloading L26ADR firmware image"
        logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "Error downloading L26ADR firmware image"
        err_cleanup_and_exit 108
    fi
else
    mv ${fwimg} ${TMP_FW_IMG}
fi

# extract tar
rm -rf ${IMG_SRC_DIR}/.upgrade >/dev/null
mkdir ${IMG_SRC_DIR}/.upgrade
echo "Extracting image files" 
logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "Extracting image files"
action=$(tar xvf ${TMP_FW_IMG} -C ${IMG_SRC_DIR}/.upgrade)

# calculate md5 of downloaded img file
cd ${IMG_SRC_DIR}/.upgrade
echo "Verifying L26ADR firmware image"
logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "Verifying L26ADR firmware image"
retval=$(md5sum -c md5sum.txt)
if [ $? -eq 0 ];then
    echo "Checksum OK"
    logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "Checksum OK"
else
    echo "Checksum FAIL"
    logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "Checksum FAIL"
    err_cleanup_and_exit 109
fi

# trigger leds start fw upgrade
echo "4" > /sys/kernel/ctm_led/diag

# determine name of FW .bin file
bin_file=$(cat md5sum.txt | awk '{print $2}' | tr -d '\r')
mv ${bin_file} ${IMG_SRC_DIR}
# enter img src dir, execute the install script
cd ${IMG_SRC_DIR}
echo "installing firmware " ${IMG_SRC_DIR}/${bin_file}
qtel-l26ul -c -i ${IMG_SRC_DIR}/${bin_file}
ret=$?

echo "0" > /sys/kernel/ctm_led/diag
/usr/bin/cmd pwr override 0
cleanup_install_files

if [ "$ret" = "0" ]; then
    echo "L26ADR firmware update completed successfully"
    logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} Success
else 
    echo "L26ADR firmware installation failed with return code "$ret
    logger -p ${RSYSLOG_FACILITY}.${RSYSLOG_LVL} -t ${RSYSLOG_ID} "L26ADR firmware installation failed with return code: $ret"
    err_cleanup_and_exit 117
fi
