#!/bin/bash
set -e
Version=9.5
Cuda=NO
ZFS=NO
Drivers=AMD
kernel_custom=YES
Webmin="webmin-2.021"
kernel_dkms="5.15.0-69-generic"

if (( $EUID != 0 )); then
   echo -e "$(tput setaf 9)This must be run as root. Try 'sudo $0'.$(tput setaf 7)"
   exit 1
fi
clear

function ubuntu_version_check {
if [[ $(lsb_release -rs) == "20.04" ]]
then

      Ubuntu_version="20.04.5"

else
       echo "Non-compatible version"
       exit 0
fi
}

function kernel_check_ {
if [ $Drivers="AMD" ]
then
	unamestr=$(uname -r)
case $kernel_custom in
NO)
	if [[ "$unamestr" == '5.15.0-69-generic' ]]
	then
	echo -e "$(tput setaf 10)DKMS Kernel install :$(tput setaf 7)"
	#if ! [ -f backup_boot.tar.gz ]
	#then
	#sudo tar cvfz backup_boot.tar.gz /boot/
	sudo rm /boot/config-5.15*
	sudo rm /boot/initrd.img-5.15*
	sudo rm /boot/System.map-5.15*
	sudo rm /boot/vmlinuz-5.15*
	#fi
	sudo apt-get install linux-image-5.4.0-54-generic linux-headers-5.4.0-54-generic linux-modules-extra-5.4.0-54-generic htop -y
	sudo update-grub2
        sudo apt-mark hold linux-image-5.4.0-54-generic linux-headers-5.4.0-54-generic linux-modules-extra-5.4.0-54-generic
        autoroot_
        decompte_
	fi
	if [[ "$unamestr" == '5.4.0-139-generic' ]]
	then
	sudo rm /boot/config-5.4.0-139-generic | true
        sudo rm /boot/System.map-5.4.0-139-generic | true
        sudo rm /boot/initrd.img-5.4.0-139-generic | true
        sudo rm /boot/vmlinuz-5.4.0-139-generic | true
        sudo update-grub2
        sudo apt-mark hold linux-image-$kernel_dkms linux-headers-$kernel_dkms linux-modules-extra-$kernel_dkms
        autoroot_
        decompte_
	fi
	;;
YES)
	if ! [[ "$unamestr" == '5.16.20-custom' ]]
	then
	echo -e "$(tput setaf 7)Custom kernel install (fixed DKMS and WOL) :$(tput setaf 7)"
	cd /tmp/
	wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/custom_kernel.tar.xz
	tar xvfJ custom_kernel.tar.xz
	cd /tmp/custom_kernel
	sudo dpkg -i linux-libc-dev_5.16.20-custom-1_amd64.deb
	sudo dpkg -i linux-headers-5.16.20-custom_5.16.20-custom-1_amd64.deb
	sudo dpkg -i linux-image-5.16.20-custom_5.16.20-custom-1_amd64.deb
	autoroot_
	decompte_
	fi
	;;
esac
fi
}

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 " ██████╗██╗   ██╗██████╗ ███████╗"
print_centered "██╔════╝██║   ██║██╔══██╗██╔════╝"
print_centered "██║     ██║   ██║██████╔╝█████╗  "
print_centered "██║     ██║   ██║██╔══██╗██╔══╝  "
print_centered "╚██████╗╚██████╔╝██████╔╝███████╗"
print_centered " ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝"
echo -n "$(tput setaf 7)"
print_centered "Cube Server installation script v$Version"
print_centered "Ubuntu version $(lsb_release -rs)"
sleep 1
}

