#!/bin/bash
set -e
script_version=0.2
rdate=20/10/2017

###########

Hostname_=raspberrypi
swap_size_=0 # 0 to 2048
IpV6_=NO
Watchdog_=NO
Wifi_WPA_=YES
Ntp_=YES
Rpi_update_=NO
Autologin_=NO

###########
# profils #
###########

Mining_=NO
Bitcoin_=NO
Clone_=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     Raspberry Auto-Setup  \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 ""
echo ""
echo -e "\e[97mConfiguration:\e[0m"
echo -e "\e[97m--------------\e[0m"
echo -e "- Hostname          : $Hostname_"
echo -e "- IpV6              : $IpV6_"
echo -e "- Watchdog          : $Watchdog_"
echo -e "- Wifi + WPA config : $Wifi_WPA_"
echo -e "- RPI-Update        : $Rpi_update_"
echo -e "- Ntp-client        : $Ntp_"
echo -e "- AutoLogin         : $Autologin_"
if [ $swap_size_ = 0 ]
then
echo -e "- Swap              : disabled"
else
echo -e "- Swap size         : $swap_size_" "Mo"
fi
if ! [ "$swap_size_" -ge 0 -a "$swap_size_" -le 2048 ]
then
echo
echo "Error in configuration, please check variables"
exit 0
fi
echo ""
echo -e "\e[97mProfils:\e[0m"
echo -e "\e[97m--------\e[0m"
echo -e "- Mining            : $Mining_"
echo -e "- Bitcoin & Crypto  : $Bitcoin_"
echo -e "- Pi Clone          : $Clone_"


function check_if_first_update_ {
if ! [ -f /tmp/wareck ]
then
md5sum /home/pi/pi_deploy.sh >/tmp/wareck
echo ""
echo -e "\e[91mPlease Check configuration before run !\e[0m"
sleep 1
exit 0
else
sleep 3
fi
}


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
}


root_check_

function swap {
echo -e "\n\e[95mBuild Swap: \e[0m"
apt-get install dphys-swapfile -y
sudo sed -i -e "s/CONF_SWAPSIZE=100/CONF_SWAPSIZE=$swap_size_/g" /etc/dphys-swapfile
sudo dphys-swapfile setup
sudo dphys-swapfile swapon
echo "Done !"
}

function No_IPV6 {
echo -e "\n\e[95mRemove IPV6 support: \e[0m"
if ! grep "net.ipv6.conf.all.disable_ipv6 = 1" /etc/sysctl.conf
then
echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf
fi
sysctl -p
echo "Done !"
}

function Wifi {
echo -e "\n\e[95mWifi setup: \e[0m"
rm /etc/wpa_supplicant/wpa_supplicant.conf
cat <<'EOF'>> /etc/wpa_supplicant/wpa_supplicant.conf
country=FR
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
        ssid="Freebox-57301A"
        psk="colligavit5-terrebo7-mutescunt7-causea5"
        proto=RSN
        key_mgmt=WPA-PSK
        pairwise=CCMP
        auth_alg=OPEN
        priority=1
}
network={
        ssid="anthill"
        psk="miner2326"
        proto=RSN
        key_mgmt=WPA-PSK
        pairwise=CCMP
        auth_alg=OPEN
        priority=1
}
EOF
rm /etc/network/interfaces
cat <<'EOF'>> /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
# Please note that this file is written to be used with dhcpcd
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
# Include files from /etc/network/interfaces.d:

source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

iface eth0 inet manual

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
EOF
ifdown wlan0
ifup wlan0
echo "Done !"
}

function update_ {
echo -e "\n\e[95mUpdate and common setup: \e[0m"
apt-get update
apt-get upgrade -y
sed -i -e "s/# set const/set const/g" /etc/nanorc
sed -i -e "s/XKBLAYOUT=\"gb\"/XKBLAYOUT=\"fr\"/g" /etc/default/keyboard
echo "Done !"
}

