#!/bin/bash
set -e
Gui=No #can be Yes No or Only
Version=4.6
MadMax=Yes
GigaHorse=Yes
BladeBit=Yes
ChiaPos=Yes
Service=No
Miner=Yes
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 jumpto
{
    label=$1
    cmd=$(sed -n "/$label:/{:a;n;p;ba};" $0 | grep -v ':$')
    eval "$cmd"
    exit
}
function simplify_config {
error_flag=0
Gui=${Gui^^}
Service=${Service^^}
MadMax=${MadMax^^}
GigaHorse=${GigaHorse^^}
BladeBit=${BladeBit^^}
ChiaPos=${ChiaPos^^}
Miner=${Miner^^}
if [ $MadMax = "YES" ] || [ $MadMax = "NO" ];then error_flag=0;else error_flag=1;fi
if [ $GigaHorse = "YES" ] || [ $GigaHorse = "NO" ];then error_flag=0;else error_flag=1;fi
if [ $BladeBit = "YES" ] || [ $BladeBit = "NO" ];then error_flag=0;else error_flag=1;fi
if [ $ChiaPos = "YES" ] || [ $ChiaPos = "NO" ];then error_flag=0;else error_flag=1;fi
if [ $Service = "YES" ] || [ $Service = "NO" ];then error_flag=0;else error_flag=1;fi
if [ $Miner = "YES" ] || [ $Miner = "NO" ];then error_flag=0;else error_flag=1;fi
}

function intro_ {
print_centered "$(tput setaf 10)"
print_centered " ______ __     __        "
print_centered "|      |  |--.|__|.---.-."
print_centered "|   ---|     ||  ||  _  |"
print_centered "|______|__|__||__||___._|"
echo -n "$(tput sgr0)"
print_centered "Toolkit for Chia v$Version"
if [ $error_flag = 1 ]
then
        echo -n "$(tput setaf 1 ;tput blink;tput bold)"
        print_centered "Config Error !!!"
        echo -n "$(tput sgr0)"
        exit 0
fi
case $Gui in
YES)
	echo -n "$(tput setaf 11)"
	print_centered "Blockchain wallet + Gui"
	print_centered "Chia Plotting Tools"
	echo -n "$(tput sgr0)"
	;;
NO)
        echo -n "$(tput setaf 11)"
        print_centered "Chia Plotting Tools"
        echo -n "$(tput sgr0)"

	;;
ONLY)
	echo -n "$(tput setaf 11)"
	print_centered "Blockchain wallet Only"
	echo -n "$(tput sgr0)"
	;;
*)
	echo -n "$(tput setaf 1 ;tput blink;tput bold)"
	print_centered "Config Error !!!"
	echo -n "$(tput sgr0)"
	exit 0
	;;
esac
if [ $Gui = "NO" ] || [ $Gui = "YES" ]
then
case $Service in
YES)
	echo -n "$(tput setaf 11)"
	print_centered "Mining service Enabled"
	echo -n "$(tput sgr0)"
	;;
esac
fi
sleep 3
if ! [ -d /home/wareck/chia ];then mkdir /home/wareck/chia;fi
}

function update_ {
echo -e "$(tput setaf 10)\nUpdate and install packages:$(tput sgr0)"
sleep 1
#if grep "Drivers=NVIDIA" /home/wareck/Build_Cube/build_cube.sh >/dev/null
#then
extend="libgomp1 ocl-icd-opencl-dev libgmp-dev libnuma-dev"
#else
#extend=""
#fi
sudo apt-get install python3-venv python3-distutils python3-dev libsodium-dev $extend -y
echo -e "\e[97mDone.\e[0m"
}

function chia_plotter {
echo -e "$(tput setaf 10)\nBuild Chia-Plotter (MadMax4ever) :$(tput sgr0)"
sleep 1
cd /home/wareck/chia/
if [ ! -d chia-plotter ]
then
git clone https://github.com/madMAx43v3r/chia-plotter.git
else
cd /home/wareck/chia/chia-plotter
git pull
fi
cd /home/wareck/chia/chia-plotter
git submodule init
git submodule update
./make_devel.sh
cd build
cat <<'EOF'>> go.sh
#!/bin/bash
POOL=a99e72c49c5ed39bf793caab9e074a80dd61011a859e975842874aab47b783f9ce84282febe2ca275c0930149b67990a
FARM=8ba5acd276b45fd0f749ce195dcddcf0e63c3bf2e751e9e273faa0125075c36a11f2ee231e7cf74abed2920be97fc373
OUT=/media/Partage/Plots/
./chia_plot -p $POOL -f $FARM -n 1 -r 6 -u 128 -t /nvme0/plotting/ -2 /nvme0/plotting/ -d $OUT
EOF
chmod +x go.sh

cat <<'EOF'>> nft.sh
#!/bin/bash
POOL=a99e72c49c5ed39bf793caab9e074a80dd61011a859e975842874aab47b783f9ce84282febe2ca275c0930149b67990a
FARM=8ba5acd276b45fd0f749ce195dcddcf0e63c3bf2e751e9e273faa0125075c36a11f2ee231e7cf74abed2920be97fc373
OUT=/media/Partage/Plots-nft/
./chia_plot -p $POOL -f $FARM -n 1 -r 4 -u 128 -t /nvme0/plotting/ -2 /nvme1/plotting/ -d $OUT
EOF
chmod +x nft.sh
echo -e "\e[97mDone.\e[0m"
}