function update_ {
if [ -f /home/wareck/.pass2 ] && [ ! -f /home/wareck/.pass3 ] && [ ! -f /home/wareck/.pass4 ]
then
echo -e "$(tput setaf 10)\nUpdate and install packages:$(tput sgr0)"
sudo apt-get update
echo -e ""
sudo apt-get install lxde lxde-icon-theme -y
echo -e ""
sudo apt-get install  checkinstall samba cifs-utils mdadm git build-essential libtool pkg-config automake autoconf ethtool net-tools openvpn filezilla \
tasksel lzma-dev lzma cmake libuv1-dev libssl-dev msr-tools tightvncserver lm-sensors openssh-server -y
echo -e ""
sudo apt-get install pixz pbzip2 pigz btrfs-progs gparted gpart gnome-remote-desktop libcanberra-gtk-module \
ctorrent libcurl4-openssl-dev bison byacc screen libudev-dev libjansson-dev libncurses5-dev libmicrohttpd-dev curl -y
echo -e ""
sudo apt-get install libglib2.0-dev libgtk2.0-dev libsensors4-dev htop vlc locate -y
echo -e ""
if [ $ZFS = "YES" ]
then
sudo apt-get install zfsutils-linux -y
fi
if [ $Cuda = "YES" ]
then
echo -e "$(tput setaf 10)\nInstall Cuda packages:$(tput sgr0)"
sudo apt-get install nvidia-cuda-toolkit -y
echo -e "Done."
fi
fi
}

function decompte_ {
echo -e "$(tput setaf 10)\nReboot in 10 seconds (CRTL+C to abord):$(tput sgr0)"
for i in {10..0}
do
echo -e -n "$i "
sleep 1
done
echo ""
sudo reboot
}

function nvidia_driver_ {
if ! [ -f /home/wareck/.pass1 ]
then
	if [ $Drivers = "NVIDIA" ]
	then
		if ! [ -f /etc/modprobe.d/blacklist-nvidia-nouveau.conf ]
		then
			sudo apt-get install build-essential libglvnd-dev -y
			sudo bash -c "echo blacklist nouveau > /etc/modprobe.d/blacklist-nvidia-nouveau.conf"
			sudo bash -c "echo options nouveau modeset=0 >> /etc/modprobe.d/blacklist-nvidia-nouveau.conf"
			sudo update-initramfs -u
			touch /home/wareck/.pass1
			decompte_
		fi
	fi
	if [ $Drivers = "AMD" ]
	then
	touch ~/.pass1
	fi
fi

if ! [ -f /home/wareck/.pass2 ]
then
sleep 2

case $Drivers in
NVIDIA)
		echo -e "$(tput setaf 10)\nInstall NVIDIA GT710 Drivers 470.$Drivers :$(tput sgr0)"
                cd /home/wareck/
                echo -e "Download MegaDownload:"
                git clone https://github.com/wareck/megadown.git
                sudo cp /home/wareck/megadown/megadown /usr/local/bin/megadown
                sudo rm -r /home/wareck/megadown
                cd /home/wareck
                echo -e ""
                echo -e "Install drivers:"
                sudo apt-get install nvidia-driver-470 -y
                touch /home/wareck/.pass2
                decompte_
;;
AMD)
		echo -e "$(tput setaf 10)\nInstall AMD Drivers :$(tput sgr0)"
                cd /home/wareck
                echo -e ""
                echo -e "Install drivers:"
		wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/amdgpu-install_5.4.50403-1_all.deb -O /home/wareck/Build_Cube/pack/amdgpu-install_5.4.50403-1_all.deb
		#wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/amdgpu-install_22.20.50200-1_all.deb -O /home/wareck/Build_Cube/pack/amdgpu-install_22.20.50200-1_all.deb
		wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/linux-firmware.tar.xz
		tar xfJ 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_5.4.50403-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 usermod -a -G video $LOGNAME
		touch /home/wareck/.pass2
		sudo apt-get autoremove -y
		decompte_
;;
*)
echo -e "$(tput setaf 9)Error driver number ! $(tput sgr0)\n"
exit 0
;;
esac
fi
}

