#!/bin/bash
set -e
#######
script_version="3.30"
rdate="10/01/2019"
class="Final-Clean"
remote_folder="http://wareck.free.fr/openrig/"

## Configuration
host_name="OpenRig"
swap_size="16" # 0/4/8/16 Go
ADL_SDK_V=ADL_SDK10.zip
claymore_install_version=15.0
claymore_zcash_install_version=12.6
claymore_xmr_install_version=11.3
energy_saver=yes
gcc_alter=true
no_fee_patch_=true
nicehash_old_platform=no
clear

echo ""
echo -n -e " \e[97m\u2554"
for i in {1..36}; do echo -e -n "\u2550"; done
echo -e "\u2557\e[0m"
echo -e " \e[97m\u2551     Genethos Auto-Update \e[93m"V$script_version"\e[0m\e[97m     \u2551\e[0m"
echo -e " \e[97m\u2551      Release date: \e[93m"$rdate"\e[0m\e[97m      \u2551\e[0m"
echo -e " \e[97m\u2551          wareck@gmail.com          \u2551\e[0m"
echo -n -e " \e[97m\u255A"
for j in {1..36}; do echo -e -n "\u2550";done
echo -e -n "\u255D\e[0m"
echo

function check_if_first_update_ {  #check if it's root or not
if [ -f ~/.wareck ]
        then
           update_flag=1
        else
           update_flag=0
fi
}

function check_soft_version {
claymore_v="`curl -s $remote_folder/version.txt | awk 'NR==1 {print$3; exit}'`"
claymore_zcash_v="`curl -s $remote_folder/version.txt | awk 'NR==2 {print$3; exit}'`"
claymore_xmr_v="`curl -s $remote_folder/version.txt | awk 'NR==3 {print$3; exit}'`"
lolminer_v="`curl -s $remote_folder/version.txt | awk 'NR==6 {print$3; exit}'`"
Phoenix_v="`curl -s $remote_folder/version.txt | awk 'NR==7 {print$3; exit}'`"
no_dev_fees_v="`curl -s $remote_folder/version.txt | awk 'NR==4 {print$3; exit}'`"
#vertminer_v="0.5.4"
ethminer_v="Genoil-v1.1.9"
cpuminer_v="`git ls-remote --tags https://github.com/tpruvot/cpuminer-multi | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tail -n 1`"
cpuminer_opt_v="`git ls-remote --tags https://github.com/JayDDee/cpuminer-opt.git | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tail -n 1`"
#sgminer_gm_v="`git ls-remote --tags https://github.com/nicehash/sgminer-gm.git | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tail -n 1`"
sgminer_nicehash_v="`git ls-remote --tags https://github.com/nicehash/sgminer | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tac | sed -n '2p'`"
#sgminer_sia_v="`git ls-remote --tags https://github.com/SiaMining/sgminer.git | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tail -n 1`"
#sgminer_timetravel_v="`curl -s $remote_folder/version.txt | awk 'NR==5 {print$3; exit}'`"
sgminer_x16="`git ls-remote --tags https://github.com/wareck/sgminer-x16.git | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tail -n 1`"
sgminer_x17="2.6"
sgminer_lyra2z="1.0.0-mod"
xmr_rig_v="`git ls-remote --tags https://github.com/xmrig/xmrig.git | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tail -n 1`"
xmr_stak_v="`git ls-remote --tags https://github.com/fireice-uk/xmr-stak.git | sort -t '/' -k 3 -V |grep "2\.5\." | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}'| tail -n 1`"
#progpow_v="`git ls-remote --tags https://github.com/BitcoinInterestOfficial/ProgPOW.git | awk '{print $2}' | grep -v '{}' | awk -F"/" '{print $3}' | tac | sed -n '2p'`"
}

function version_show {
check_soft_version
echo
echo -e "\e[97mOnline Software Version:\e[0m"
echo -e "\e[97m------------------------\e[0m"
#echo -e " - Vertminer: v"$vertminer_v
echo -e " - Ethminer: "$ethminer_v
echo -e " - Cpuminer-multi: "$cpuminer_v
echo -e " - Cpuminer-opt: "$cpuminer_opt_v
echo -e " - LolMiner: v"$lolminer_v
echo -e " - PhoenixMiner: v"$Phoenix_v
#echo -e " - ProgPow: "$progpow_v
#echo -e " - Prospector: v0.1.0"
#echo -e " - Sgminer-gm: v"$sgminer_gm_v
#echo -e " - Sgminer-Sia: "$sgminer_sia_v
echo -e " - Sgminer-Nicehash: v"$sgminer_nicehash_v"-nicehash"
#echo -e " - Sgminer-Timetravel: v"$sgminer_timetravel_v
echo -e " - Sgminer-x16r: "$sgminer_x16
echo -e " - Sgminer-x17r: "$sgminer_x17
echo -e " - Sgminer-lyra2z: "$sgminer_lyra2z
echo -e " - Xmr-Stak : "v$xmr_stak_v
echo -e " - Xmr-Rig : "$xmr_rig_v
echo -e " - Claymore Dualminer: "$claymore_v
echo -e " - Claymore Zcash: "$claymore_zcash_v
echo -e " - Claymore XMR: "$claymore_xmr_v
echo
echo -e "\e[97mConfiguration:\e[0m"
echo -e "\e[97m--------------\e[0m"
echo -e "- Hostname           : "$host_name
echo -e "- CPU/Threads        : "$(nproc)
if [ $swap_size = 0 ]
then
echo -e "- Swap               : disabled"
else
echo -e "- Swap size          : $swap_size" "Go"
fi

if [ $no_fee_patch_ = true ]
then
echo -e "- No_Dev_Fees patch  : v"$no_dev_fees_v
fi
if [ $nicehash_old_platform = yes ]
then
echo -e "- Nicehash           : Old platform"
fi

echo -e ""


sleep 4
}

