#!/bin/bash
# Ping google to test internet connection
if ping -c 1 8.8.8.8 &>/dev/null; then
  if [ $# -ne 2 ]
    then
      echo "Arguments needed: filename.deb, LTE-APN"
  elif [ -f $1 -a -n $2 ]
    then
      echo $1 $2
      systemctl stop watchdog.service
      systemctl stop slcengine.service
  # Take backups of /var/www/html and /opt
      mkdir ./www_bup
      cp -varP /var/www/html/* ./www_bup
      mkdir ./slc_bup
      cp -varP /opt/* ./slc_bup
  # Remove old version
      apt remove -y bitware-stable
  # Install new version
      echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections
      echo iptables-persistent iptables-persistent/autosave_v6 boolean true | sudo debconf-set-selections
      apt install -y ./$1
  # Set APN
      sed -i "/apn/s/= *.*/=${2}/" /etc/NetworkManager/system-connections/nm-lte
  # Copy backups back to appropriate places (doesn't overwrite files apart from home.xml)
      cp -vurP ./slc_bup/apps/* /opt/apps/
      cp -vurP ./slc_bup/slc/prg/* /opt/slc/prg/
      cp -vurP ./www_bup/* /var/www/html/
      cp -vaP --remove-destination ./slc_bup/apps/bitware/var/www/html/pages/home.xml /opt/apps/bitware/var/www/html/pages/
      cp -vaP --remove-destination ./www_bup/pages/home.xml /var/www/html/pages/

      echo "!REMEMBER! Ethernet IP settings should be changed and applied on Actiweb settings page!"
      echo "New connections are named nm-con#"
      echo "!!!REBOOT IN 30 MINUTES!!!"

      shutdown -r +30
  fi
else
  echo "Could not connect to the internet! Aborting update!"
fi
