#!/bin/bash
set -e
Version=2.3
Gui=no #YES for gui build
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
}

print_centered "$(tput setaf 10)"
print_centered " ______ __     __        "
print_centered "|      |  |--.|__|.---.-."
print_centered "|   ---|     ||  ||  _  |"
print_centered "|______|__|__||__||___._|"
echo -n "$(tput setaf 7)"
print_centered "Toolkit for Chia v$Version"
if [ $Gui = "YES" ]
then
echo -n "$(tput setaf 7)"
print_centered "(Gui / wallet enabled)"
fi
print_centered "$(tput setaf 7)"
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"

echo -e "$(tput setaf 2)\nDownload pack:$(tput sgr0)"
wget -c -q --show-progress http://folivier.homelinux.org/cube/pack/chia-miner.tar.xz
mv chia-miner.tar.xz /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/
cd /home/wareck
tar xvfJ chia-miner.tar.xz
rm chia-miner.tar.xz

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
cd /home/wareck/chia-miner/
#su wareck -c "screen -dmS chia-miner /home/wareck/chia-miner/hpool-miner-chia -config /home/wareck/chia-miner/config.yaml"
if ping -c 1 192.168.1.11
then
sleep 3
su wareck -c "screen -dmS chia-miner /home/wareck/chia-miner/hpool-miner-chia -config /home/wareck/chia-miner/config_proxy.yaml"
else
sleep 3
su wareck -c "screen -dmS chia-miner /home/wareck/chia-miner/hpool-miner-chia -config /home/wareck/chia-miner/config.yaml"
fi
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
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"

if [ $Gui = "YES" ]
then
echo -e "$(tput setaf 2)\nBuild Chia-Gui :$(tput sgr0)"
cd /home/wareck
if [ ! -d  /home/wareck/chia-pos ]
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 "\n\e[97mEnd of process...\e[0m"