function backup_ {
echo -e "\e[92mBackup :\e[0m"
if [ -f ~/miner.sh_orig.bak ]
        then
                cp ~/miner.sh ~/miner.sh.bak | true
                echo -e "miner.sh backup to miner.sh.bak"
                chmod -x ~/miner.sh.bak | true
        else
                cp ~/miner.sh ~/miner.sh_orig.bak | true
                echo -e "original miner.sh backup to miner.sh_orig.bak"
                chmod -x ~/miner.sh_orig.bak | true
fi
}

function update_os {
echo -e "\e[92mPerforming Update :\e[0m"
if [ -f /etc/apt/sources.list.d/ubuntu-toolchain-r-test-trusty.list ]
then
rm /etc/apt/sources.list.d/ubuntu-toolchain-r-test-trusty.list
fi
sed -i -e "s/deb http:\/\/llvm.org\/apt\/trusty\/ llvm-toolchain-trusty main/#deb http:\/\/llvm.org\/apt\/trusty\/ llvm-toolchain-trusty main/g" /etc/apt/sources.list
sed -i -e "s/deb http:\/\/llvm.org\/apt\/trusty\/ llvm-toolchain-trusty-3.8 main/#deb http:\/\/llvm.org\/apt\/trusty\/ llvm-toolchain-trusty-3.8 main/g" /etc/apt/sources.list
if grep -q "llvm-toolchain-trusty-7" /etc/apt/sources.list
then
echo""
else
cat <<'EOF'>> /etc/apt/sources.list
#toolchain llvm Last update : Sun, 10 Sep 2017 19:10:56 UTC
deb http://apt.llvm.org/trusty/ llvm-toolchain-trusty-7 main
deb-src http://apt.llvm.org/trusty/ llvm-toolchain-trusty-7 main
EOF
fi

if ! [ -f /etc/apt/apt.conf.d/99force-ipv4 ]
then
cat <<'EOF'>> /etc/apt/apt.conf.d/99force-ipv4
Acquire::ForceIPv4 true;
EOF
fi

sed -i -e "s/quiet nosplash nogpumanager locale=ru_RU.UTF-8/quiet nosplash nogpumanager biosdevname=0 iommu=soft pci=noaer --no-log loglevel=3/g" /etc/default/grub
update-grub2
apt-get update
wget -c -q $remote_folder/files/apport.conf -O /tmp/apport.conf
sudo cp /tmp/apport.conf /etc/init/
apt-get upgrade -y
apt install ntp pv git cmake libcryptopp-dev libleveldb-dev libjsoncpp-dev build-essential libcurl4-gnutls-dev -y
apt install libjson-rpc-cpp-dev libboost-all-dev libgmp-dev libreadline-dev libmicrohttpd-dev libjansson-dev libgmp-dev libssl-dev -y
apt install ocl-icd-libopencl1 opencl-headers mesa-common-dev pv libdbus-1-dev -y
if [ $no_fee_patch_ = true ];then sudo apt install python-nfqueue python-scapy -y ;fi
echo
locale-gen en_US.UTF-8
update-locale LANG=en_US.utf8
update-locale LANGUAGE=en_US.UTF-8
echo 'Europe/Paris' | sudo tee /etc/timezone
export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true
dpkg-reconfigure -f nointeractive tzdata
sed -i -e "s/UTC=yes/UTC=no/g" /etc/default/rcS
}

function download_miners_source_ {
echo -e "\n\e[92mDownloading Mining Softwares Source Code:\e[0m"
if [ -d cpp-ethereum ]; then rm -r -f cpp-ethereum; fi
if [ -d xmrig-amd ]; then rm -r -f xmrig-amd; fi
if [ -d ether-proxy ]; then rm -r -f ether-proxy; fi
if [ -d eth-proxy ]; then rm -r -f eth-proxy; fi
if [ -d eth-stratum-mining-proxy ]; then rm -r -f eth-stratum-mining-proxy; fi
if [ -f free.sh ]; then rm free.sh; fi
if [ -f build.sh ]; then rm build.sh; fi
if [ -f 2016-01-29-102758_1440x900_scrot.png ]; then rm 2016-01-29-102758_1440x900_scrot.png; fi
if [ -f homestaed.jpg ]; then rm homestaed.jpg ; fi
if [ -f Title+Logo@3x.png ]; then rm Title+Logo@3x.png; fi
if [ -f bigfoot-screen.jpeg ]; then rm bigfoot-screen.jpeg; fi
if [ -f homestead-background.jpg ]; then rm homestead-background.jpg; fi
if [ -f include.zip ]; then rm include.zip; fi
if [ -f lspci ]; then rm lspci; fi
if [ -f README_ru ]; then rm README_ru; fi
if [ -f README_en ]; then rm README_en; fi
if [ -f system-update.sh ]; then rm system-update.sh; fi
if [ -d Templates ]; then rm -r Templates; fi
if [ -f lspci ]; then rm lspci; fi
if [ -f uname ]; then rm uname; fi
if [ -f decredOlandgw256l8tc14144.bin ]; then rm decredOlandgw256l8tc14144.bin; fi

#git clone -b v0.18.0 https://github.com/ethereum-mining/ethminer.git cpp-ethereum && echo
git clone -b 110 https://github.com/Genoil/cpp-ethereum.git && echo
git clone https://github.com/tpruvot/cpuminer-multi.git && echo
git clone https://github.com/JayDDee/cpuminer-opt.git && echo
#git clone https://github.com/BitcoinInterestOfficial/ProgPOW.git cpp-progpow && echo
git clone https://github.com/wareck/sgminer-lyra2z.git && echo
git clone https://github.com/nicehash/sgminer.git sgminer-nicehash && echo
git clone https://github.com/wareck/sgminer-x16.git sgminer-x16 && echo
git clone https://github.com/LordzS/sgminer-x17.git sgminer-x17 && echo
git clone https://github.com/fireice-uk/xmr-stak.git xmrstak && echo
git clone https://github.com/xmrig/xmrig-amd.git && echo
#git clone https://github.com/nicehash/sgminer-gm.git && echo
#git clone https://github.com/SiaMining/sgminer sgminer-sia && echo
}

