#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;93m'
MAGENTA='\033[0;35m'
NC='\033[0m' # No Color

SLEEP_TIME="0.5"

LOG="/var/log/uncom-kernel68-update.log"

whoami | grep root || {
    echo "Error, should be launched as root"
    echo "Error, should be launched as root" >> $HOME/uncom-kernel68-update.log
    exit 1
}

function print() {
    echo -e "${GREEN}$1${NC}"
    echo $1 >> $LOG
}

function print_error() {
    echo -e "${RED}$1${NC}"
    echo $1 >> $LOG
}


date >> $LOG

cat /etc/apt/sources.list | grep -x "deb http://repository.uncom.tech/ andromeda-gnome45-rebuild main-rebuild" ; code1=$?
cat /etc/apt/sources.list | grep -x "deb http://repository.uncom.tech/ andromeda-kernel6.8-rebuild main-rebuild" ; code2=$?

# If no gnome45 in repo - probably user is on Uncom 2.2, no update in this case.
if [ $code1 -ne 0 ]
    then print_error "Error: No andromeda-gnome45-rebuild in repo."
        cat /etc/os-release >> $LOG
        exit 1
fi


if [[ $code1 -eq 0 && $code2 -ne 0 ]]
then sed -i "s/deb http\:\/\/repository.uncom.tech\/ andromeda-gnome45-rebuild main-rebuild/deb http\:\/\/repository.uncom.tech\/ andromeda-gnome45-rebuild main-rebuild\\ndeb http\:\/\/repository.uncom.tech\/ andromeda-kernel6.8-rebuild main-rebuild/" /etc/apt/sources.list
else print "Repository andromeda-kernel6.8-rebuild already added, proceed"
fi

print "Checking i386 architecture"
dpkg --print-foreign-architectures | grep i386 ; code386=$?
if [ $code386 -eq 0 ]
    then print "Found i386, removing..."
        dpkg --remove-architecture i386
    else print "No i386 found, proceeding..."
fi

print "Running apt update"
apt update 2>&1 | grep "" >> $LOG

# Check if nvidia-driver pack is installed
apt list --installed 2>&1 | grep "nvidia-driver" ; code=$?
if [ $code -eq 0 ]
    then print "nvidia-driver is installed, we need gcc-13 for new drivers"
        apt install -y gcc-13 g++-13 || {
            print_error "Error installing gcc-13, exiting"
            exit 1
}
        print "Removing current alternatives"
        update-alternatives --remove-all gcc
        update-alternatives --remove-all g++
        print "Adding alternatives for gcc"
        update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 10
        update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 20
        update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 30
        print "Adding alternatives for g++"
        update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 10
        update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 30
        print "Switch to gcc-13"
        update-alternatives --set gcc "/usr/bin/gcc-13"
        print "Switch to g++-13"
        update-alternatives --set g++ "/usr/bin/g++-13"
    else print "No nvidia-driver installed, proceeding..."
fi


print "Running apt dist-upgrade"
# We can't redirect output to LOG - it breaks whiptail messages on glibc packs updates
mv /var/log/apt/term.log /var/log/apt/term.log.bak
echo "Starting apt dist-upgrade" > /var/log/apt/term.log
apt dist-upgrade -y
cat /var/log/apt/term.log >> $LOG
print "apt dist-upgrade finished"
#2>&1 | grep "" >> $LOG

#print "Removing update pack"
#apt remove -y uncom-kernel68-update

#print "Removing not needed packs"
#apt -y autoremove 2>&1 | grep "" >> $LOG

print "Final_success"

# Remove updates links.
find /home/ -name "uncom-kernel68-update.desktop" > /var/log/update.f.txt
while read -r line;
do
    # To avoid space problems in "Рабочий стол"
    name=`echo $line | sed "s/ /\\\ /g"`
    rm -f "$name"
done < /var/log/update.f.txt

rm -f /var/log/update.f.txt

# Update icon cache
update-icon-caches /usr/share/icons/*
