#!/bin/bash
function print_centered {
     [[ $# == 0 ]] && return 1

     declare -i TERM_COLS="$(tput cols)"
     declare -i str_len="${#1}"
     [[ $str_len -ge $TERM_COLS ]] && {
          echo "$1";
          return 0;
     }

     declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))"
     [[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" "
     filler=""
     for (( i = 0; i < filler_len; i++ )); do
          filler="${filler}${ch}"
     done

     printf "%s%s%s" "$filler" "$1" "$filler"
     [[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}"
     printf "\n"

     return 0
}

function intro {
print_centered "$(tput setaf 5)"
print_centered "Chroot"
echo -n "$(tput setaf 7)"
}

function nvidia_driver_ {
if ! [ -f /home/wareck/.pass1 ]
then
cd /home/wareck/
wget -c http://wareck.free.fr/genethos/NVIDIA-Linux-x86_64-470.129.06.run
chmod +x /home/wareck/NVIDIA-Linux-x86_64-470.129.06.run
touch /home/wareck/.pass1
sudo systemctl isolate multi-user.target
fi
if ! [ -f /home/wareck/.pass2 ]
then
modprobe -r nvidia-drm
cd /home/wareck/
sudo ./NVIDIA-Linux-x86_64-470.129.06.run -a
touch /home/wareck/.pass2
fi
if ! [ -f /home/wareck/.pass3 ]
then
systemctl start graphical.target
rm /home/wareck/.pass1
rm /home/wareck/.pass2
rm /home/wareck/NVIDIA-Linux-x86_64-470.129.06.run
rm -r -f /home/wareck/.config/autostart/nvidia_rtx.desktop
systemctl start graphical.target
fi

}

function autorunme {
if [ ! -d /home/wareck/.config/autostart ]
then
echo -e "$(tput setaf 10)\nInit install & autostart:$(tput sgr0)"
mkdir /home/wareck/.config/autostart
echo -e "Done."
fi

if [ ! -f /home/wareck/.config/autostart/nvidia_rtx.desktop ]
then
cat <<'EOF'>> /home/wareck/.config/autostart/nvidia_rtx.desktop
[Desktop Entry]
# VERSION=3.36.2
Name=Terminal
Comment=Use the command line
Keywords=shell;prompt;command;commandline;cmd;
TryExec=gnome-terminal
Exec=gnome-terminal -- bash -c "sudo /home/wareck/backup_users_win10/build/nvidia_rtx.sh; exec bash"
Icon=org.gnome.Terminal
Type=Application
Categories=GNOME;GTK;System;TerminalEmulator;
StartupNotify=true
X-GNOME-SingleWindow=false
OnlyShowIn=GNOME;Unity;
Actions=new-window;preferences;
X-Ubuntu-Gettext-Domain=gnome-terminal

[Desktop Action new-window]
Name=New Window
Exec=gnome-terminal --window

[Desktop Action preferences]
Name=Preferences
Exec=gnome-terminal --preferences
EOF
chmod +x /home/wareck/.config/autostart/nvidia_rtx.desktop
fi

if ! grep "wareck" /etc/sudoers >/dev/null
then
sudo sh -c "echo \"wareck ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers"
fi
}


intro
autorunme
nvidia_driver_