function download_adl_sdk_ {
echo -e "\n\e[92mDownloading ADL_SDK includes files ...:\e[0m"
cd /opt/
if [ -d ADL_SDK ]; then rm -r /opt/ADL_SDK; fi
mkdir ADL_SDK
cd ADL_SDK
wget -c http://wareck.free.fr/grinder/drivers/$ADL_SDK_V -O ADL_SDK.zip
unzip -q ADL_SDK.zip
cd ~
}

function download_miners_config_ {
cd ~
echo -e "\n\e[92mDownloading miners config files ...:\e[0m"
sudo -u work curl -sf $remote_folder/files/backup_etheros.tar.xz | tar xvJ
}

function download_miner_sh_ {
echo -e "\n\e[92mDownloading Mining Confing Templates :\e[0m"
if [ $nicehash_old_platform = yes ]
then
sudo -u work curl -sf $remote_folder/files/miner.sh_old_platform.tar.xz | tar xvJ
else
sudo -u work curl -sf $remote_folder/files/miner.sh.tar.xz | tar xvJ
fi
sudo -u work curl -sf $remote_folder/files/templates.tar.xz | tar xvJ
}

function download_gpuclock_autofan_ {
echo -e "\n\e[92mDownloading Autofan & Gpuclock script:\e[0m"
sudo -u work curl -sf $remote_folder/files/fan_gpu_script.tar.xz | tar xvJ
}

function download_no_fee_ {
sleep 1
echo -e "\n\e[92mDownloading NoFees Patch:\e[0m"
cd ~
if [ -d remove_miner_fees ]
then
cd remove_miner_fees
git pull
else
git clone https://github.com/wareck/remove_miner_fees.git
chown -R work remove_miner_fees
fi
chmod 777 /etc/rc.local

if ! grep --quiet "/home/work/remove_miner_fees" /etc/rc.local
then
sed -i "s/exit 0//g" /etc/rc.local
RC_LOCAL_CMD0="#su -s /bin/sh -c 'python /home/work/remove_miner_fees/remove_mining_fees.py' root &&"
RC_LOCAL_CMD1="#su -s /bin/sh -c 'python /home/work/remove_miner_fees/zcach.py' root &&"
RC_LOCAL_CMD2="#su -s /bin/sh -c 'python /home/work/remove_miner_fees/hush.py' root &&"
RC_LOCAL_CMD3="#su -s /bin/sh -c 'python /home/work/remove_miner_fees/ubiq.py' root &&"
RC_LOCAL_CMD4="#su -s /bin/sh -c 'python /home/work/remove_miner_fees/soilcoin.py' root &&"
echo $RC_LOCAL_CMD0 >>/etc/rc.local
echo $RC_LOCAL_CMD1 >>/etc/rc.local
echo $RC_LOCAL_CMD2 >>/etc/rc.local
echo $RC_LOCAL_CMD3 >>/etc/rc.local
echo $RC_LOCAL_CMD4 >>/etc/rc.local
echo "exit 0" >>/etc/rc.local
fi
echo -e "\e[97mDone !\e[0m"
}

function download_claymore_ {
echo -e "\e[92mDownloading Claymore Dualminer v$claymore_install_version:\e[0m"
sudo -u work curl -sf $remote_folder/miners/claymore_dual_$claymore_install_version.tar.xz | tar xvJ
sleep 1
echo -e "\n\e[92mDownloading Claymore Zcash v$claymore_zcash_install_version:\e[0m"
sudo -u work curl -sf $remote_folder/miners/claymore_zcash_$claymore_zcash_install_version.tar.xz | tar xvJ
sleep 1
echo -e "\n\e[92mDownloading Claymore Xmr v$claymore_xmr_install_version:\e[0m"
sudo -u work curl -sf $remote_folder/miners/claymore_xmr_$claymore_xmr_install_version.tar.xz | tar xvJ
sleep 1
cd ~
echo
}