function download_packs_ {
if [ ! -f /home/wareck/.pass3 ]
then
echo -e "$(tput setaf 10)\nDownload complement packs:$(tput sgr0)"
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/cgminer.conf -O /home/wareck/Build_Cube/pack/cgminer.conf
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/chia-miner.tar.xz -O /home/wareck/Build_Cube/pack/chia-miner.tar.xz
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/gerbera.tar.xz -O /home/wareck/Build_Cube/pack/gerbera.tar.xz
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/start_cgminer.tar.xz -O /home/wareck/Build_Cube/pack/start_cgminer.tar.xz
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/$Webmin.tar.xz -O /home/wareck/Build_Cube/pack/$Webmin.tar.xz
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/xmrig.tar.xz -O /home/wareck/Build_Cube/pack/xmrig.tar.xz
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/zfsmanager-0.0.4.wbm.gz -O /home/wareck/Build_Cube/pack/zfsmanager-0.0.4.wbm.gz
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/gpu-miners.tar.xz -O /home/wareck/Build_Cube/pack/gpu-miners.tar.xz
if [ $Drivers = "AMD" ]
then
cp /home/wareck/Build_Cube/pack/gpu-miners.tar.xz /home/wareck
fi
cp /home/wareck/Build_Cube/pack/pack1.tar.xz /home/wareck
cp /home/wareck/Build_Cube/pack/pack2.tar.xz /home/wareck
cp /home/wareck/Build_Cube/pack/xmrig.tar.xz /home/wareck
cp /home/wareck/Build_Cube/pack/start_cgminer.tar.xz /home/wareck
echo "Done."
if  [ ! -d /home/wareck/$Webmin ]
then
cp /home/wareck/Build_Cube/pack/$Webmin.tar.xz /home/wareck/
cd /home/wareck/
echo -e "$(tput setaf 10)\nWebmin uncompress:$(tput sgr0)"
tar xfJ $Webmin.tar.xz
rm $Webmin.tar.xz
echo -e "Done"
fi

echo -e "$(tput setaf 10)\nDownloading miners:$(tput sgr0)"
cd /home/wareck
if ! [ -d miners ]; then mkdir miners;fi

cd /home/wareck/miners
if ! [ -d xmrig ]
then
git clone https://github.com/xmrig/xmrig.git
echo -e ""
fi
if ! [ -d xmr-stak ]
then
git clone https://github.com/fireice-uk/xmr-stak.git
echo -e ""
fi
if ! [ -d xmr-stak-rx ]
then
git clone https://github.com/fireice-uk/xmr-stak.git -b xmr-stak-rx xmr-stak-rx
echo -e ""
fi
if ! [ -d cgminer-gekko ]
then
git clone https://github.com/wareck/cgminer-gekko.git
echo -e ""
fi
if ! [ -d cgminer-lketc ]
then
git clone https://github.com/wareck/cgminer-lketc.git
fi

fi
echo "Done."
}

function build_miners_ {
if [ ! -f /home/wareck/.pass3 ]
then
if [ $Drivers = "AMD" ]
then
cd /home/wareck
tar xfJ gpu-miners.tar.xz
fi
cd /home/wareck/miners
echo -e "$(tput setaf 10)\nBuild Cgminer-gekko:$(tput sgr0)"
cd cgminer-gekko
sudo usermod -a -G dialout,plugdev wareck
sudo cp 01-cgminer.rules /etc/udev/rules.d/
CFLAGS="-O2 -march=native" ./autogen.sh
./configure --enable-gekko
make -j4
tar xfJ /home/wareck/start_cgminer.tar.xz
rm /home/wareck/start_cgminer.tar.xz
cd ..
cat <<'EOF'>> /tmp/cgminer
#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          cgminer
# Required-Start:    networking
# Default-Start:     3 4 5
# Default-Stop:      0 6
### END INIT INFO
case "$1" in
        start)
        cd /home/wareck/miners/cgminer-gekko/
        su wareck -c "screen -dmS cgminer /home/wareck/miners/cgminer-gekko/cgminer"
        ;;
        stop)
        sudo killall -9 cgminer | true
        ;;
        *)
        echo "Usage: /etc/init.d/cgminer {start|stop}"
        exit 1
        ;;
esac

