#!/bin/bash

. ./function.sh
led='/usr/sbin/iTalk/led';

$led -l2 -o0 ;$led -l2 -o0; $led -l3 -o0;
Beep 1 1 0;
blink &
BLINK_PID=$!;

KeepProjectFile;
Return=$(WebUpdateCheckTarFileName);
if [ $Return -eq -1 ]; then
    ShowCheckTarFileNameError;
    kill $BLINK_PID;
    $led -l2 -o1 ;
    exit 0;
fi
WriteStringToWebLog "Install Start";
#Beep 1 1 0;
ClearApp;
RenameFactoryFolder;
WriteStringToWebLog "Step1: Backup Firmware.";
RenameAPPFolder;
WebUpdateDecompressedFile;
WriteStringToWebLog "Step2: Decompressed Firmware Successful.";
Install_openvpn_if_needed;
WebUpdateDeleteDecompressedFile;
DeleteServiceEnvironment;
UsbCopyEnvironment;
UsbSetEnvironment;
ChmodFile;
UpdateService /;
DeleteOldAPPFolder;
DeleteOldFactoryFolder;
CreatFactoryEnvironment;
ClearEeprom;
MigrateRestartUnit;
SetEeprom "Successful update by Web";
RestoreProjectFiles;
sync;
WriteStringToWebLog "Step4: WebUpdate OK.";
DeleteWebRedundantFile;
sleep 5
kill $BLINK_PID;
$led -l2 -o1 ;
Beep  2 1 0.5;
$led -l2 -o0 ;

reboot -f