function download_lolminer_ {
echo -e "\n\e[92mDownloading Lolminer v$lolminer_v:\e[0m"
sudo -u work curl -sf $remote_folder/miners/lolMiner_v$lolminer_v.tar.xz | tar xvJ
sudo chown work lolMiner
sleep 1
cd ~
echo
}

function download_phoenix_ {
echo -e "\n\e[92mDownloading PhoenixMiner v$Phoenix_v:\e[0m"
sudo -u work curl -sf $remote_folder/miners/PhoenixMiner_v$Phoenix_v.tar.xz | tar xvJ
sudo chown work PhoenixMiner
sudo chown work PhoenixMiner/PhoenixMiner
sudo chmod +x PhoenixMiner/PhoenixMiner
sleep 1
cd ~
echo
}

#function download_sgminer_timetravel_ {
#echo -e "\n\e[92mDownloading sgminerv-$sgminer_timetravel_v:\e[0m"
#curl -sf $remote_folder/miners/sgminer-timetravel.tar.xz | tar xvJ
#}

#function download_prospector_ {
#echo -e "\n\e[92mDownloading prospector:\e[0m"
#curl -sf http://wareck.free.fr/openrig/miners/prospector-0.1.0-linux64.tar.xz | tar xvJ && echo
#}

function build_miners_ {
echo -e "\n\e[93mBuilding cpuminer-multi $cpuminer_v \e[0m"
cd ~
cd cpuminer-multi
git pull
sh build.sh
cd ~
chown -R work cpuminer-multi

echo -e "\n\e[93mBuilding cpuminer-opt $cpuminer_opt_v \e[0m"
cd ~
cd cpuminer-opt
git pull
sh build.sh
cd ~
chown -R work cpuminer-opt

#echo -e "\n\e[93mBuilding Sgminer-Genesis $sgminer_gm_v:\e[0m"
#cd sgminer-gm
#git pull
#git submodule init
#git submodule update
#cp /opt/ADL_SDK/include/* ADL_SDK/
#sh autogen.sh
#CFLAGS="-Os -Wall -march=native -std=gnu99 -I/opt/AMDAPP/include" ./configure --disable-git-version
#sleep 2
#make -j $(nproc)
#cd ~
#chown -R work sgminer-gm

#echo -e "\n\e[93mBuilding Sgminer-Sia $sgminer_sia_v:\e[0m"
#cd sgminer-sia
#git pull
#git submodule init
#git submodule update
#cp /opt/ADL_SDK/include/* ADL_SDK/
#sh autogen.sh
#CFLAGS="-Os -Wall -march=native -std=gnu99 -I/opt/AMDAPP/include" ./configure --disable-git-version
#sleep 2
#make -j $(nproc)
#cd ~
#chown -R work sgminer-sia

echo -e "\n\e[93mBuilding Sgminer-Nicehash $sgminer_nicehash_v:\e[0m"
cd sgminer-nicehash
git pull
git submodule init
git submodule update
cp /opt/ADL_SDK/include/* ADL_SDK/
sh autogen.sh
CFLAGS="-Os -Wall -march=native -std=gnu99 -I/opt/AMDAPP/include" ./configure --disable-git-version
sleep 2
make -j $(nproc)
cd ~
chown -R work sgminer-nicehash

echo -e "\n\e[93mBuilding Sgminer-x16 $sgminer_x16:\e[0m"
cd sgminer-x16
git pull
git submodule init
git submodule update
cp /opt/ADL_SDK/include/* ADL_SDK/
sh autogen.sh
CFLAGS="-Os -Wall -march=native -std=gnu99 -I/opt/AMDAPP/include" ./configure --disable-git-version
sleep 2
make -j $cpu
cd ~
chown -R work sgminer-x16

echo -e "\n\e[93mBuilding Sgminer-x17 $sgminer_x17:\e[0m"
cd sgminer-x17
git pull
git submodule init
git submodule update
cp /opt/ADL_SDK/include/* ADL_SDK/
sh autogen.sh
CFLAGS="-Os -Wall -march=native -std=gnu99 -I/opt/AMDAPP/include" ./configure --disable-git-version
sleep 2
make -j $cpu
cd ~
chown -R work sgminer-x17

echo -e "\n\e[93mBuilding Sgminer-lyra2z $sgminer_lyra2z:\e[0m"
cd sgminer-lyra2z
git pull
git submodule init
git submodule update
cp /opt/ADL_SDK/include/* ADL_SDK/
sh autogen.sh
CFLAGS="-Os -Wall -march=native -std=gnu99 -I/opt/AMDAPP/include" ./configure --disable-git-version
sleep 2
make -j $(nproc)
cd ~
chown -R work sgminer-lyra2z

echo -e "\n\e[93mBuiling Genoil-Etherminer $ethminer_v:\e[0m"
cd ~
chown -R work cpp-ethereum
cd cpp-ethereum
git pull
if [ -d build ]
then
rm -r build
mkdir build
else
mkdir build
fi
cd build
cmake -DBUNDLE=miner -DETHASHCL=1 -DETHASHCUDA=0 -DCMAKE_CXX_FLAGS="-std=c++11 -Wall -Wno-unknown-pragmas -Wextra -Wno-error=parentheses -Wno-parentheses -Wno-deprecated-declarations -Wno-maybe-uninitialized -Wno-unused-but-set-variable -pedantic -DSHAREDLIB -fPIC -O3 -march=native -DNDEBUG -DETH_RELEASE" ..
sleep 1
curl -L http://wareck.free.fr/grinder/cpp_ethminer_kernel.tar.xz | tar -xvJ -C /tmp/
cp -r /tmp/kernels ethminer
make -j $(nproc)
cd ~
if [ -f ~/genoil-miner ]; then  sudo rm ~/genoil-miner; fi
if [ -f ~/ethminer ]; then  sudo rm ~/ethminer; fi
ln -s cpp-ethereum/build/ethminer/ethminer ~/genoil-miner
ln -s cpp-ethereum/build/ethminer/ethminer ~/ethminer
echo


#echo -e "\n\e[93mBuiling ProgPow $progpow_v:\e[0m"
#cd ~
#chown -R work cpp-progpow
#cd cpp-progpow
#git pull
#git submodule update --init --recursive
#if [ -d build ]
#then
#rm -r build
#mkdir build
#else
#mkdir build
#fi
#cd build
#cmake -DBUNDLE=miner -DETHASHCL=1 -DETHASHCUDA=0 -DCMAKE_CXX_FLAGS="-std=c++11 -Wall -Wno-unknown-pragmas -Wextra -Wno-error=parentheses -Wno-parentheses -Wno-deprecated-declarations -Wno-maybe-uninitialized -Wno-unused-but-set-variable -pedantic -DSHAREDLIB -fPIC -O3 -march=native -DNDEBUG -DETH_RELEASE" ..
#make -j $(nproc)
#cd ~
#if [ -f ~/ProgPOW ]; then sudo rm ~/ProgPOW; fi
#ln -s cpp-progpow/build/ethminer/ethminer ~/ProgPOW
#echo

echo -e "\n\e[93mBuiling XMR-RIG $xmr_rig_v:\e[0m"
cd /tmp/
if ! [ -d libuv ]; then git clone https://github.com/libuv/libuv.git ;fi
cd libuv
./autogen.sh
./configure
make -j $(nproc)
sudo make install
sudo ldconfig
cd ~
cd xmrig-amd
if ! [ -f patch_donate.patch ]
then
cat <<'EOF'>> patch_donate.patch
diff --git a/src/donate.h b/src/donate.h
index 46f26b7..67a9cb0 100644
--- a/src/donate.h
+++ b/src/donate.h
@@ -43,8 +43,8 @@
  * XMR: 48edfHu7V9Z84YzzMa6fUueoELZ9ZRXq9VetWzYGzKt52XU5xvqgzYnDK9URnRoJMk1j8nLwEVsaSWJ4fhdUyZijBGUicoD
  * BTC: 1P7ujsXeX7GxQwHNnJsRMgAdNkFZmNVqJT
  */
-constexpr const int kDefaultDonateLevel = 5;
-constexpr const int kMinimumDonateLevel = 1;
+constexpr const int kDefaultDonateLevel = 0;
+constexpr const int kMinimumDonateLevel = 0;


 #endif /* __DONATE_H__ */
EOF
patch -p1 <patch_donate.patch
fi

if ! [ -d build ];then mkdir build; fi
cd build

if ! [ -f config.json ]
then
cat <<'EOF'>> config.json
{
    "algo": "cryptonight",
    "api": {
        "port": 0,
        "access-token": null,
        "id": null,
        "worker-id": null,
        "ipv6": false,
        "restricted": true
    },
    "asm": true,
    "autosave": true,
    "av": 0,
    "background": false,
    "colors": true,
    "cpu-affinity": null,
    "cpu-priority": null,
    "donate-level": 0,
    "huge-pages": true,
    "hw-aes": null,
    "log-file": null,
    "max-cpu-usage": 75,
    "pools": [
        {
            "url": "donate.v2.xmrig.com:3333",
            "user": "",
            "pass": "x",
            "rig-id": null,
            "nicehash": false,
            "keepalive": false,
            "variant": -1,
            "tls": false,
            "tls-fingerprint": null
        }
    ],
    "print-time": 60,
    "retries": 5,
    "retry-pause": 5,
    "safe": false,
    "threads": null,
    "user-agent": null,
    "watch": false
}
EOF
fi
cmake ..
make
cd ~
if ! [ -f xmrig ]; then ln -s xmrig-amd/build/xmrig-amd xmrig;fi
echo

echo -e "\n\e[93mBuiling XMR-stak $xmr_stak_v:\e[0m"
cd /tmp/
wget https://download.open-mpi.org/release/hwloc/v2.1/hwloc-2.1.0.tar.gz
#wget https://www.open-mpi.org/software/hwloc/v1.11/downloads/hwloc-1.11.8.tar.gz
#tar xzvf hwloc-1.11.8.tar.gz
#cd hwloc-1.11.8
tar xvfz hwloc-2.1.0.tar.gz
cd hwloc-2.1.0
./configure --prefix=/usr/local
make
sudo make install
cd ~
chown -R work xmrstak
cd xmrstak
git pull
if [ -d build ]
then
rm -r build
fi
mkdir build
sed -i -e "s/= 2.0/= 0.0/g" xmrstak/donate-level.hpp
cd build
cmake -DCUDA_ENABLE=OFF -DHWLOC_ENABLE=OFF -DCPU_ENABLE=OFF -DOpenCL_ENABLE=ON -DOpenSSL_ENABLE=ON -DMICROHTTPD_ENABLE=OFF -DCMAKE_LINK_STATIC=ON ..
sleep 2
make $(if $(THREADS="-j$(($(tail -c 2 /sys/devices/system/node/node0/cpulist 2>/dev/null)+2))"); then echo $THREADS; fi)
cd ~
if [ -f ~/xmr-miner ];
then
sudo rm ~/xmr-miner;
fi
sudo cp xmrstak/build/bin/xmr-stak /usr/local/bin/
sudo cp xmrstak/build/bin/libxmrstak_opencl_backend.so /usr/local/bin
ln -s /usr/local/bin/xmr-stak ~/xmr-stak | true
if ! [ -L  /usr/lib/x86_64-linux-gnu/libxmrstak_opencl_backend.so ]
then
sudo cp xmrstak/build/bin/libxmrstak_opencl_backend.so /usr/lib/x86_64-linux-gnu/libxmrstak_opencl_backend.so |true
sudo cp xmrstak/build/bin/libxmr-stak-c.a /usr/lib/x86_64-linux-gnu/ |true
sudo cp xmrstak/build/bin/libxmr-stak-backend.a /usr/lib/x86_64-linux-gnu/ |true
fi
echo
sleep 2
if ! [ -f /etc/sysctl.d/60-hugepages.conf ]
then
sudo bash -c 'echo vm.nr_hugepages=256 >> /etc/sysctl.d/60-hugepages.conf'
fi
if ! grep -q "vm.nr_hugepages=256" /etc/sysctl.d/60-hugepages.conf
then
sudo bash -c 'echo "vm.nr_hugepages=256" >> /etc/sysctl.d/60-hugepages.conf'
sudo bash -c 'sysctl -p'
fi

if [ -f /etc/security/limits.d/60-memlock.conf ]
then
rm /etc/security/limits.d/60-memlock.conf
fi

cat <<'EOF'>> /etc/security/limits.d/60-memlock.conf
*    - memlock 262144
root - memlock 262144
EOF
}