exit 0
EOF
chmod +x /tmp/cgminer
sudo cp /tmp/cgminer /etc/init.d/cgminer
sudo update-rc.d cgminer defaults
mkdir /home/wareck/.cgminer
cat <<'EOF'>> /tmp/cgminer.conf
{
"pools" : [
        {
                "url" : "stratum+tcp://stratum.braiins.com:3333",
                "user" : "wareck.gekko",
                "pass" : "x"
        }
]
,
"api-description" : "cgminer 4.12.0-wrk",
"api-mcast-addr" : "224.0.0.75",
"api-mcast-code" : "FTW",
"api-mcast-des" : "",
"api-mcast-port" : "4028",
"api-port" : "4028",
"api-host" : "0.0.0.0",
"gekko-lowboost" : true,
"gekko-2pac-freq" : "155.0",
"gekko-compac-freq" : "150.0",
"gekko-tune-down" : "95.0",
"gekko-tune-up" : "97.0",
"gekko-wait-factor" : "0.5",
"gekko-bauddiv" : "0",
"gekko-newpac-freq" : "100",
"gekko-start-freq" : "100",
"gekko-step-freq" : "6.2",
"gekko-step-delay" : "15",
"gekko-tune2" : "0",
"fallback-time" : "120",
"hotplug" : "5",
"log" : "5",
"shares" : "0",
"suggest-diff" : "0"
}
EOF
cp /tmp/cgminer.conf /home/wareck/.cgminer/
echo "Done."



cd /home/wareck/miners
echo -e "$(tput setaf 10)\nBuild Cgminer-lketc:$(tput sgr0)"
cd cgminer-lketc
CFLAGS="-O2 -march=native" ./autogen.sh
./configure
make -j4
cd ..
echo "Done."


if ! grep -q "modded" /usr/include/CL/cl_version.h
then
sudo cp /usr/include/CL/cl_version.h /home/wareck/
echo "/*modded*/" > /home/wareck/cl_version_m.h
echo "#define CL_TARGET_OPENCL_VERSION 220" >> /home/wareck/cl_version_m.h
cat /home/wareck/cl_version.h >> /home/wareck/cl_version_m.h
rm /home/wareck/cl_version.h
mv /home/wareck/cl_version_m.h /home/wareck/cl_version.h
sudo cp /home/wareck/cl_version.h /usr/include/CL/cl_version.h
sudo rm /home/wareck/cl_version.h
fi

echo -e "$(tput setaf 10)\nUncompress xmr files:$(tput sgr0)"
tar xfJ /home/wareck/xmrig.tar.xz --checkpoint=.100
echo "Done."
echo -n -e "\n"
echo -e "$(tput setaf 10)\nBuild xmrig:$(tput sgr0)"
cd xmrig
git config --global --add safe.directory /home/wareck/miners/xmrig
git pull
sed -i "s/kDefaultDonateLevel = 5;/kDefaultDonateLevel = 0;/g" src/donate.h
sed -i "s/kMinimumDonateLevel = 1;/kMinimumDonateLevel = 0;/g" src/donate.h
wget -c https://download.open-mpi.org/release/hwloc/v2.7/hwloc-2.7.1.tar.bz2
tar xfj hwloc-2.7.1.tar.bz2 --checkpoint=.100
cd hwloc-2.7.1
./configure
make -j4
sudo make install
cd ..
sudo ldconfig
cd build
cmake ..
make -j4
cd ..
cd ..
echo "Done."

#echo -e "$(tput setaf 10)\nBuild xmrig-upx:$(tput sgr0)"
#cd xmrig-upx
#git config --global --add safe.directory /home/wareck/miners/xmrig-upx
#git pull
#sed -i "s/kDefaultDonateLevel = 5;/kDefaultDonateLevel = 0;/g" src/donate.h
#sed -i "s/kMinimumDonateLevel = 1;/kMinimumDonateLevel = 0;/g" src/donate.h
#cd build
#cmake .. -DWITH_HTTPD=OFF
#make -j4
#cd ..
#cd ..
#echo "Done."
rm -r -f xmrig-upx

echo -e "$(tput setaf 10)\nBuild xmr-stak-rx:$(tput sgr0)"
cd xmr-stak-rx
git config --global --add safe.directory /home/wareck/miners/xmr-stak-rx
git pull
cd build
cmake ..
make -j4
cd ..
cd ..
echo "Done."
echo -e "$(tput setaf 10)\nBuild xmr-stak:$(tput sgr0)"
cd xmr-stak
if ! [ -d build ];then mkdir build ;fi
git config --global --add safe.directory /home/wareck/miners/xmr-stak
git pull
cd build
if [ $Drivers = "AMD" ]
then
cmake .. -DCUDA_ENABLE=OFF
else
cmake .. -DOpenCL_ENABLE=OFF -DCUDA_ENABLE=OFF
fi
make -j4
cd ..
cd ..
echo "Done."