function Mining {
echo -e "\n\e[95mInstall libraries for mining software: \e[0m"
#apt-get update
apt-get install build-essential autoconf automake libtool pkg-config libcurl4-openssl-dev libudev-dev libusb-1.0-0-dev libncurses5-dev  -y
apt-get install git screen htop libjansson-dev uthash-dev libncursesw5-dev libevent-dev libmicrohttpd-dev -y
cd /home/pi
if ! [ -d /home/pi/hidapi ]
then
sudo -u pi git clone https://github.com/signal11/hidapi.git
cd /home/pi/hidapi
./bootstrap
./configure
make
make install
fi
echo "Done!"
}

function Bitcoin {
echo -e "\n\e[95mInstall libraries for cryptocurrency software: \e[0m"
apt-get update
sudo apt-get install build-essential libboost-dev libboost-system-dev libboost-filesystem-dev libboost-program-options-dev -y
sudo apt-get install libboost-thread-dev libssl-dev libdb++-dev libminiupnpc-dev git g++ g++-4.6 -y
sudo apt-get install autoconf libevent-dev libtool libssl-dev libboost-all-dev libminiupnpc-dev git -y
echo "Done !"
}

function Watchdog {
echo -e "\n\e[95mEnabling Hard Watchdog: \e[0m"
apt-get install watchdog -y
update-rc.d watchdog enable
apt-get install watchdog chkconfig -y
chkconfig watchdog on
/etc/init.d/watchdog start
sed -i -e "s/#watchdog-device/watchdog-device/g" /etc/watchdog.conf
sed -i -e "s/#interval             = 1/interval            = 4/g" /etc/watchdog.conf
sed -i -e "s/#RuntimeWatchdogSec=0/RuntimeWatchdogSec=14/g" /etc/systemd/system.conf
if ! [ -f /etc/modprobe.d/bcm2835_wdt.conf ]
then
touch /etc/modprobe.d/bcm2835_wdt.conf
echo "alias char-major-10-130 bcm2835_wdt" /etc/modprobe.d/bcm2835_wdt.conf
echo "alias char-major-10-131 bcm2835_wdt" /etc/modprobe.d/bcm2835_wdt.conf
echo "bcm2835_wdt" >>/etc/modules
fi

echo "Done !"
}

function rpi_clone {
echo -e "\n\e[95mEnabling rpi-clone: \e[0m"
sudo apt-get install dosfstools -y
cd /home/pi
if ! [ -d /home/pi/rpi-clone ]
then
git clone https://github.com/billw2/rpi-clone.git
fi
echo "Done !"
}

function Ntp {
echo -e "\n\e[95mEnabling ntp-client: \e[0m"
apt-get install ntp
/etc/init.d/ntp restart
echo "Done !"
}

function Rpi_update {
echo -e "\n\e[95mRpi-Update : \e[0m"
rpi-update
echo "Done !"
}

function Autologin {
echo -e "\n\e[95mEnabling Autologin: \e[0m"
ln -fs /etc/systemd/system/autologin@.service /etc/systemd/system/getty.target.wants/getty@tty1.service
echo "Done !"
}

check_if_first_update_
update_
if [ $Wifi_WPA_ = "YES" ] ; then Wifi ; fi
if [ $Ntp_ = "YES" ]; then Ntp; fi
if ! [ $swap_size_ = 0 ]; then swap; fi
if [ $IpV6_ = "NO" ]; then No_IPV6; fi
if [ $Mining_ = "YES" ]; then Mining; fi
if [ $Bitcoin_ = "YES" ]; then Bitcoin; fi
if [ $Watchdog_ = "YES" ]; then Watchdog; fi
if [ $Clone_ = "YES" ]; then rpi_clone; fi
if [ $Rpi_update_ = "YES" ]; then Rpi_update; fi
if [ $Autologin_ = "YES" ]; then Autologin; fi

echo ""
echo -e "\e[93mEverything is finished.\e[0m"
echo -e "\e[93mNow, press a key to start raspi-config and finalize tuning\e[0m"
echo -e "\e[93mDon't forget to reboot !\e[0m"
sed -i -e "s/raspberrypi/$Hostname_/g" /etc/hostname
read -p ""

raspi-config
