#!/bin/env bash
#
# /* Nathaniel Russell <naterussell83@gmail.com>
#  * Copyright (C) 2021.
#  *
#  * Note: Above list of copyright holders is incomplete...
#  */

export user=`whoami`
# Input file
FILE=/etc/slackpkg/slackpkgplus.conf
FILE1=/bin/check
# How many seconds before file is deemed "older"
OLDTIME=86400
# Get current and file times
CURTIME=$(date +%s)
FILETIME=$(stat $FILE -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)

# If the user is root, its UID is zero.

if [ $UID  -eq  0 ]
then
  echo "You are root!"
  echo "Please wait until this program finish's!"
else
  echo "You are not root."
exit 0
fi
(
echo "1" ; sleep 1
if [ $TIMEDIFF -gt $OLDTIME ]; then
echo "# Resetting Slackpkg+ Configurations" ; sleep 1
( cd /etc/slackpkg ; rm -f slackpkgplus.conf )
( cd /etc/slackpkg ;  wget http://10.7.0.45/files/slackpkgplus.conf )
( cd /etc/slackpkg ; chmod -x slackpkgplus.conf ; touch slackpkgplus.conf)
echo "# Setting a Restore Point" ; sleep 1
xterm -e timeshift --create --comments "A new backup" --tags O
slackpkg update gpg
slackpkg update
rm /var/lock/slackpkg.* > /dev/null 2>&1 # Unlock
else
echo "# Slackpkpg+ Configuration is Current" ; sleep 1
fi
echo "10" ; sleep 1
echo "# Checking for System Updates" ; sleep 1
/usr/sbin/slackpkg check-updates 2> /dev/null | grep -q "AVAILABLE UPDATES"
rm /var/lock/slackpkg.* > /dev/null 2>&1 # Unlock
slackpkg update
echo "20" ; sleep 1
echo "# Checking for New Packages" ; sleep 1
xterm -e slackpkg -batch=on -default_answer=y -postinst=off install-new
echo "30" ; sleep 1
echo "# Checking Multilib" ; sleep 1
xterm -e slackpkg -batch=on -default_answer=y -postinst=off install multilib
echo "40" ; sleep 1
echo "# Checking Cinnamon Desktop" ; sleep 1
xterm -e slackpkg install cinnamon 
#echo "45" ; sleep 1
#echo "# Checking Gnome Desktop" ; sleep 1
#xterm -e slackpkg install gnome
echo "45" ; sleep 1
echo "# Checking Mate Desktop" ; sleep 1
xterm -e slackpkg install mate 
echo "60" ; sleep 1
echo "# Checking My Repository" ; sleep 1
xterm -e slackpkg install mine
echo "75" ; sleep 1
echo "# Upgrading All Packages" ; sleep 1
xterm -e slackpkg -batch=on -default_answer=y -postinst=off upgrade-all
echo "80" ; sleep 1
echo "# Checking for New Configurations" ; sleep 1
xterm -e slackpkg new-config
echo "85" ; sleep 1
if [ -f /boot/grub/grub.cfg ]; then
echo "# Grub is configured for Kernel" ; sleep 1
elif [ -f /boot/lilo.conf ]; then
echo "# Lilo is configured for Kernel" ; sleep 1
/sbin/liloconfig
elif [ -f /boot/efi/EFI/Slackware/elilo.conf ]; then
echo "# Elilo is configured for Kernel" ; sleep 1
/usr/sbin/eliloconfig
else
echo "# Kernel needs to be configured for System" ; sleep 1
fi
echo "95" ; sleep 1
echo "# Resetting cron jobs" ; sleep 1
/etc/rc.d/rc.crond restart
echo "# Resetting system jobs" ; sleep 1
echo "98" ; sleep 1
rm /var/lock/slackpkg.* > /dev/null 2>&1 # Unlock
if [ -f /etc/slackpkg/mirrors.new  ]; then
echo "10" ; sleep 1
echo "# Restarting System Updates" ; sleep 1
rm /var/lock/slackpkg.* > /dev/null 2>&1 # Unlock
slackpkg update
echo "50" ; sleep 1
echo "# Upgrading All Packages" ; sleep 1
xterm -e slackpkg -batch=on -default_answer=y -postinst=off upgrade-all
echo "75" ; sleep 1
echo "# Checking for New Configurations" ; sleep 1
xterm -e slackpkg new-config
else
echo ""
fi
echo "# Resetting system update program" ; sleep 1
echo "100" ; sleep 1
rm /var/lock/slackpkg.* > /dev/null 2>&1 # Unlock

) |
zenity --progress ..-percentage=0

zenity --info --title="Installation complete" --text="Please restart the system if a new Kernel was installed" --no-wrap
#zenity --info --title="Slackware Upgrade Program" --text="Upgrade complete." --width=200

#if [[ "$?" != "0" ]] ; then
#        zenity --error \
#          --text="Update canceled."
#	exit 1
#fi
