#!/bin/bash

. ./function.sh

led='/usr/sbin/iTalk/led';

$led -l2 -o0 ;$led -l2 -o0; $led -l3 -o0;
blink &
BLINK_PID=$!;
#2021/01/03
mkdir /tmp/Project/;
cp -rf /app/Web/public/File /tmp/Project/;
rm /tmp/Project/File/Version.txt;
rm /tmp/Project/File/Factory/Version.txt;
cp -rf /app/Web/cgi-bin /tmp/Project/;
cp -rf /app/Certificates /tmp/Project/;
cp -rf /app/DDNS/inadyn.conf /tmp/Project;
cp -rf /app/MQTT/Broker/mosquitto.conf /tmp/Project/;
cp -rf /app/runtime/project /tmp/Project/;
cp -rf /app/Wireless/wvdial.conf /tmp/Project/;
cp -rf /app/Firewall/blocklist/rule_config.json /tmp/Project/;
cp -rf /app/SNMP/File/snmpd.conf /tmp/Project/;
cp -rf /app/SNMP/File/Transform.json /tmp/Project/;

#2021/01/03
Return=$(UsbCheckTarFileName);
if [ $Return -eq -1 ]; then
    ShowCheckTarFileNameError;
    Beep 4 0.25 0.1;
    kill $BLINK_PID;
    $led -l2 -o1 ;
    exit 0;
fi

ClearEeprom;
ShowStartInstall;
#Beep 1 1 0;
ClearApp;
RemoveApp;
CreateEnvironment;
UsbCopyInstallFileToEnvironment;

Return=$(UsbComparisonTarFile);
if [ $Return -eq -1 ]; then
    ShowComparisonTarFileError;
    Beep 4 0.25 0.1;
    kill $BLINK_PID;
    $led -l2 -o1 ;
    exit 0;
fi

#RemoveFactory;
DecompressedFile;
UsbCopyEnvironment;
UsbSetEnvironment;
ChmodFile;
Install_openvpn_if_needed;
UsbDeleteResidualFile;
MigrateRestartUnit;
Sync;
SetEeprom "Successful update by USB";
 
#2024/01/11
cp -rf /tmp/Project/File /app/Web/public/;
cp -rf /tmp/Project/cgi-bin /app/Web/;
cp -rf /tmp/Project/Certificates /app/;
cp -rf /tmp/Project/inadyn.conf /app/DDNS;
cp -rf /tmp/Project/mosquitto.conf /app/MQTT/Broker/;
cp -rf /tmp/Project/project /app/runtime/;
cp -rf /tmp/Project/wvdial.conf /app/Wireless/;
cp -rf /tmp/Project/rule_config.json /app/Firewall/blocklist/;
cp -rf /tmp/Project/snmpd.conf /app/SNMP/File/;
cp -rf /tmp/Project/Transform.json /app/SNMP/File/;
rm -rf /tmp/Project/;

Sync;
#2021/01/03
ShowInstallSuccess;
kill $BLINK_PID;
$led -l2 -o1 ;
Beep  2 1 0.5;
$led -l2 -o0 ;