if [ $Cuda = "YES" ]
then
echo -e "$(tput setaf 10)\nBuild xmrig-cuda:$(tput sgr0)"
sudo apt-get install git build-essential cmake libuv1-dev nvidia-cuda-dev nvidia-cuda-toolkit libmicrohttpd-dev gcc-8 g++-8
git clone https://github.com/xmrig/xmrig-nvidia.git
cd xmrig-nvidia
mkdir build
cd build
cmake .. -DCMAKE_C_COMPILER=$(which gcc-8) -DCMAKE_CXX_COMPILER=$(which g++-8)
make -j4
fi

echo "Done"
touch /home/wareck/.pass3
fi
sudo chmod -R 775 /home/wareck/miners
}

function configure_ {
if [ ! -f /home/wareck/.pass4 ]
then
echo -e "$(tput setaf 10)\nlxPanel sensors:$(tput sgr0)"
cd /home/wareck
if ! [ -d sensors-lxpanel-plugin ]
then
git clone https://github.com/danamlund/sensors-lxpanel-plugin.git
fi
cd /home/wareck
cd sensors-lxpanel-plugin
make
sudo make install
sudo rm -r /home/wareck/sensors-lxpanel-plugin
echo "Done."

echo -e "$(tput setaf 10)\nConfig files:$(tput sgr0)"
cd /home/wareck/
zip="['firefox.desktop', 'thunderbird.desktop', 'org.remmina.Remmina.desktop', 'filezilla.desktop', 'org.gnome.Nautilus.desktop', 'org.gnome.Terminal.desktop']"
cat <<'EOF'>> zap
#!/bin/bash
gsettings set org.gnome.shell.extensions.dash-to-dock dash-max-icon-size 32
gsettings set org.gnome.desktop.background picture-uri /home/wareck/Images/mr-robot-wallpapers.jpg
gsettings set org.gnome.shell favorite-apps "XXX"
#su wareck -c 'gsettings set org.gnome.shell.extensions.dash-to-dock show-mounts false'
dconf load /org/gnome/terminal/ < /usr/local/bin/zap2
EOF
sed -i "s/XXX/$zip/g" zap

UUID=$(gsettings get org.gnome.Terminal.ProfilesList default | tr -d \')
cat <<'EOF'>> zap2
[legacy/profiles:/:XXX]
default-size-columns=110
font='Monospace 9'
use-system-font=false
use-theme-colors=false
EOF
sed -i "s/XXX/$UUID/g" zap2
chmod +x zap
sudo cp zap /usr/local/bin
sudo cp zap2 /usr/local/bin
sudo rm zap
sudo rm zap2
sleep 1

sudo rm -r -f /etc/update-motd.d/*
tar xfJ pack1.tar.xz --checkpoint=.10
echo -n -e "\n"
sudo cp /home/wareck/pack2.tar.xz /
cd /
sudo tar xfJ pack2.tar.xz --checkpoint=.10
echo -n -e "\n"
cd /home/wareck

#clear
dynmotd

sudo sed -i -e "s/# set constantshow/set constantshow/g" /etc/nanorc

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

echo -e "$(tput setaf 10)\nBuild folder and mount drives:$(tput sgr0)"
if ! [ -r /media/Cloud ];then sudo mkdir /media/Cloud && cd / & sudo ln -s /media/Cloud /Cloud ;fi
if ! [ -r /media/Store ];then sudo mkdir /media/Store && cd / & sudo ln -s /media/Store /Store;fi
if ! [ -r /media/Partage ];then sudo mkdir /media/Partage && cd / & sudo ln -s /media/Partage /Partage;fi
if ! [ -r /media/Nas ];then sudo mkdir /media/Nas && cd / & sudo ln -s /media/Nas /Nas;fi
if ! [ -r /media/Nvme0 ];then sudo mkdir /media/Nvme0 && cd / & sudo ln -s /media/Nvme0 /Nvme0 ;fi
if ! [ -r /media/Nvme1 ];then sudo mkdir /media/Nvme1 && cd / & sudo ln -s /media/Nvme1 /Nvme1 ;fi
if ! [ -r /Chia ];then sudo mkdir /Chia;fi
if ! [ -r /Chia/Chia1 ];then sudo mkdir /Chia/Chia1;fi
if ! [ -r /Chia/Chia2 ];then sudo mkdir /Chia/Chia2;fi
if ! [ -r /Chia/Chia3 ];then sudo mkdir /Chia/Chia3;fi
if ! grep "UUID=UUID=1df72b63-fca0-45c6-992c-3e2532d5591a /media/Store ext4" /etc/fstab >/dev/null
then
cp /etc/fstab /tmp/fstab
cat <<"EOF">> /tmp/fstab
#ext4
UUID=6fc06d70-1a38-4706-ae83-0a329181b9f7 /media/Cloud ext4
UUID=1df72b63-fca0-45c6-992c-3e2532d5591a /media/Store ext4
UUID=9a837a47-aa05-4ca5-a2e1-2838e53bb9b7 /media/Partage ext4
#btrfs
UUID=4d5ed5b8-6bb3-4d15-9c31-a29ab9e87411 /media/Nvme0 btrfs ssd,nodatacow,noatime
UUID=147589ac-fda7-4542-9580-0a3120b9129a /media/Nvme1 btrfs ssd,nodatacow,noatime
#Chia drive
UUID=15edb9ad-b099-476f-97b0-5855fc5b6fb7 /Chia/Chia1 ext4 auto,nofail,noatime,rw,user    0   0
UUID=66be44f6-f8e9-473c-9b71-db849f870099 /Chia/Chia2 ext4 auto,nofail,noatime,rw,user    0   0
EOF
sudo cp /tmp/fstab /etc/fstab
fi

sudo rm /etc/hosts
sudo cat <<'EOF'>> /etc/hosts
127.0.0.1       localhost
127.0.1.1       cube
192.168.1.8     miniserver miniserver gogserver.dnsalias.com
192.168.1.10    nzxt
192.168.1.11    garage
192.168.1.254   mafreebox.freebox.fr
192.168.1.200   node

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF
echo "Done."

echo -e "$(tput setaf 10)\nSensors drivers:$(tput sgr0)"
if ! grep "nct6775" /etc/modules >/dev/null
then
cp /etc/modules /tmp/modules
cat <<"EOF">> /tmp/modules

# Chip drivers
nct6775
EOF
sudo cp /tmp/modules /etc/modules
fi

if [ -f /tmp/sensors3.conf ]
then
chmod 777 /tmp/sensors3.conf
rm /tmp/sensors3.conf
fi
cat <<'EOF'>> /tmp/sensors3.conf
chip "fam15h_power-pci-00c4"
ignore power1

chip "amdgpu-pci-0200"
label fan1 "GPU Fan"

chip "k10temp-pci-00c3"
ignore temp1

chip "nct6776-isa-0290"
label in0 "Vcore"
label in2 "AVCC"
label in3 "+3.3V"
label in7 "3VSB"
label in8 "Vbat"
set in2_min  3.3 * 0.90
set in2_max  3.3 * 1.10
set in3_min  3.3 * 0.90
set in3_max  3.3 * 1.10
set in7_min  3.3 * 0.90
set in7_max  3.3 * 1.10
set in8_min  3.0 * 0.90
set in8_max  3.3 * 1.10
set temp1_max 65.000
set temp1_max_hyst 60.000
ignore fan1
ignore fan2
ignore fan5
ignore fan4
ignore intrusion0
ignore intrusion1
ignore beep_enable
ignore temp10
ignore temp9
ignore temp8
ignore temp7
ignore temp3
set fan3_min 100.000
set fan3_max 3000.000
ignore in1
ignore in4
ignore in5
ignore in6
label fan3  "CPU Fan"
label temp1 "M/B Temp"
label temp2 "CPU Temp"
label temp2_crit  "CPU Crit"
EOF
chmod 644 /tmp/sensors3.conf
sudo cp /tmp/sensors3.conf /etc/
echo "Done."

echo -e "$(tput setaf 10)\nVnc init:$(tput sgr0)"
sudo update-rc.d vncserver defaults
echo "Done."

echo -e "$(tput setaf 10)\nOpening ports:$(tput sgr0)"
sudo ufw allow 8333 >/dev/null
sudo ufw allow 22 >/dev/null
sudo ufw allow 5900 >/dev/null
sudo ufw allow 10000 >/dev/null
sudo ufw allow 139 >/dev/null
sudo ufw allow 5901 >/dev/null
sudo ufw allow 5902 >/dev/null
sudo ufw allow 445 >/dev/null
echo "Done."

echo -e "$(tput setaf 10)\nBashrc mod:$(tput sgr0)"
if ! grep "#mod for tightvncserver" /home/wareck/.bashrc
then
echo "#mod for tightvncserver" >>/home/wareck/.bashrc
echo "cd ~" >>/home/wareck/.bashrc
fi
echo "Done."

echo -e "$(tput setaf 10)\nDisable IPv6:$(tput sgr0)"
if ! grep "ipv6.disable=1" /etc/default/grub >/dev/null
then
sed -i -e "s/quiet splash/quiet splash ipv6.disable=1/g" /etc/default/grub
sed -i -e "s/GRUB_CMDLINE_LINUX=\"/GRUB_CMDLINE_LINUX=\"ipv6.disable=1 acpi_enforce_resources=lax/g" /etc/default/grub
sudo update-grub2
fi
sudo chown -R wareck:wareck /home/wareck/miners/xmr*
sudo chown -R wareck:wareck /home/wareck/miners/cgminer*
sudo apt-get remove light-locker* -y
if [ ! -d /home/wareck/.config/lxpanel/LXDE/panels ]
then
mkdir /home/wareck/.config/lxpanel
mkdir /home/wareck/.config/lxpanel/LXDE
mkdir /home/wareck/.config/lxpanel/LXDE/panels
fi
sudo cp /home/wareck/Build_Cube/pack/panel /home/wareck/.config/lxpanel/LXDE/panels/panel
sudo chmod 777 /home/wareck/.config/lxpanel/LXDE/panels/panel
if [ ! -d  /home/wareck/.config/lxterminal ]
then
mkdir /home/wareck/.config/lxterminal
fi
sudo cp /home/wareck/Build_Cube/pack/lxterminal.conf /home/wareck/.config/lxterminal/
sudo chmod 777 /home/wareck/.config/lxterminal/lxterminal.conf

echo "Done."
touch /home/wareck/.pass4
decompte_
fi
}

function printer_ {
if [ ! -f /home/wareck/.pass3 ]
then
if [ ! -d /usr/local/share/foomatic/ ]
then
if [ ! -d /tmp/foomatic-db/ ]
then
cd /tmp/
echo -e "$(tput setaf 10)\nPrinter Drivers$(tput sgr0)"
git clone https://github.com/OpenPrinting/foomatic-db.git
fi
cd /tmp/foomatic-db
if [ ! -d /usr/local/share/foomatic/ ]
then
./make_configure
./configure
sudo make install
echo "Done."
fi
fi
fi
}

function mounting_drives_ {
if [ ! -f /home/wareck/.pass4 ]
then
echo -e "$(tput setaf 10)\nMounting Drives$(tput sgr0)"
if [ $ZFS = "YES" ]
then
if findmnt /media/Cloud/ >/dev/null 2>&1
then
sudo zpool export Cloud
else
sudo zpool import Cloud
fi
fi
if ! findmnt /media/Partage >/dev/null ; then sudo mount /media/Partage ;fi
if ! findmnt /media/Store >/dev/null ; then sudo mount /media/Store ;fi
sudo chmod 777 /media/Store
sudo chmod 777 /media/Partage
sudo chmod 777 /media/nvme
sudo chmod 777 /media/Nas
echo "Done."
fi
}

function cleaning_ {
if [ -f /home/wareck/.pass4 ]
then
echo -e "$(tput setaf 10)\nCleaning install files:$(tput sgr0)"
if [ -f /pack2.tar.xz ]; then sudo rm /pack2.tar.xz ;fi
if [ -f /home/wareck/pack1.tar.xz ]; then rm /home/wareck/pack1.tar.xz ;fi
if [ -f /home/wareck/pack2.tar.xz ]; then rm /home/wareck/pack2.tar.xz ;fi
if [ -f /home/wareck/xmrig.tar.xz ]; then rm /home/wareck/xmrig.tar.xz ;fi
if [ -f /home/wareck/gpu-miners.tar.xz ]; then rm /home/wareck/gpu-miners.tar.xz;fi
if [ -r /home/wareck/Music ]; then sudo rm -r -f /home/wareck/Music ;fi
if [ -r /home/wareck/Public ]; then sudo rm -r -f /home/wareck/Public ;fi
if [ -r /home/wareck/Templates ]; then sudo rm -r -f /home/wareck/Templates ;fi
if [ -r /home/wareck/Vidéos ]; then sudo rm -r -f /home/wareck/Vidéos ;fi
if [ -f /home/wareck/NVIDIA-Linux-x86_64-470.74.run ]; then rm /home/wareck/NVIDIA-Linux-x86_64-470.74.run ;fi
if [ -f /home/wareck/NVIDIA-Linux-x86_64-470.82.00.run ]; then rm /home/wareck/NVIDIA-Linux-x86_64-470.82.00.run ;fi
if [ -f /home/wareck/.pass1 ]; then rm /home/wareck/.pass1 ;fi
if [ -f /home/wareck/.pass2 ]; then rm /home/wareck/.pass2 ;fi
if [ -f /home/wareck/.pass3 ]; then rm /home/wareck/.pass3 ;fi
if [ -f /home/wareck/.pass4 ]; then rm /home/wareck/.pass4 ;fi
if [ -f /usr/local/bin/zap ]; then sudo rm /usr/local/bin/zap* ;fi
if [ -f /home/wareck/.config/autostart/install_os.desktop ];then sudo rm /home/wareck/.config/autostart/install_os.desktop;fi
if [ -f /home/wareck/.config/autostart/zappy.desktop ];then sudo rm /home/wareck/.config/autostart/zappy.desktop;fi
if [ -r /lib/modules/5.15.0-60-generic ]; then sudo rm -r /lib/modules/5.15.0-60-generic ;fi
if [ -r /lib/modules/5.15.0-46-generic ]; then sudo rm -r /lib/modules/5.15.0-46-generic ;fi
cd /home/wareck/Build_Cube/pack
sudo chown -R wareck:wareck /home/wareck/miners
sudo chmod -R 775 /home/wareck/miners
./clean.sh
echo -e "Done. \n"

echo `date` "install/update" >/home/wareck/.buildcube
fi
sudo apt-get autoremove -y
sleep 1
}

function autoroot_ {
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/install_os.desktop ]
then
cat <<'EOF'>> /home/wareck/.config/autostart/install_os.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/Build_Cube/build_cube.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/install_os.desktop
fi

if [ ! -f /home/wareck/.config/autostart/zappy.desktop ]
then
cat <<'EOF'>> /home/wareck/.config/autostart/zappy.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 "/usr/local/bin/zap"
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/zappy.desktop
fi
if ! grep "wareck" /etc/sudoers >/dev/null
then
sudo sh -c "echo \"wareck ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers"
fi
}

function remove_screensaver {
#remove screensaver
cat <<EOF>> autostart
@lxpanel --profile LXDE
@pcmanfm --desktop --profile LXDE
EOF
sudo chmod 744 autostart
sudo cp autostart /etc/xdg/lxsession/LXDE/
sudo cp autostart /home/wareck/.config/lxsession/LXDE/
sudo rm autostart
}

intro_
autoroot_
ubuntu_version_check
update_
kernel_check_
autoroot_
nvidia_driver_
update_
download_packs_
printer_
build_miners_
configure_
mounting_drives_
cleaning_
clear
dynmotd
remove_screensaver


echo -e "$(tput setaf 10)\nEnd of script:$(tput sgr0) \n"
echo -e "$(tput setaf 7)Need to install webmin manually (reboot required)$(tput sgr0)"
echo -e "$(tput setaf 7)after install webmin, reboot is required $(tput sgr0)"
echo -e ""