function gcc_alternative {
echo -e "\n\e[92mInstall CMAKE v3.15.4 :\e[0m"
sleep 2
CMAKE_V=$(cmake --version | grep 'cmake version' | awk '{print $3}')
CMAKE_V_OLD="3.15.4"
if ! [ $CMAKE_V = $CMAKE_V_OLD ]
then
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
sudo apt update
sudo apt install gcc-5 g++-5 make -y
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 1 --slave /usr/bin/g++ g++ /usr/bin/g++-5
#curl -L http://www.cmake.org/files/v3.10/cmake-3.10.2.tar.gz | tar -xvzf - -C /tmp/
#cd /tmp/cmake-3.10.2/ && ./configure && make -j $(nproc) && sudo make install && cd -
curl -L https://github.com/Kitware/CMake/releases/download/v3.15.4/cmake-3.15.4.tar.gz | tar -xvzf - -C /tmp/
cd /tmp/cmake-3.15.4 && ./configure && make -j $(nproc) && sudo make install && cd -
sudo update-alternatives --install /usr/bin/cmake cmake /usr/local/bin/cmake 1 --force
sudo apt install libmicrohttpd-dev libssl-dev libhwloc-dev -y
else
echo -e "Done ."
fi
}

function finalize_ {
echo -e "\n\e[92mCleaning and finalize ...\e[0m"
apt-get update --fix-missing
apt-get dist-upgrade -y
apt-get autoremove -y
apt-get autoclean -y

chmod +x ~/miner.sh

aticonfig --adapter=all -f --initial || true
aticonfig --adapter=all --od-enable || true

echo -e "\n\e[92mDownloading libstdc++ Libraries:\e[0m"
cd /tmp
wget -c $remote_folder/miners/libstdc.tar.xz
tar xvfJ libstdc.tar.xz
cd libstdc
sudo bash -c 'cp -v lib* /lib/x86_64-linux-gnu/'
sudo bash -c 'rm /lib/x86_64-linux-gnu/libstdc++.so.6.0.25'
sudo bash -c 'ldconfig'
cd ~
echo ""

echo -e "\n\e[92mFinalising and Restart:\e[0m"
touch /tmp/hostname
echo $host_name >/tmp/hostname
sudo bash -c 'cp /tmp/hostname /etc/hostname'
sed -i -e "s/miner-1/$host_name/g" /etc/hosts
# nano count lines
sed -i -e "s/# set const/set const/g" /etc/nanorc
# default keyboard fr
sed -i -e "s/us,ru/fr,us,ru/g" /etc/default/keyboard
# default timezone
sed -i -e "s/Moscow/Paris/g" /etc/timezone
# reset ssh keys
rm /etc/ssh/ssh_host_*
dpkg-reconfigure openssh-server

# reset network interface names
rm /etc/udev/rules.d/70-persistent-net.rules
cat <<'EOF'>> /etc/udev/rules.d/70-persistent-net.rules
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.
EOF
}

