#!/bin/sh

export PATH=/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
UPDATE_TARBALL=update.tar.gz

echo "Update Script started"
pre_vers=`head -n 1 /etc/cabversion`

#####################################################################
## For AnyRover Mini / Emu4G
## An unexpected modem reset can lead to a reboot,
## which may corrupt the root file system.
## This workaround tries to prevent that by
##  - ensuring the AnyRover reboots in ~10 minutes in case of failure
##  - resetting the modem and preventing it from reconnecting
##  - waiting for the modem to settle
## If the modem reset works as expected, the update will just normally
## install.

(sleep 620 && reboot) &
pid=$!

touch /var/lock/modem
killall pppd
sleep 15
##
#####################################################################

path=$PWD

rm -f /etc/conf.d/cablynx.factory.rej
#<REMOVE>
cd /
tar xzf ${path}/${UPDATE_TARBALL}
sync

cd ${path}

## Finished, no more need for reboot, so cancel countdown
kill ${pid}

## Free the modem to connect again.
rm /var/lock/modem

echo "Update finished."

post_vers=`head -n 1 /etc/cabversion`
echo "Updated from ${pre_vers} to ${post_vers}"

echo "The config file /etc/cablynx.conf was untouched."
echo "Please use the new config file in /etc/cablynx.conf.${post_vers#*: }"

