#!/bin/bash
set -e
version=2
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 "|__|  |_|___|_,_|_| |__,|_| |_|_|_|___|_|  "
print_centered "                                           "
echo -n "$(tput setaf 7)"
print_centered "Flexpool mining software"

function build_service {
cat > $HOME/flexfarmer.service << EOF
# The flexfarmer service (part of systemd)
# file: /etc/systemd/system/flexfarmer.service

[Unit]
Description     = FlexFarmer Service
After=network.target

[Service]
Type=simple
User            = $USER
WorkingDirectory= $HOME/flexfarmer
ExecStart       = $HOME/flexfarmer/flexfarmer -c $HOME/flexfarmer/config.yml
Restart         = on-failure
RestartSec=30
LimitNOFILE=99999
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=flexfarmer


[Install]
WantedBy    = multi-user.target
EOF
sudo mv $HOME/flexfarmer.service /etc/systemd/system/flexfarmer.service  >/dev/null
sudo chmod 644 /etc/systemd/system/flexfarmer.service

cat <<'EOF'>> flexfarmer.conf
if $programname == 'flexfarmer' then /var/log/flexfarmer.log
& stop
EOF
sudo cp flexfarmer.conf /etc/rsyslog.d/flexfarmer.conf
sudo rm flexfarmer.conf
if [ ! -f /var/log/flexfarmer.log ]
then
sudo touch /var/log/flexfarmer.log
sudo chown syslog:adm /var/log/flexfarmer.log
fi
cat <<'EOF'>> logrot
/var/log/flexfarmer.log {
    daily
    rotate 7
    copytruncate
    notifempty
    missingok
    su root syslog
}
EOF
sudo cp logrot /etc/logrotate.d/flexfarmer
sudo rm logrot
print_centered "Install service : [ OK ]"
}


function install_ {
cd /home/wareck/
wget -c -q http://folivier.homelinux.org/cube/pack/flexfarmer.tar.xz
tar xfJ flexfarmer.tar.xz
rm /home/wareck/flexfarmer.tar.xz
sudo cp /home/wareck/flexfarmer/flexfarmer /usr/local/bin/
print_centered "Install software : [ OK ]"
}

function nft_builder {
if [ -f /home/wareck/chia-plotter/build/flexpool.sh ];then rm /home/wareck/chia-plotter/build/flexpool.sh;fi
if [ ! -d /home/wareck/chia-plotter/build/ ]; then mkdir -p /home/wareck/chia-plotter/build/ ;fi
cat <<'EOF'>> /home/wareck/chia-plotter/build/flexpool.sh
#!/bin/bash
./chia_plot -n 1 -r 4 -u 128 -t /media/Nvme/plotting/ -2 /media/Nvme/plotting/ -d /Chia/Chia2/Plots-nft/ -c xch10yjksfwm8s66z32qy35x950608hk8l67vmsur4rwpaanpkd6ks3qhfw5nk -f 8ba5acd276b45fd0f749ce195dcddcf0e63c3bf2e751e9e273faa0125075c36a11f2ee231e7cf74abed2920be97fc373
EOF
chmod +x /home/wareck/chia-plotter/build/flexpool.sh
print_centered "Install nft script : [ OK ]"
}

echo
install_
build_service
nft_builder
sudo systemctl restart rsyslog
sudo systemctl daemon-reload >/dev/null
sudo systemctl enable flexfarmer >/dev/null
sudo systemctl start flexfarmer >/dev/null
#sudo systemctl status flexfarmer
echo