function bladebit {
echo -e "$(tput setaf 10)\nBuild Bladebit v2 :$(tput sgr0)"
sleep 1
cd /home/wareck/chia
if [ ! -d bladebit ]
then
git clone https://github.com/Chia-Network/bladebit.git
fi
cd /home/wareck/chia/bladebit/
if [ ! -d build ];then mkdir build;fi
cd build
cmake ..
make -j6
cat <<'EOF'>> cuda.sh
#!/bin/bash
./bladebit_cuda \
-c xch10yjksfwm8s66z32qy35x950608hk8l67vmsur4rwpaanpkd6ks3qhfw5nk \
-f 8ba5acd276b45fd0f749ce195dcddcf0e63c3bf2e751e9e273faa0125075c36a11f2ee231e7cf74abed2920be97fc373 \
diskplot -t1 /nvme0/plotting/ -t2 /nvme1/plotting/ /farm/farm0/Plots/
EOF
cat <<'EOF'>> flex.sh
#!/bin/bash
CRT=xch10yjksfwm8s66z32qy35x950608hk8l67vmsur4rwpaanpkd6ks3qhfw5nk
FRM=8ba5acd276b45fd0f749ce195dcddcf0e63c3bf2e751e9e273faa0125075c36a11f2ee231e7cf74abed2920be97fc373
OUT=/media/Partage/Plots-nft/
./bladebit -f $FRM -c $CRT diskplot --cache 100G -t1 /nvme1/plotting/ -t2 /nvme1/plotting/ $OUT
EOF
cat <<'EOF'>> cuda_h9.sh
#!/bin/bash
#!/bin/bash
POOL=a99e72c49c5ed39bf793caab9e074a80dd61011a859e975842874aab47b783f9ce84282febe2ca275c0930149b67990a
FARM=8ba5acd276b45fd0f749ce195dcddcf0e63c3bf2e751e9e273faa0125075c36a11f2ee231e7cf74abed2920be97fc373
OUT=/farm/farm4/Plots
./bladebit_cuda -p $POOL -f $FARM -z 7 cudaplot -d 0 --disk-16 -t1 /nvme0/plotting $OUT
EOF
chmod +x cuda.sh
chmod +x flex.sh
chmod +x cuda_h9.sh
echo -e "\e[97mDone.\e[0m"
}

