#!/bin/bash
set -e
Gui=No #can be Yes No or Only
Service=No
Version=3.0

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
}

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 [ $Gui = "Yes" ]
then
echo -n "$(tput setaf 3)"
print_centered "Chia Gui + mining Enabled"
echo -n "$(tput sgr0)"
fi
if [ $Gui = "Only" ]
then
echo -n "$(tput setaf 3)"
print_centered "Chia blockchain Gui Only"
echo -n "$(tput sgr0)"
fi
if [ $Gui = "No" ]
then
echo -n "$(tput setaf 3)"
print_centered "Chia Mining Enabled"
echo -n "$(tput sgr0)"
fi

sleep 3
echo -e "$(tput setaf 2)\nUpdate and install packages:$(tput sgr0)"
sudo apt-get install python3-venv python3-distutils python3-dev libsodium-dev -y
echo -e "\e[97mDone.\e[0m"


if ! [ $Gui = "Only" ]
then
echo -e "$(tput setaf 2)\nDownload pack:$(tput sgr0)"
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

echo -e "$(tput setaf 2)\nBuild Chia-Miner:$(tput sgr0)"
cp ../pack/chia-miner.tar.xz /home/wareck/miners
cd /home/wareck/miners
tar xvfJ chia-miner.tar.xz
rm chia-miner.tar.xz

if [ $Service = "Yes" ]
then
cat <<'EOF'>> chia-miner.sh
#!/bin/sh -e
### BEGIN INIT INFO
# Provides:          chiaminer
# Required-Start:    networking
# Default-Start:     3 4 5
# Default-Stop:      0 6
### END INIT INFO
case "$1" in
        start)
	cd /home/wareck/miners/chia-miner/
	#su wareck -c "screen -dmS chia-miner /home/wareck/miners/chia-miner/hpool-miner-chia -config /home/wareck/miners/chia-miner/config.yaml"
	if ping -c 1 192.168.1.11
	then
	sleep 3
	su wareck -c "screen -dmS chia-miner /home/wareck/miners/chia-miner/hpool-miner-chia -config /home/wareck/miners/chia-miner/config_proxy.yaml"
	else
	sleep 3
	su wareck -c "screen -dmS chia-miner /home/wareck/miners/chia-miner/hpool-miner-chia -config /home/wareck/miners/chia-miner/config.yaml"
	fi
	;;
	stop)
	sudo killall -9 hpool-miner-chia | true
	;;
        *)
        echo "Usage: /etc/init.d/chia-miner {start|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
sudo rm chia-miner.sh
fi
echo -e "\e[97mDone.\e[0m"

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

cat <<'EOF'>> nft.sh
#!/bin/bash
./chia_plot -n 1 -r 4 -u 128 -t /media/Nvme/ -2 /media/Nvme/ -d /media/Partage/Plots-nft/ -c xch18c2jp56msc47yzww25wcnzz5fk9y268zl3zh357kftsp0n7l6m8scyqg4e -f 8ba5acd276b45fd0f749ce195dcddcf0e63c3bf2e751e9e273faa0125075c36a11f2ee231e7cf74abed2920be97fc373
EOF
chmod +x nft.sh
echo -e "\e[97mDone.\e[0m"

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

if [ $Gui = "Yes" ] || [ $Gui = "Only" ]
then
echo -e "$(tput setaf 2)\nBuild Chia-Gui :$(tput sgr0)"
cd /home/wareck
if [ ! -d  /home/wareck/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
fi

echo -e "$(tput setaf 2)\nChiaGiga Horse:$(tput sgr0)"
if [ -d /home/wareck/chia-gigahorse ]
then
cd /home/wareck/chia-gigahorse
git pull
else
cd /home/wareck/
git clone https://github.com/madMAx43v3r/chia-gigahorse.git
fi
sudo cp ~/chia-gigahorse/plot-sink/linux/x86_64/* /usr/local/bin/
sudo cp ~/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
chia_plot_sink -p 1447 /Chia/Chia1/Plots-nft/ /Chia/Chia2/Plots-nft/ /Partage/Plots-nft/ /Cloud/Plots-nft/ /Store/Plots-nft/
EOF
chmod +x distrib_chia.sh
sudo mv distrib_chia.sh /usr/local/bin/

cat <<'EOF'>> ~/go.sh
#!/bin/bash
./chia_plot -z 1447 -M 8 -C 6 -n -1 -t /Nvme0/plotting/ -2 /Nvme1/plotting/ -d @192.168.1.100 \
-c xch10yjksfwm8s66z32qy35x950608hk8l67vmsur4rwpaanpkd6ks3qhfw5nk \
-f 8ba5acd276b45fd0f749ce195dcddcf0e63c3bf2e751e9e273faa0125075c36a11f2ee231e7cf74abed2920be97fc373
EOF
chmod +x ~/go.sh
cp ~/go.sh ~/chia-gigahorse/cpu-plotter/linux/x86_64/
rm ~/go.sh
echo -e "\n\e[97mEnd of process...\e[0m"