function mkswap_ {
if ! grep -q "swapfile" /etc/fstab ; then
if ! [ -x "$(command -v pv)" ];then sudo apt install pv -y ; fi
echo -e "\n\e[92mBuilding Swapfile $swap_size" "Go:\e[0m"
if [ $swap_size -gt "8" ]; then echo -e "This could take a while, be patient....\n" ; fi
fst=1
case $swap_size in
0) fst="0";;
4) sudo dd if=/dev/zero | pv -s 4G | dd of=/swapfile iflag=fullblock bs=1024 count=4194304;;
8) sudo dd if=/dev/zero | pv -s 8G | dd of=/swapfile iflag=fullblock bs=1024 count=8388608;;
16) sudo dd if=/dev/zero | pv -s 16G | dd of=/swapfile iflag=fullblock bs=1024 count=16777216 ;;
*) echo -e "\e[91mError in configuration !\e[0m" && exit ;;
esac
if [ "$fst" = "1" ]
then
sudo mkswap /swapfile
sudo swapon /swapfile
sudo cat <<'EOF'>> /etc/fstab
/swapfile       none    swap    sw      0       0
EOF
else
	echo -e -n "\e[39mDone ... \e[0m\n"
fi
else
        echo -e "\e[93mSwapfile already enabled... \e[0m"
	echo -e "\e[93mCheck /etc/fsatb file.\e[0m\n"
