#!/bin/bash
echo -e "linux config v2.1 wareck@gmail.com"
echo -e ""
PS3='Please enter your choice: '
options=("NVIDIA-RTX" "NVIDIA-GTX" "AMD" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "NVIDIA-RTX")
	    Mode="NVIDIARTX"
	    break
            ;;
        "NVIDIA-GTX")
            Mode="NVIDIAGTX"
            break
            ;;
        "AMD")
	    Mode="AMD"
	    break
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done

function update_one {
sudo timedatectl set-local-rtc 1 --adjust-system-clock
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get dist-upgrade -y
sudo apt-get install htop python3-pip openssh-server screen wakeonlan -y
cp miners.tar.xz /home/wareck/
cp wallpaper.tar.xz /home/wareck/
cp hashcat.tar.xz /home/wareck/
cd /home/wareck/
tar xvfJ wallpaper.tar.xz
sudo rm wallpaper.tar.xz
}

function update_two {
if [ $Mode = "NVIDIAGTX" ]
then
sudo apt-get install nvidia-cuda-toolkit nvidia-cuda-dev nvidia-driver-470 -y
fi
if [ $Mode = "NVIDIARTX" ]
then
sudo apt-get install nvidia-cuda-toolkit nvidia-cuda-dev nvidia-driver-510 -y
fi

sudo apt-get install samba cifs-utils mdadm git build-essential libtool pkg-config automake autoconf ethtool net-tools openvpn filezilla libuv1-dev libuvc-dev cmake git nmap -y
sudo apt-get install gcc-8 g++-8 mesa-utils libmicrohttpd-dev libcurl4-openssl-dev lvm2 llvm-dev gparted msr-tools nasm btrfs-progs -y
sudo apt-get install libssl-dev p7zip-full -y
sudo apt autoremove -y

if [ $Mode = "AMD" ]
then
cd /home/wareck
echo -e ""
echo -e "Install drivers:"
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/amdgpu-install_22.10.2.50102-1_all.deb -O /home/wareck/Build_Cube/pack/amdgpu-install_22.10.2.50102-1_all.deb
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/linux-firmware.tar.xz
tar xvfJ linux-firmware.tar.xz
sudo cp /home/wareck/linux-firmware/amdgpu/* /lib/firmware/amdgpu/
sudo rm -r -f /home/wareck/linux-firmware /home/wareck/linux-firmware.tar.xz
sudo apt-get install /home/wareck/Build_Cube/pack/amdgpu-install_22.10.2.50102-1_all.deb -y
sudo apt-get update
sudo amdgpu-install --opencl=legacy --accept-eula -y
sudo apt-get install ocl-icd-opencl-dev -y
sudo apt install build-essential cmake git libuv1-dev libssl-dev libhwloc-dev libmicrohttpd-dev lm-sensors htop clinfo -y
fi
}

function build_miner {
#gnome-terminal -- htop
cd ~
if [ ! -d miners ];then mkdir miners;fi
cd ~/miners
echo ""
git clone https://github.com/tpruvot/ccminer.git && echo ""
git clone https://github.com/tpruvot/cpuminer-multi.git && echo ""
git clone https://github.com/xmrig/xmrig.git && echo ""
if [ $Mode = "NVIDIAGTX" ] || [ $Mode = "NVIDIARTX" ]
then
git clone https://github.com/xmrig/xmrig-nvidia.git && echo ""
fi
git clone https://github.com/uPlexa/xmrig-upx.git && echo ""
git clone https://github.com/fireice-uk/xmr-stak.git && echo ""
git clone https://github.com/fireice-uk/xmr-stak.git -b xmr-stak-rx xmr-stak-rx  && echo ""
if [ $Mode = "AMD" ]
then
git clone https://github.com/xmrig/xmrig-amd.git && echo ""
fi
cd ~
tar xvfJ miners.tar.xz
rm miners.tar.xz
cd ~/miners
wget -c https://download.open-mpi.org/release/hwloc/v2.5/hwloc-2.5.0.tar.bz2
tar xvfj hwloc-2.5.0.tar.bz2
cd hwloc-2.5.0
./configure
make -j$(nproc)
sudo make install
cd ..
rm -r hwloc-2.5.0
rm hwloc-2.5.0.tar.bz2
cd xmrig
cd build
cmake ..
make -j$(nproc)
cd ~/miners
if [ $Mode = "NVIDIAGTX" ] || [ $Mode = "NVIDIARTX" ]
then
cd xmrig-nvidia
cd build
cmake .. -DCMAKE_C_COMPILER=gcc-8 -DCMAKE_CXX_COMPILER=g++-8
make -j$(nproc)
fi
cd ~/miners
cd xmrig-upx
cd build
cmake ..
make -j$(nproc)
cd ~/miners
cd xmr-stak
mkdir build
cd build
cmake .. -DCMAKE_C_COMPILER=gcc-8 -DCMAKE_CXX_COMPILER=g++-8
make -j$(nproc)
cd ~/miners
cd cpuminer-multi
./build.sh
if [ $Mode = "AMD" ]
then
cd ~/miners/xmrig-amd
mkdir build
cd build
cmake ..
make -j$(nproc)
cd ~
fi
}

function makesud_ {
if ! sudo grep -q "wareck ALL=(ALL) NOPASSWD: ALL" /etc/sudoers
then
sudo cp /etc/sudoers /tmp/
sudo chmod 777 /tmp/sudoers
cat <<"EOF">> /tmp/sudoers
wareck ALL=(ALL) NOPASSWD: ALL
EOF
sudo chmod 0644 /tmp/sudoers
sudo cp /tmp/sudoers /etc/
fi
cat <<'EOF'>> wakeup_cube.sh
sudo wakeonlan -i 192.168.1.100 D0:50:99:56:32:C8
EOF
chmod +x wakeup_cube.sh
sudo mv wakeup_cube.sh /usr/local/bin
}

function liquidcontrol {
if [ $Mode = "NVIDIAGTX" ] || [ $Mode = "NVIDIARTX" ]
then
sudo pip3 install liquidctl
fi
if [ $Mode = "NVIDIAGTX" ]
then
cat <<'EOF'>> liquidcfg.service
[Unit]
Description=AIO startup service

[Service]
Type=oneshot
ExecStart=liquidctl initialize --match "smart device"
ExecStart=liquidctl --match "smart device" set sync speed 55
ExecStart=liquidctl --match "smart" set fan1 speed 90
ExecStart=liquidctl --match "smart" set fan2 speed 90
ExecStart=liquidctl --match "smart" set fan3 speed 90
ExecStart=liquidctl --match "smart" set led color fixed eb0869

[Install]
WantedBy=default.target
EOF
sudo cp liquidcfg.service /etc/systemd/system/
rm liquidcfg.service
sudo systemctl daemon-reload
sudo systemctl start liquidcfg
sudo systemctl enable liquidcfg
gsettings set org.gnome.desktop.background picture-uri /home/wareck/Images/wallpaperNvidia.jpg
gsettings set org.gnome.shell.extensions.dash-to-dock dash-max-icon-size 32
fi

if [ $Mode = "NVIDIARTX" ]
then
cat <<'EOF'>> liquidcfg.service
[Unit]
Description=AIO startup service

[Service]
Type=oneshot
ExecStart=liquidctl initialize --match "smart device"
ExecStart=liquidctl --match "smart device" set sync speed 55
ExecStart=liquidctl --match "smart" set fan1 speed 90
ExecStart=liquidctl --match "smart" set fan2 speed 90
ExecStart=liquidctl --match "smart" set fan3 speed 90
ExecStart=liquidctl --match "smart" set led color fixed f32a9e

[Install]
WantedBy=default.target
EOF
sudo cp liquidcfg.service /etc/systemd/system/
rm liquidcfg.service
sudo systemctl daemon-reload
sudo systemctl start liquidcfg
sudo systemctl enable liquidcfg
gsettings set org.gnome.desktop.background picture-uri /home/wareck/Images/875469.png
gsettings set org.gnome.shell.extensions.dash-to-dock dash-max-icon-size 32
fi
if [ $Mode = "AMD" ]
then
gsettings set org.gnome.desktop.background picture-uri /home/wareck/Images/wallpaperAMD.jpg
gsettings set org.gnome.shell.extensions.dash-to-dock dash-max-icon-size 32
fi
}

function intro {
clear
print_centered "$(tput bold)$(tput setaf 6)"
print_centered "▄▄▄▄·  ▄▄▄·  ▄▄· ▄ •▄ ▄• ▄▌ ▄▄▄·    "
print_centered "▐█ ▀█▪▐█ ▀█ ▐█ ▌▪█▌▄▌▪█▪██▌▐█ ▄█    "
print_centered "▐█▀▀█▄▄█▀▀█ ██ ▄▄▐▀▀▄·█▌▐█▌ ██▀·    "
print_centered "██▄▪▐█▐█ ▪▐▌▐███▌▐█.█▌▐█▄█▌▐█▪·•    "
print_centered "·▀▀▀▀  ▀  ▀ ·▀▀▀ ·▀  ▀ ▀▀▀ .▀       "
print_centered "▄▄▄▄· ▄• ▄▌▪  ▄▄▌  ·▄▄▄▄  ▄▄▄ .▄▄▄  "
print_centered "▐█ ▀█▪█▪██▌██ ██•  ██▪ ██ ▀▄.▀·▀▄ █·"
print_centered "▐█▀▀█▄█▌▐█▌▐█·██▪  ▐█· ▐█▌▐▀▀▪▄▐▀▀▄ "
print_centered "██▄▪▐█▐█▄█▌▐█▌▐█▌▐▌██. ██ ▐█▄▄▌▐█•█▌"
print_centered "·▀▀▀▀  ▀▀▀ ▀▀▀.▀▀▀ ▀▀▀▀▀•  ▀▀▀ .▀  ▀"
print_centered "$(tput setaf 7)$(tput bold)"
print_centered "Autoconfig Ubuntu pour le backup des Users"
if [ $Mode = "NVIDIAGTX" ]
then
print_centered "NVIDIA-GTX + NZXT + windows 10"
fi
if [ $Mode = "NVIDIARTX" ]
then
print_centered "NVIDIA-RTX + NZXT + windows 10"
fi
if [ $Mode = "AMD" ]
then
print_centered "AMD RX580 + windows 10"
fi
echo "$(tput setaf 7)"
sleep 4
}

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 password_hack {
cd /home/wareck/
git clone https://github.com/hashcat/hashcat.git
cd /home/wareck/hashcat
make -j$(nproc)
cd /home/wareck/
tar xvfJ hashcat.tar.xz
rm /home/wareck/hashcat.tar.xz
cd /home/wareck/
echo -e ""
git clone https://github.com/openwall/john.git
cd john
sudo apt-get -y install git build-essential libssl-dev zlib1g-dev
sudo apt-get -y install yasm pkg-config libgmp-dev libpcap-dev libbz2-dev clang
cd src
./configure
make -s clean
make -j$(nproc)
}

function outro {
print_centered "$(tput setaf 3)$(tput bold)"
print_centered "Système pret, reboot recommandé !"
echo "$(tput setaf 7)"
touch /home/wareck/.pass1
}

intro
update_one
liquidcontrol
sleep 1
update_two
makesud_
build_miner
password_hack
outro
