#! /bin/bash

. ./function.sh
KeepProjectFile;
Return=$(WebUpdateCheckTarFileName);
if [ $Return -eq -1 ]; then
    ShowCheckTarFileNameError;
    Beep 4 0.001 0.5;
    exit 0;
fi
WriteStringToWebLog "Install Start";
Beep 1 1 0.001;
ClearApp;
RenameFactoryFolder;
WriteStringToWebLog "Step1: Backup Firmware.";
RenameAPPFolder;
WebUpdateDecompressedFile;

WriteStringToWebLog "Step2: Decompressed Firmware Successful.";
Install_openvpn_if_needed;
WebUpdateDeleteDecompressedFile;
DeleteServiceEnvironment;
UsbCopyEnvironment;
UsbSetEnvironment;

ChmodFile;
DeleteOldAPPFolder;
DeleteOldFactoryFolder;
CreatFactoryEnvironment;
ClearEeprom;
SetEeprom "WebUpdate";
RestoreProjectFiles;
UpdateComPortSetting;
sync;
WriteStringToWebLog "Step4: WebUpdate OK.";
DeleteWebRedundantFile;
Beep 1 1 0.5;
Beep 1 1 0.001;
sleep 5
reboot -f