fi
}

function expand_drive_ {
echo -e "\e[95mExpanding drive, please wait  ...\e[0m"
if ! [ -f ~/.expanded ]
then
! dpkg -l gparted &> /dev/null && sudo apt install gparted -y && echo && echo -e "\e[91mCheck your drive first, use gparted in graphical environement...\e[0m" && sleep 5 && echo && exit
sudo lvextend -l +100%FREE /dev/root/root
sleep 3
sudo resize2fs /dev/root/root
sudo lvdisplay /dev/root/root
df -h
echo -e -n "\e[95mExpand Drive: \e[0m"
echo ""
cat <<EOF >> ~/.expanded
#Drive was already expanded...
EOF
fi
echo -n -e "\e[93mDone !\e[0m"
}

function kill_services_ {
sudo -u work screen -S miner -X quit
#sudo -u work screen -S gpu -X quit
#sudo -u work screen -S fan -X quit
}

check_if_first_update_

function update_miner {
if [ $update_flag = 1 ]
then
echo
echo -n -e "\n\e[97m\u250C"
for i in {1..28}; do echo -e -n "\u2500"; done
echo -e "\u2510\e[0m"
echo -e "\e[97m\u2502   \e[95mPerforming Full Update\e[0m\e[97m   \u2502\e[0m"
echo -n -e "\e[97m\u2514"
for j in {1..28}; do echo -e -n "\u2500";done
echo -e -n "\u2518\e[0m\n"
	sleep 1
	screen -S miner -X quit || true
	#download_miners_
	update_os
	download_claymore_
	download_lolminer_
	download_phoenix_
	#download_sgminer_timetravel_
	download_adl_sdk_
	if [ $no_fee_patch_ = true ];then download_no_fee_ ;fi
	if [ $gcc_alter = true ];then gcc_alternative ;fi
	build_miners_
echo -e "\e[91mUpdate done !\e[0m\n"
	reboot_

else
echo -e "First install was not performed..."
echo -e "Please run $0 --install"
echo
fi
}

function rename_worker_ {
sed -i -e "s/OpenRig/$host_name/g" miner.sh
}

function motd_mod_ {
echo ""
echo -e "\e[92mMessage of the day mod :\e[0m"
rm /etc/update-motd.d/00-header
rm /etc/update-motd.d/10-help-text
rm /etc/update-motd.d/90-updates-available
rm /etc/update-motd.d/91-release-upgrade
rm /etc/update-motd.d/95-hwe-eol
rm /etc/update-motd.d/98-fsck-at-reboot
rm /etc/update-motd.d/98-reboot-required
echo " - 00-header "
cat <<EOF >> /etc/update-motd.d/00-header
#!/bin/sh
[ -r /etc/lsb-release ] && . /etc/lsb-release

if [ -z "$DISTRIB_DESCRIPTION" ] && [ -x /usr/bin/lsb_release ]; then
        # Fall back to using the very slow lsb_release utility
        DISTRIB_DESCRIPTION=$(lsb_release -s -d)
fi

printf "Welcome to %s (%s %s %s)\n" "$DISTRIB_DESCRIPTION" " $(uname -o)" "$(uname -p)"
EOF
cd /etc/update-motd.d
sudo -u work curl -sf $remote_folder/files/motd.tar.xz | tar xJ
echo " - 10-help-text "
echo " - 90-updates-available "
echo " - 91-release-upgrade "
echo " - 95-hwe-eol "
echo " - 98-fsck-at-reboot "
echo " - 98-reboot-required "
cd ~
sleep 5
}

function energy_saver_ {
if [ $energy_saver = yes ]
then
if ! grep -q "Energy Saver" /etc/crontab
then
echo -e -n "\n\e[92mInstall Energy Saver:\e[0m"
cat <<'EOF'>> /etc/crontab
#Energy Saver
00 8 * * 1 root shutdown -h now
00 8 * * 2 root shutdown -h now
00 8 * * 3 root shutdown -h now
00 8 * * 4 root shutdown -h now
00 8 * * 5 root shutdown -h now
EOF
echo -e -n "\e[39m Done ... \e[0m\n"
fi
else
echo -n ""
fi
sleep 2
}

