#!/bin/bash
Version=1.8
Release=18/11/2018
set -e
echo -e "\e[93mMiniMiner build $Version ($Release)\e[0m"
sleep 1

echo -e "\n\e[95mSystem Update :\e[0m"
sudo apt-get update
sudo apt install screen git build-essential autoconf automake libtool pkg-config libcurl4-openssl-dev libudev-dev libjansson-dev uthash-dev libncursesw5-dev libncurses5-dev libusb-1.0-0-dev libevent-dev libmicrohttpd-dev libhidapi-dev -y
sudo rpi-update
echo "Done."

echo -e "\n\e[95mDownload sources:\e[0m"
if [ ! -d bfgminer ];then git clone https://github.com/wareck/bfgminer.git && echo;fi
if [ ! -d cgminer-lketc ];then git clone https://github.com/wareck/cgminer-lketc.git && echo;fi
if [ ! -d cgminer-x11 ]
then
mkdir cgminer-x11
cd cgminer-x11
wget https://raw.githubusercontent.com/wareck/X11-Miner/master/bin/pi_cgminer/Sha1
wget https://raw.githubusercontent.com/wareck/X11-Miner/master/bin/pi_cgminer/cgminer
wget https://raw.githubusercontent.com/wareck/X11-Miner/master/bin/pi_cgminer/README
chmod +x cgminer
mv cgminer cgminer-x11
cd ..
echo
fi
echo "Done."

echo -e "\n\e[95mBuild bfgminer:\e[0m"
cd bfgminer
./autogen.sh
./configure --enable-scrypt
make
echo "Done."
cd ..

echo -e "\n\e[95mBuild cgminer zeus/lketc/gridseed :\e[0m"
cd cgminer-lketc
./autogen.sh
./configure --enable-scrypt --enable-zeus --enable-lketc
make
echo "Done."
cd ..

echo -e "\n\e[95mGen configuration files:\e[0m"
if [ ! -f x11.conf ]
then
echo "x11.conf"
cat <<'EOF'>> x11.conf
{
"pools" : [
        {
                "url" : "stratum+tcp://eu.multipool.us:11111",
                "user" : "wareck.x11",
                "pass" : "x"
        },
        {
                "url" : "stratum+tcp://x11.eu.nicehash.com:3336",
                "user" : "16F8V2EnHCNPVQwTGLifGHCE12XTnWPG8G.X11",
                "pass" : "x"
        }
]
,
"hotplug" : "5",
"queue" : "1",
"scan-time" : "0"
}
EOF
fi

if [ ! -f scrypt.conf ]
then
echo "scrypt.conf"
cat <<'EOF'>> scrypt.conf
{
"pools" : [
        {
                "url" : "stratum+tcp://eu.multipool.us:7777/#xnsub#skipcbcheck",
                "user" : "wareck.scrypt",
                "pass" : "x"
        },
	{
                "url" : "stratum+tcp://eu.multipool.us:7777",
                "user" : "wareck.scrypt",
                "pass" : "x"
        }
]
,
"expiry" : "60",
"failover-switch-delay" : "300",
"hotplug" : "10",
"queue" : "1",
"scan-time" : "30",
"scrypt" : true
}
EOF
fi

if [ ! -f sha256.conf ]
then
echo "sha256.conf"
cat <<'EOF'>> sha256.conf
{
"pools" : [
        {
                "url" : "stratum+tcp://eu.multipool.us:8888",
                "user" : "wareck.sha256",
                "pass" : "x"
        }
]
,
"expiry" : "60",
"failover-switch-delay" : "300",
"hotplug" : "10",
"queue" : "1",
"scan-time" : "30"
}
EOF
fi
echo "Done."


echo -e "\n\e[95mGenerate rc.local file:\e[0m"
if ! grep "Start Mining" /etc/rc.local >/dev/null
then
sudo bash -c 'sed -i -e "s/exit 0//g" /etc/rc.local'
sudo bash -c 'cp /etc/rc.local rc.local'
sudo bash -c 'chown pi rc.local'

cat <<'EOF'>> alpha
set -e
echo "\n\e[92mStart Mining\e[0m:"

#echo "\e[97mbfgminer (sha256)\e[0m"
#su pi -c "screen -dmS bfgminer /home/pi/bfgminer/bfgminer -c /home/pi/sha256.conf -S all"

echo "\e[97mMoonlander (scrypt)\e[0m"
su pi -c "screen -dmS bfgminer /home/pi/bfgminer/bfgminer --scrypt -c /home/pi/scrypt.conf -S all -S all --set MLD:clock=600"

echo "\e[97mZeus (scrypt)\e[0m"
su pi -c "screen -dmS cgminer /home/pi/cgminer-lketc/cgminer --scrypt -c /home/pi/scrypt.conf --lketc-chips 1 --lketc-clock 290"

echo "\e[97mX11 (X11)\e[0m"
su pi -c "screen -dmS x11 /home/pi/cgminer-x11/cgminer-x11 --x11 -c /home/pi/x11.conf --dr1-clk 400 --dr1-fan LV3 -S /dev/ttyACM0 -S /dev/ttyACM1 --du1"

echo ""
echo "use screen -r to show miners"
echo ""
exit 0
EOF

cat alpha >> rc.local
rm alpha
sudo bash -c 'cp rc.local /etc/rc.local'
rm rc.local
fi
echo "Done."

echo -e "\n\e[95mAdd udev rules:\e[0m"
wget -q --show-progress http://folivier.homelinux.org/miniminer/70-bfgminer.rules
wget -q --show-progress http://folivier.homelinux.org/miniminer/01-cgminer.rules
sudo bash -c 'mv 01-cgminer.rules /etc/udev/rules.d/'
sudo bash -c 'mv 70-bfgminer.rules /etc/udev/rules.d/'
sudo bash -c 'usermod -a -G dialout,plugdev,tty pi'
echo "Done"

echo -e "\n\e[95mInstall message banner :\e[0m"
ident=`cat /proc/device-tree/model | mawk '{print $1" "$2" "$3""$5" v"$7}'`
if [ -f /tmp/motd ]; then rm /tmp/motd ;fi
cat <<EOF>> /tmp/motd
MiningPi v$Version

Use screen -r to show mining screen.

EOF
sudo bash -c 'mv /tmp/motd /etc/motd | sudo -s'
if  grep "raspberrypi" /etc/hostname >/dev/null;then sudo sed -i -e "s/raspberrypi/MiningPi/g" /etc/hostname ;fi
if  grep "raspberrypi" /etc/hosts >/dev/null;then sudo sed -i -e "s/raspberrypi/MiningPi/g" /etc/hosts;fi
echo -e "Done."

echo -e "\n\e[97mBuild was done.\e[0m"
echo -e "\e[97mReboot or use sudo /etc/rc.local to start mining.\e[0m"
echo ""
