#!/bin/bash
clear
export LC_ALL=C
export DEBIAN_FRONTEND=noninteractive
echo "This will launch the Major upgrade to EVE Sat 5.0"
echo "Requirements:"
echo "- EVE Sat must be up to date ( 4.0.1-86 )"
echo "- All Ubuntu packages must be up to date"
echo "- External repositories are no supported ( only ubuntu and eve-ng are valids )"
echo -n  "type yes to continue: "
read confirm
if [ "$confirm" != "yes" ] ; then exit 0 ; fi
dpkg -l eve-agent | grep -q '4.0.1-86\|5.0.*'
if [ $? -ne 0 ]
 then echo  "Upgrade REJECT !!!!!"
 echo  "Upgrade to latest EVE Sat Pro version first. Use 'apt update; apt dist-upgrade'"
 echo
 echo  "Retry when condition is met"
 exit 0
fi
dpkg -l eve-agent | grep -q '5.0.*' 
if [ $? -eq 0 ]
	then apt remove -y -q eve-agent
fi
apt update 
apt list --upgradable | grep -q from
if [ $? -eq 0 ]
 then echo  "Upgrade REJECT !!!!!"
 echo "First use 'apt update ; apt upgrade; apt dist-upgrade ; reboot'"
 echo
 echo  "Retry when condition is met"
 exit 0
fi
E=$(ls /var/lib/apt/lists/*Packages | grep -v 'eve-ng.net\|ubuntu.com' | wc -l)
if [ $E -gt 0 ]
	then echo  "Upgrade REJECT !!!!!"
	echo "external repository detected"
	echo "Fresh install is recommended"
	echo "rejected repository:" 
	ls -1 /var/lib/apt/lists/*Packages | grep -v 'eve-ng.net\|ubuntu.com' 
	exit 0
fi
cat > /etc/apt/apt.conf.d/local << EOF
Dpkg::Options {
   "--force-confdef";
   "--force-confold";
}
EOF

apt remove -y -q netplan.io
apt autoremove -y -q
apt-mark hold netplan.io
apt install ubuntu-release-upgrader-core -y -q
do-release-upgrade -f DistUpgradeViewNonInteractive 
apt-get -y -q install software-properties-common
apt-mark unhold netplan.io
wget -O - http://www.eve-ng.net/focal-test/eczema@ecze.com.gpg.key | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64]  http://www.eve-ng.net/focal-test focal main"
apt update
apt-get -y -q install eve-agent
apt dist-upgrade -y -q
apt autoremove -y -q
rm /etc/apt/apt.conf.d/local
echo -n  "Upgrade process done. Press Enter to reboot "
read confirm
reboot