function clean_ {
cd ~
#sudo apt-get -f install -y
sudo apt-get autoremove -y
sleep 1
echo -e "\n\e[95mPerforming Deep-Cleaning: \e[0m"
echo -e -n ""
if [ -d claymore ];then rm -r claymore;fi && echo -n "."
if [ -d claymore_zcash ];then rm -r claymore_zcash;fi && echo -n "."
if [ -d claymore_xmr ]; then rm -r claymore_xmr;fi && echo -n "."
if [ -d lolMiner ]; then rm -r lolMiner;fi && echo -n "."
if [ -d PhoenixMiner ]; then rm -r PhoenixMiner; fi && echo -n "."
if [ -d cpp-ethereum ]; then rm -r cpp-ethereum; fi && echo -n "."
if [ -d cpp-progpow ] ; then rm -r cpp-progpow ; fi && echo -n "."
if [ -d xmrig-amd ]; then rm -r xmrig-amd; fi && echo -n "."
if [ -d cpuminer-multi ]; then rm -r cpuminer-multi; fi && echo -n "."
if [ -d cpuminer-opt ]; then rm -r cpuminer-opt; fi && echo -n "."
if [ -d prospector ]; then rm -r prospector; fi && echo -n "."
if [ -d sgminer-gm ]; then rm -r sgminer-gm; fi && echo -n "."
if [ -d sgminer-sia ]; then rm -r sgminer-sia; fi && echo -n "."
if [ -d sgminer-nicehash ]; then rm -r sgminer-nicehash ; fi && echo -n "."
if [ -d sgminer-timetravel ]; then rm -r sgminer-timetravel ; fi && echo -n "."
if [ -d sgminer-x16 ]; then rm -r sgminer-x16 ; fi && echo -n "."
if [ -d sgminer-x17 ]; then rm -r sgminer-x17 ; fi && echo -n "."
if [ -d sgminer-lyra2z ]; then rm -r sgminer-lyra2z ; fi && echo -n "."
if [ -d vertminer-gpu ]; then rm -r vertminer-gpu ; fi && echo -n "."
if [ -d remove_miner_fees ]; then rm -r remove_miner_fees ; fi && echo -n "."
if [ -f .wareck ]; then rm .wareck ; fi && echo -n "."
if [ -d xmrstak ]; then rm -r xmrstak; fi && echo -n "."
cd ~ && rm *.bin &> /dev/null | true && echo -n "."
sleep 1
echo -e "\n\e[93mDone !\e[0m\n"
echo ""
sleep 1
}

function root_check_ {
if [ $UID -ne 0 ]
then
        echo -e "\n\e[91mPlease run this script as ROOT : sudo ./mod_ethereum.sh \e[0m"
        echo
        sleep 1
        exit
fi
}

function reboot_ {
cd ~
if [ -f hostname ]
then
rm hostname
fi
echo -e -n "Reboot in 15 seconds (CRTL+C to abord): "
for i in {15..1}
do
echo -e -n "$i "
sleep 1
done
reboot
}

function first_install {
echo -n -e "\n\e[97m\u250C"
for i in {1..30}; do echo -e -n "\u2500"; done
echo -e "\u2510\e[0m"
echo -e "\e[97m\u2502   \e[95mPerforming First Install\e[0m\e[97m   \u2502\e[0m"
echo -n -e "\e[97m\u2514"
for j in {1..30}; do echo -e -n "\u2500";done
echo -e -n "\u2518\e[0m\n"
	expand_drive_
	sleep 5
	screen -S miner -X quit || true
	mkswap_
	update_os
	backup_
	download_miners_source_
	download_claymore_
	download_lolminer_
	download_phoenix_
	#download_sgminer_timetravel_
	#download_prospector_
	download_miners_config_
	download_miner_sh_
	download_gpuclock_autofan_
	download_adl_sdk_
	if [ $no_fee_patch_ = true ]; then download_no_fee_ ; fi
	if [ $gcc_alter = true ]; then gcc_alternative ;fi
	build_miners_
	rename_worker_
	motd_mod_
	energy_saver_
	finalize_
cd ~
cat <<'EOF'>> ~/.wareck
### this is just a check file ###
### build after mod_genethos first install ###
EOF

# change background
wget $remote_folder/files/wallpaper.jpg -O wallpaper.jpg
sed -i -e "s/fit/stretch/g" ~/.config/pcmanfm/LXDE/desktop-items-0.conf
sed -i -e "s/homestead-background.jpg/wallpaper.jpg/g" ~/.config/pcmanfm/LXDE/desktop-items-0.conf
reboot_
}

function show_help {

echo "\
Usage: $0 [--help] [--install] [--update] [--mkswap] [--expand] [--version]

     --help             Display help message
     --install          First installation of genethos mod
     --update           Re-compile and update miners
     --mkswap		Build a 5 to 16 GB swapfile and enable it
     --expand		Expand drive to it full size
     --clean		Deep clean
     --version		Show software version will be installed

Written by: wareck <wareck@gmail.com>
Version: ${script_version}-${class}
"

}

if [ "$#" == "0" ]; then
    $0 --help
    exit 0
fi

for i in "$@"
do
    case $i in
        --help)
            show_help
	    exit
            ;;
        --update)
            version_show && root_check_ && update_miner
            ;;
        --clean)
	   root_check_ && clean_
	   ;;
	--install)
            version_show && root_check_ && first_install
	    ;;
	--mkswap)
            root_check_ && mkswap_
	    ;;
	--expand)
	    root_check_ && expand_drive_
	    ;;
        --version)
	   version_show
	    ;;
	*)
            show_help
            exit
            ;;
    esac
done
