#!/bin/bash

# Script to upgrade SolydXK 11 to SolydXK 12
# Arjen Balfoort, 25-05-2023

# Packages that deborphan must NOT treat as orphans - comma separated list
# Set to '*' to keep everything
NOTORPHAN=''

# ===========================================================

# Check if this script is run as root
if [ $UID -ne 0 ]; then
  echo 'Please, type root password...'
  su -c "$0 $@"
  exit
fi

# Save user name
USR=$(logname)

# Save log file
LOG="/home/$USR/solydxk12-upgrade.log"
touch $LOG
chown $USR:$USR "$LOG"

# Get version
XK='solydx'; [ -e '/usr/bin/kdeinit5' ] && XK='solydk'
echo -e "\n>>> Start $XK upgrade to SolydXK 12 (Debian Bookworm)\n" | tee "$LOG"
echo "    Log: $LOG" | tee -a "$LOG"

# Ask user for an unattended upgrade
APT='apt-get'
echo
read -p "Do you feel brave enough to run an unattended upgrade (y/N)?" ANSWER
case "$ANSWER" in 
    y|Y )
        # Make this script unattended
        # https://debian-handbook.info/browse/stable/sect.automatic-upgrades.html
        export DEBIAN_FRONTEND=noninteractive
        APT="yes '' | apt-get -y -o DPkg::options::=--force-confdef -o DPkg::options::=--force-confold"
        echo "    User selected unattended upgrade." | tee -a "$LOG"
        ;;
esac

# Fix apt
echo -e '\n>>> Fix apt in case of broken installs\n' | tee -a "$LOG"
dpkg --configure -a
apt-get install -f

echo -e '\n>>> Make sure the system is up-to-date\n' | tee -a "$LOG"
apt-get --allow-releaseinfo-change update | tee -a "$LOG"
eval $APT dist-upgrade | tee -a "$LOG"

echo -e '\n>>> Point sources.list to solydxk-12/bookworm repositories\n' | tee -a "$LOG"
sed -i '{
s|deb.debian.org/debian [a-z ]*non-free.*|deb.debian.org/debian bookworm main contrib non-free non-free-firmware|g
s|bullseye|bookworm|g
s|testing|bookworm|g
s|solydxk[0-9e-]\+|solydxk-12|g
}' /etc/apt/sources.list
cat /etc/apt/sources.list | tee -a "$LOG"
apt-get update

echo -e '\n>>> Upgrade to SolydXK 12/Debian Bookworm' | tee -a "$LOG"
eval $APT upgrade | tee -a "$LOG"

# virtualbox-guest-dkms is not available anymore
# and causes errors during dist-upgrade in virtualbox
apt-get purge -y -q virtualbox-guest-dkms | tee -a "$LOG"

echo -e '\n>>> Finally run dist-upgrade\n' | tee -a "$LOG"
eval $APT dist-upgrade | tee -a "$LOG"
eval $APT autoremove | tee -a "$LOG"

echo -e '\n>>> Install/replace packages\n' | tee -a "$LOG"
if [ "$XK" == 'solydx' ]; then
    eval $APT purge solydxee-info | tee -a "$LOG"
    eval $APT install solydx-system-adjustments-12 | tee -a "$LOG"
else
    eval $APT purge solydkee-info | tee -a "$LOG"
    eval $APT install solydk-system-adjustments-12 | tee -a "$LOG"
fi

# Cleanup
echo -e '\n>>> Do you want to remove unavailable and orphaned packages?\n\n' \
'   WARNING: this will remove all packages not found in the current repositories\n' \
'            and it will remove all orphaned packages.\n\n'
read -p 'Do you want to continue  (y/N)?' ANSWER
case "$ANSWER" in 
    y|Y )
        if [ "$NOTORPHAN" != '*' ]; then
            echo -e '\n>>> Remove unavailable packages only when not manually held back\n' | tee -a "$LOG"
            for PCK in $(env LANG=C apt-show-versions | grep 'available' | cut -d':' -f1); do
                REMOVE=true
                for HELDPCK in $(env LANG=C dpkg --get-selections | grep hold$ | awk '{print $1}'); do
                    if [ $PCK == $HELDPCK ]; then
                        REMOVE=false
                    fi
                done
                if $REMOVE; then
                    if [[ "$NOTORPHAN" =~ "$PCK" ]] || [ "$NOTORPHAN" == '*' ]; then
                        echo "Not available but keep installed: $PCK" | tee -a "$LOG"
                    else
                        eval $APT purge $PCK | tee -a "$LOG"
                    fi
                fi
            done
            echo -e '\n>>> Removing orphaned packages\n' | tee -a "$LOG"
            DEBORPHAN='deborphan'
            if [ "$NOTORPHAN" ]; then
                DEBORPHAN='deborphan | sed "/"${NOTORPHAN//,/\/d;/}"/d"'
            fi
            ORPHANED=$(eval $DEBORPHAN)
            while [ "$ORPHANED" ]; do
                eval $APT purge $ORPHANED | tee -a "$LOG"
                RC=$(dpkg-query -l | sed -n 's/^rc\s*\(\S*\).*/\1/p')
                [ "$RC" ] && eval $APT purge $RC
                ORPHANED=$(eval $DEBORPHAN)
            done
        fi
        ;;
esac

echo
eval $APT autoremove | tee -a "$LOG"
echo
echo "Done!"
