#!/bin/bash

self="$(basename "$0")"
log_file="$1"
if [ -z "${log_file}" ]; then
    log_file="/dev/null"
fi

message="${self}: Applying the new opkg signing configuration"
echo "${message}" | tee -a "${log_file}"

ACTILITY_DATA_DIR=/opt/actility-upgrade-data
[ -z "${ROOTACT}" ] && ROOTACT=/home/actility/

if [ $(ls ${ACTILITY_DATA_DIR}/lrr-*.tar.gz | wc -l ) -ne 1 ]; then
    message="${self}: No (or too many) possible LRR package"
    echo "${message}" | tee -a "${log_file}"
    exit 1
fi

ACTILITY_LRR=$(readlink -f $(ls ${ACTILITY_DATA_DIR}/lrr-*.tar.gz))
ACTILITY_LRR_VERSION=$(echo $(basename "${ACTILITY_LRR}") | cut -d- -f2)

cp ${ACTILITY_DATA_DIR}/lrr-* /tmp/

message="${self}: Upgrading LRR to ${ACTILITY_LRR_VERSION}"
echo "${message}" | tee -a "${log_file}"

$ROOTACT/lrr/com/cmd_shells/upgrade.sh -D 0 -S tek_generic -V ${ACTILITY_LRR_VERSION}

konaLTEmodem.py -c  'AT!ENTERCND="A710"'
modem_status=$(konaLTEmodem.py -c 'AT!USBCOMP?' | grep bitmask | cut -d' ' -f3)
if [ "${modem_status}" = "0000050D" ]; then
    konaLTEmodem.py -c  'AT!USBCOMP=1,1,10D'
    konaLTEmodem.py -c  'AT!CUSTOM="UIM2ENABLE",1'
    sleep 10
fi

modem_status=$(konaLTEmodem.py -c 'AT!UDUSBCOMP?' | grep UDUSBCOMP | cut -d':' -f2 | sed 's/ //g')
if [ "${modem_status}" = "7" ]; then
    konaLTEmodem.py -c 'AT!UDUSBCOMP=6'
    sleep 10
fi

message="${self}: Done"
echo "${message}" | tee -a "${log_file}"

/etc/init.d/gwmgr_restore start

safe_reset
