#! /bin/bash

. ./function.sh
upload_path=$1

#2021/01/03
mkdir /tmp/Project/;
cp -rf /app/Web/public/File /tmp/Project/;
rm -rf /tmp/Project/File/Version.txt;
rm -rf /tmp/Project/File/Factory;
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

CheckTarFileName()
{
    Total=`ls -l $upload_path/*.tar  | grep "^-" | wc -l`;
    if [ "$Total" != "1" ]; then
        expr -1;
        return;
    fi
    
    Path="$upload_path/";
    TarName=`ls "$Path"ua-*.tar`;
    if [ "$TarName" = "" ]; then
        expr 0;
        return;
    fi

    mv $TarName "$Path"ICPDAS_Temp_app.tar;
    TarName1="UA-";
    TarName2=`echo $TarName | awk -F'-' '{print $2}'`;
    TarName=$TarName1$TarName2;
    mv "$Path"ICPDAS_Temp_app.tar $Path$TarName;
    expr 1;
}



Return=$(CheckTarFileName);
if [ $Return -eq -1 ]; then
    ShowCheckTarFileNameError;
    Beep 4 0.001 0.5;
    exit 0;
fi

ClearEeprom;
ShowStartInstall;
Beep 1 1 0.001;
ClearApp;
RemoveApp;
sync;sync;sync;
CreateEnvironment;
cp -rf $upload_path/app.tar.gz /.;
sync;sync;sync;
rm -rf /factory/*;
cp -rf $upload_path/UA-*.tar /factory;
sync;sync;sync;


#RemoveFactory;
#DecompressedFile;

tar --warning=no-timestamp -zxf /app.tar.gz -C /.;
sync;sync;sync;

#CopyCommonFile;
chmod 644 $upload_path/Environment/Service/*.service;
cp -rf $upload_path/Environment/Service/*.service /lib/systemd/system/;
cp -rf $upload_path/RotarySwitch.sh /;
cp -rf $upload_path/Environment/DetectionNetWork.sh /Init/;
cp -rf $upload_path/Environment/function.sh /Init/;
chmod 755 /Init/function.sh >& /dev/null;
chmod 755 /Init/DetectionNetWork.sh >& /dev/null;

#CopyWirelessFile;
Wireless_3G=`lsusb -d 05c6:`;
Wireless_4G=`lsusb -d 2c7c:`;
if [ "$Wireless_3G" != "" ] || [ "$Wireless_4G" != "" ]; then
    cp -rf $upload_path/Environment/Wireless/PPP0route /etc/ppp/ip-up.d/.;
    cp -rf $upload_path/Environment/Service/UA-Wireless.service  /lib/systemd/system/;
else
    rm -rf /app/Wireless/*;
fi

#SetEnvironment;

systemctl enable OPC-5231-SearchIP.service;
systemctl enable UA-5000-RotarySwitch.service;
systemctl enable Nodejs.service;
systemctl enable IftttConditionTrigger.service;
systemctl enable Daemon.service;
systemctl enable UA-Wireless.service;
systemctl enable BackstageService.service;
systemctl enable dynamic-blocklist.service;
systemctl enable DetectionNetWork.service;

ChmodFile;

Install_openvpn_if_needed;

#DeleteResidualFile;
rm -rf $upload_path;
rm -rf /app.tar.gz;
rm -rf /app/VPNClient;

sync;sync;sync;
SetEeprom "Successful update from utility";

#2021/01/03 
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/;  
rm -rf /tmp/Project/;
UpdateComPortSetting;
sync;
#2021/01/03
ShowInstallSuccess;
Beep 1 1 0.5;
Beep 1 1 0.001;
