#!/bin/bash

# Detect language
_LANG=$LANG
_aptParams="-y -o Acquire::https::Verify-Peer=false -o Dpkg::Options::=\"--force-confold\" -o Dpkg::Options::=\"--force-overwrite\""

# Use a log file
# Redirect stdout ( > ) into a named pipe ( >() ) running "tee"
exec > >(tee -i /var/log/neptune-upgrade.txt)
exec 2>&1

function mustBeRootError() {
  if [ $_LANG == "de_DE.UTF-8" ]; then
    echo "Dieses Script muss als root ausgeführt werden. Probieren Sie es doch mit sudo." 1>&2
  else
    echo "This script must be run as root. Try it for example with sudo." 1>&2
  fi
}

function notEnoughSpace() {
  if [ $_LANG == "de_DE.UTF-8" ]; then
    echo " ";
    echo "Nicht genügend freier Speicherplatz. Bitte stellen Sie sicher, dass Sie 2.4 GB freien Speicherplatz haben."
  else
    echo " ";  
    echo "Not enough free space. You need at least 2.4 GB free disk space"
  fi
}

function updateListError() {
  if [ $_LANG == "de_DE.UTF-8" ]; then
    echo " ";
    echo "Softwareliste konnte nicht aktualisiert werden. Stellen Sie sicher, dass Sie eine stabile Internetverbindung haben und probieren es erneut."
  else
    echo " ";  
    echo "Error updating repo list. Make sure to have a stable internet connection and try again."
  fi
}

function showWelcome() {
  clear;
  echo "Neptune Upgrade Script";
  echo "-----------------------------";
  if [ $_LANG == "de_DE.UTF-8" ]; then
    echo "Dieses Scipt upgradet Neptune 8.x auf Neptune 9."; 
    echo -e "\033[1mBitte stellen Sie sicher dass die mit dem Internet verbunden sind, genug freien Speicherplatz haben und ein Stromkabel am Laptop angesteckt ist\033[0m"; 
    echo ""; 
    echo "Drücken Sie eine beliebige Taste um fortzufahren oder STRG+C zum Abbrechen";
  else
    echo "This will upgrade Neptune 8.x to Neptune 9."; 
    echo -e "\033[1mPlease make sure to have a working internet connection, enough free space and a plugged in power cord on your laptop\033[0m"; 
    echo ""; 
    echo "Press any key to continue or CTRL+C to abort" ; 
  fi
}

function showGrubMsg() {
  echo "Running bootloader-config..."

  if [ -d /sys/firmware/efi/efivars ]; then
      echo " * Installing grub-efi (uefi)..."
      DEBIAN_FRONTEND=noninteractive apt-get -y install grub-efi
  else
      echo " * install grub... (bios)"
      DEBIAN_FRONTEND=noninteractive apt-get -y install grub-pc
  fi
  # Grub-PC needs specific update
  clear
  echo " "
#   if [ $_LANG == "de_DE.UTF-8" ]; then
#     echo "Bitte wählen Sie im folgenden Dialog ihre Startmedium aus (in der Regel /dev/sda)" 
#     echo " (Benutzen Sie die Tabulator Taste zur Navigation und Leertaste zur Auswahl)"
#     echo " "
#     echo "Drücken Sie eine beliebige Taste um fortzufahren oder STRG+C zum Abbrechen";
#   else
#     echo "Please choose your start disk in the following dialog" 
#     echo " (Use Tab keys to navigate and Spacebar to choose)"
#     echo " "
#     echo "Press any key to continue or CTRL+C to abort" ; 
#   fi
}

function showEndMsg() {
  if [ $_LANG == "de_DE.UTF-8" ]; then
    echo " ";
    echo " ";
    echo "Upgrade abgeschlossen. Bitte starten Sie den Rechner neu.";
    echo " "
    echo "Wenn die Oberfläche dazu nicht funktioniert tippen Sie bitte"
    echo "sudo reboot"
    echo "ein." 
    echo " "
  else
    echo " ";
    echo " ";
    echo "Upgrade finished. Please reboot.";
    echo " "
    echo "If the user interface for this is not working please type in"
    echo "sudo reboot"
    echo " "
  fi  
}

function checkDiskSpace() {
  DISK_SPACE=`df -m / | awk '/[0-9]%/{print $(NF-2)}'`
  if [[ $DISK_SPACE -lt 2400 ]]; then
    notEnoughSpace  
    exit 1
  fi
}

# -----------------------------------------------------------------------------------------------------------------------------------------

if [ "$(id -u)" != "0" ]; then
  mustBeRootError
  exit 1
fi

checkDiskSpace


showWelcome
read FOO
echo "Upgrade process starts...";

export DEBIAN_FRONTEND=noninteractive

# Kill packagekitd
killall -9 packagekitd

# Create Backup in case anything goes wrong
find /etc/apt/sources.list.d/ -exec cp {}{,.bak} \;
cp /etc/apt/sources.list{,.bak}
# save current grub config 
cp /etc/default/grub /etc/default/grub.sav
# Replace old debian with new stable
sed -i -e "s/bookworm/trixie/" /etc/apt/sources.list.d/*.list /etc/apt/sources.list
# Disable backports
sed -i '/bookworm-backports/s/^/#/g' /etc/apt/sources.list.d/debian.list
# Set correct new security repo
echo "deb http://security.debian.org/debian-security trixie-security main contrib non-free" > /etc/apt/sources.list

apt $_aptParams update
apt -y -o Acquire::https::Verify-Peer=false -o Dpkg::Options::="--force-confnew" install neptune-sourceslist neptune-base
apt $_aptParams update
#apt $_aptParams upgrade

apt purge -y -f --allow-remove-essential virtualbox-dkms libcrypt1:i386 libgcc-s1:i386 libc6:i386 gcc-12-base:i386 

apt $_aptParams install plasma-workspace gdb plasma-desktop neptune-settings-sddm plasma-nm skanlite gwenview hplip libvirt-daemon-system sane-utils amarok=3.2.2-1 amarok-common=3.2.2-1 amarok-utils=3.2.2-1 fwupd fwupdate libopenconnect5 pipewire-pulse udisks2

#apt $_aptParams update
# Run the full upgrade hopefully without any issue
apt $_aptParams dist-upgrade
# ------------------------------------------------------------------------------------------------------------------------------------------

apt -y -f -o Acquire::https::Verify-Peer=false install

apt $_aptParams full-upgrade

apt -y -f install

apt -y -o Dpkg::Options::="--force-confold" full-upgrade

showGrubMsg
# read FOO
cp /etc/default/grub.sav /etc/default/grub
update-grub
#DEBIAN_FRONTEND=dialog dpkg --configure grub-pc

apt -y full-upgrade

apt clean

showEndMsg