function gigahorse {
echo -e "$(tput setaf 10)\nMadMax4ever GigaHorse:$(tput sgr0)"
sleep 1
if [ -d /home/wareck/chia/chia-gigahorse ]
then
cd /home/wareck/chia/chia-gigahorse
git pull
else
cd /home/wareck/chia/
git clone https://github.com/madMAx43v3r/chia-gigahorse.git
fi
echo "generate bash files..."
sudo cp ~/chia/chia-gigahorse/plot-sink/linux/x86_64/* /usr/local/bin/
sudo cp ~/chia/chia-gigahorse/cpu-plotter/linux/x86_64/* /usr/local/bin/
cd ~
if [ -f distrib_chia.sh ];then sudo rm distrib_chia.sh;fi
if [ -f /usr/local/bin/distrib_chia.sh ];then sudo rm /usr/local/bin/distrib_chia.sh;fi
cat <<'EOF'>> distrib_chia.sh
#!/bin/bash
echo "IP : $(hostname -I)"
chia_plot_sink -p 1447 /farm/farm0/Plots-nft/ /farm/farm1/Plots-nft/ /farm/farm2/Plots-nft/ /farm/farm3/Plots-nft/ /farm/farm4/Plots-nft/ /Partage/Plots-nft/
EOF
chmod +x distrib_chia.sh
sudo mv distrib_chia.sh /usr/local/bin/

cat <<'EOF'>> ~/go.sh
#!/bin/bash
CRT=xch10yjksfwm8s66z32qy35x950608hk8l67vmsur4rwpaanpkd6ks3qhfw5nk
FRM=8ba5acd276b45fd0f749ce195dcddcf0e63c3bf2e751e9e273faa0125075c36a11f2ee231e7cf74abed2920be97fc373
OUT=@192.168.1.100
./chia_plot -c $CRT -f $FRM -G -r 6 -z 1447 -C 8 -n -1 -t /nvme0/plotting/ -2 /nvme1/plotting/ -d $OUT
EOF
chmod +x ~/go.sh
cp ~/go.sh ~/chia/chia-gigahorse/cpu-plotter/linux/x86_64/
rm ~/go.sh
echo -e "\e[97mDone.\e[0m"
}

function chiapos {
echo -e "$(tput setaf 10)\nBuild Chia-Pos:$(tput sgr0)"
sleep 1
cd /home/wareck/chia
if [ ! -d  /home/wareck/chia/chia-pos ]
then
git clone https://github.com/Chia-Network/chiapos.git chia-pos
cd /home/wareck/chia/chia-pos
else
cd /home/wareck/chia/chia-pos
git pull
fi
if [ ! -d build ]; then mkdir build;fi
cd build
cmake ..
cmake --build . -- -j 6
echo -e "\e[97mDone.\e[0m"
}

function miner_ {
echo -e "$(tput setaf 10)\nChia-Miner (hpool):$(tput sgr0)"
sleep 1
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
cp /home/wareck/Build_Cube/pack/chia-miner.tar.xz /home/wareck/miners
cd /home/wareck/miners
tar xvfJ chia-miner.tar.xz
rm chia-miner.tar.xz
echo -e "\e[97mDone.\e[0m"
}

function chia_blockchain {
echo -e "$(tput setaf 10)\nBuild Chia-blockchain & Gui :$(tput sgr0)"
sleep 1
cd /home/wareck/chia
if [ ! -d  /home/wareck/chia/chia-blockchain ]
then
git clone https://github.com/Chia-Network/chia-blockchain.git -b latest --recurse-submodules
cd chia-blockchain
else
cd chia-blockchain
git pull
fi
sh install.sh
source ./activate
sh install-gui.sh
echo -e "\e[97mDone.\e[0m"
}

function service_old {
echo -e "$(tput setaf 10)\nInstall chia-miner service (hpool) :$(tput sgr0)"
sleep 1
echo "Generation du fichier /etc/init.d/chia-miner"
cat <<'EOF'>> chia-miner.sh
#!/bin/sh
### BEGIN INIT INFO
# Provides:          chiaminer
# Required-Start:    networking
# Default-Start:     3 4 5
# Default-Stop:      0 6
### END INIT INFO
case "$1" in
        start)
        if pgrep -x "hpool-miner-chia" > /dev/null
        then
        sudo killall -9 hpool-miner-chia | true
        fi
        su wareck -c "screen -dmS chia-miner /home/wareck/miners/chia-miner/hpool-miner-chia -config /home/wareck/miners/chia-miner/config.yaml"
        ;;
        stop)
        if pgrep -x "hpool-miner-chia" > /dev/null
        then
        sudo killall -9 hpool-miner-chia | true
        fi
        ;;
        restart)
        $0 stop
        $0 start
        ;;
        *)
        echo "Usage: /etc/init.d/chia-miner {start|restart|stop}"
        exit 1
        ;;
esac

exit 0
EOF
chmod +x chia-miner.sh
sudo cp chia-miner.sh /etc/init.d/chia-miner
sudo update-rc.d chia-miner defaults
sleep 1
sudo /etc/init.d/chia-miner start
sudo rm chia-miner.sh
echo -e "\e[97mDone.\e[0m"
}

function service_ {
echo -e "$(tput setaf 10)\nInstall chia-miner service (hpool) :$(tput sgr0)"
sleep 1
cat <<'EOF'>> chia-miner.service
[Unit]
Description=HPool miner
After=network.target
StartLimitIntervalSec=500
StartLimitBurst=5
[Service]
#Restart=on-failure
#RestartSec=5s
WorkingDirectory=/home/wareck/miners/chia-miner/
User=wareck
Group=adm
ExecStart=/usr/bin/screen -DmS hpool-miner /home/wareck/miners/chia-miner/hpool-miner-chia -config /home/wareck/miners/chia-miner/config.yaml
ExecStop=/usr/bin/screen -S hpool-miner -X quit

LimitNOFILE=999999
[Install]
WantedBy=multi-user.target
EOF
sudo mv chia-miner.service /etc/systemd/system/hpool-miner.service
sudo systemctl daemon-reload
sudo systemctl start hpool-miner.service
sudo systemctl enable hpool-miner.service
echo -e "\e[97mDone.\e[0m"
}

function cmake_ {
if [ $BladeBit = "YES" ]
then
pids=""
/home/wareck/Build_Cube/options/tools/_cmake.sh & pids="$pids $!"
wait $pids
fi
}

simplify_config
intro_
update_
cmake_
case $Gui in
NO|YES)
        if [ $GigaHorse = "YES" ];then gigahorse;fi
	if [ $MadMax = "YES" ];then chia_plotter;fi
	if [ $BladeBit = "YES" ];then bladebit;fi
	if [ $ChiaPos = "YES" ];then chiapos;fi
	if [ $Service = "YES" ];then service_;fi
	if [ $Miner = "YES" ];then miner_;fi
	;;
esac
case $Gui in
YES|ONLY)
        chia_blockchain
	;;
*)
        ;;
esac

echo -e "\n\e[97mEnd of process...\e[0m"
