#!/bin/bash
FoxyPool_service=Yes
FoxyPool_service=${FoxyPool_service^^}

if [ -f /tmp/latest ]; then rm /tmp/latest ; fi
curl -s https://api.github.com/repos/foxypool/foxy-gh-farmer/releases/latest > /tmp/latest
VERSION=$(cat /tmp/latest | grep "tag_name" | awk {'print$2'} | tr -d \",)
URL=$(cat /tmp//latest |grep "browser_download_url.*ubuntu.zip" |  cut -d : -f 2,3 | tr -d \")

if [[ "$FoxyPool_service" == "YES" ]] || [[ "$FoxyPool_service" == "NO" ]];then error_flag=$((error_flag+0));else error_flag=$((error_flag+1));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
}



export TERM=xterm-256color
print_centered "$(tput setaf 130)"
print_centered " _____                         _ "
print_centered "|   __|___ _ _ _ _ ___ ___ ___| |"
print_centered "|   __| . |_'_| | | . | . | . | |"
print_centered "|__|  |___|_,_|_  |  _|___|___|_|"
print_centered "              |___|_|            "
print_centered ""
echo -n "$(tput sgr0)"
print_centered "Version $VERSION"

if [ $error_flag -ne 0 ]
then
        echo -n "$(tput setaf 1 ;tput bold)"
        print_centered "Config Error !!!"
        echo -n "$(tput sgr0)"
        exit 0
fi
case $FoxyPool_service in
YES)
        echo -n "$(tput setaf 44)"
        print_centered "FoxyPool Service Enabled"
        echo -n "$(tput sgr0)"
        ;;
esac
echo -e "\n"


if [ ! -d ~/miners/foxypool ]
then
mkdir ~/miners/foxypool
fi

echo -e "$(tput setaf 10)\nInstall Foxy-gh-Farmer:$(tput sgr0)"
cd ~/miners/foxypool
wget -q $URL
unzip -q -o foxy-gh-farmer-ubuntu.zip
rm foxy-gh-farmer-ubuntu.zip
echo -e "\e[97mDone.\e[0m"

function foxypool_service_ {
echo -e "$(tput setaf 10)\nInstall FoxyPool service :$(tput sgr0)"
cat > $HOME/foxypool.service << EOF
# The foxypool service (part of systemd)
# file: /etc/systemd/system/foxypool.service

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

[Service]
Type=simple
User            = $USER
WorkingDirectory= $HOME/miners/foxy-gh-farmer
ExecStart       = $HOME/miners/foxy-gh-farmer/foxy-gh-farmer -c $HOME/miners/foxy-gh-farmer/foxy-gh-farmer.yaml
Restart         = on-failure
RestartSec=30
LimitNOFILE=99999
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=foxypool


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

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

sudo systemctl restart rsyslog >/dev/null 2>&1
sudo systemctl daemon-reload >/dev/null 2>&1
sudo systemctl enable foxypool >/dev/null 2>&1
sudo systemctl start foxypool >/dev/null 2>&1

echo -e "\e[97mDone.\e[0m"
}

function bashing {
cat <<'EOF'>> ~/foxypool.sh
CTR=xch1hsadv5h4c6mvk2av8vg6pdmezu8rapmrsta8z7ds0yd24yt2xshshqrtsy
FPK=b6d0c62c90338b2dc9c5c2368e4acd1cbf60cadb3b86c0bf58a3706d59b276b49520bf8bfb4e07735fdfb92b03053170
OUT=/Partage/Plots/
./cuda_plot_k32 -g 0 -c $CTR -f $FPK -C 17 -n 2 -t /raid/ -3 /raid/ -d $OUT
ProofOfSpace farm -t 8 -d 100 -f /Partage/Plots/*.plot
EOF
chmod +x ~/foxypool.sh
cp ~/foxypool.sh ~/chia/chia-gigahorse/cuda-plotter/linux/x86_64/foxypool.sh
rm ~/foxypool.sh
}

if [ $FoxyPool_service = "YES" ]; then foxypool_service_;fi
rm /tmp/latest
bashing
