#!/bin/bash
####################################################################
## 
## 
##   ███    ███ ██ ███    ██ ███████ ██ ████████  ██████  ██████  
##   ████  ████ ██ ████   ██ ██      ██    ██    ██    ██ ██   ██ 
##   ██ ████ ██ ██ ██ ██  ██ █████   ██    ██    ██    ██ ██████  
##   ██  ██  ██ ██ ██  ██ ██ ██      ██    ██    ██    ██ ██   ██ 
##   ██      ██ ██ ██   ████ ███████ ██    ██     ██████  ██   ██ 
##                                                                                                                            
##  
##  
####################################################################
if [ "$(id -u)" -ne 0 ]; then
        echo 'This script must be run by root' >&2
        exit 1
fi

M_USER="mineitor"
M_HOME="/home/$M_USER"
M_URL="https://mineitor.com"
M_firmware_URL="https://mineitor.com/get_firmware"
ARCH=`uname -m`
APP="app.tar.gz"


if [ $ARCH = "i386" ]; then
	ARCH="32"
elif    [ $ARCH = "i486" ]; then
	ARCH="32"
elif    [ $ARCH = "i586" ]; then
	ARCH="32"
elif    [ $ARCH = "i686" ]; then
	ARCH="32"
elif [ $ARCH = "x86_64" ]; then
	ARCH="64"
elif [[ $ARCH == *"arm"* ]]; then
	ARCH="arm"
else
	ARCH="64"
fi

if [[ $ARCH == "arm" ]]; then
  APP="app_arm.tar.gz"
fi
if [[ $ARCH == "32" ]]; then
  APP="app_i386.tar.gz"
fi
#--------------------------------------------------
# Update Server
#--------------------------------------------------
echo -e "\n---- Update Server ----"
sudo apt-get update
# sudo apt-get upgrade -y 

#--------------------------------------------------
# Install Dependencies
#--------------------------------------------------

echo -e "\n--- Installing Dependencies --"
sudo apt-get install curl git python3 build-essential wget python3-dev python3-venv libxslt1-dev arp-scan curl dialog gcc libffi-dev libsnmp-dev libssl-dev monit net-tools policykit-1 python-dev python-netaddr python-setproctitle snmp-mibs-downloader unzip openssh-server libssl-dev libffi-dev nmap -y
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt-get update
if [[ $ARCH == "32" ]]; then
curl https://bootstrap.pypa.io/pip/3.6/get-pip.py  -o /tmp/get-pip.py
  sudo apt-get install python3.6 python3.6-dev python3.6-venv -y
  sudo apt-get install python3-distutils -y
  sudo apt-get install python3-distutils-extra -y
  sudo apt-get install libzip-dev libsasl2-dev python3-setuptools gdebi -y
elif [[ $ARCH == "64" ]]; then
  curl https://bootstrap.pypa.io/pip/3.6/get-pip.py  -o /tmp/get-pip.py
  sudo apt-get install python3.6 -y
  sudo apt-get install python2.7 -y
  sudo apt-get install python3.6-distutils python3-distutils-extra python3.6-dev -y
  sudo apt-get install libzip-dev libsasl2-dev python3-setuptools gdebi -y
else
  curl https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
  sudo apt-get install python3.7 -y
  sudo apt-get install python2.7 -y
  sudo apt-get install python3-distutils python3-distutils-extra python3.7-dev -y
  sudo apt-get install libzip-dev libsasl2-dev python3-setuptools gdebi -y
fi

echo -e "\n---- Install python requirements ----"


if [[ $ARCH == "32" ]]; then
  sudo python3.6 /tmp/get-pip.py
  sudo pip3 install -r ${M_URL}/installer/requirements3.txt
else
  sudo python3.6 /tmp/get-pip.py
  sudo pip3.6 install -r ${M_URL}/installer/requirements3.txt
fi

if [[ $ARCH == "arm" ]]; then
  sudo pip3 install Adafruit-DHT
  sudo pip3 install sense_hat
fi

#--------------------------------------------------
# Create Mineitor User 
#--------------------------------------------------

echo -e "\n---- Create system user ----"
sudo adduser --system --quiet --shell=/bin/bash --home=$M_HOME --gecos $M_USER --group $M_USER
#The user should also be added to the sudo'ers group.
sudo adduser $M_USER sudo


#--------------------------------------------------
# Install Mineitor
#--------------------------------------------------

echo -e "\n==== Installing Mineitor ====" 

sudo wget ${M_URL}/installer/${APP} -O ${M_HOME}/${APP}
sudo tar --overwrite -xpzf ${M_HOME}/${APP} -C ${M_HOME}

echo -e "\n---- Setting permissions on home folder ----"

sudo chown -R $M_USER:$M_USER $M_HOME/*


#--------------------------------------------------
# Adding Mineitor as a deamon (initscript)
#--------------------------------------------------

echo -e "* Security Init File"
sudo mv ${M_HOME}/mineitor /etc/init.d/mineitor
sudo chmod 755 /etc/init.d/mineitor
sudo chown root: /etc/init.d/mineitor

sudo mv ${M_HOME}/monitrc /etc/monit/
sudo chmod 600 /etc/monit/monitrc
sudo chown root: /etc/monit/monitrc

sudo mv ${M_HOME}/webserver /etc/init.d/webserver
sudo chmod 755 /etc/init.d/webserver
sudo chown root: /etc/init.d/webserver

sudo mv ${M_HOME}/updater /etc/init.d/updater
sudo chmod 755 /etc/init.d/updater
sudo chown root: /etc/init.d/updater

sudo mkdir /templates
sudo mkdir -p /home/mineitor/server/templates
sudo cp ${M_HOME}/template.html /templates
sudo mv ${M_HOME}/template.html /home/mineitor/server/templates

sudo chmod +x /home/mineitor/firmware/main.py
sudo chmod +x /home/mineitor/firmware/updater.py
sudo chmod +x /home/mineitor/server/server.py

echo -e "* Enabling Mineitor on Startup"
sudo update-rc.d webserver defaults
sudo update-rc.d mineitor defaults

sudo rm ${M_HOME}/${APP}

# if [[ $ARCH == "32" ]]; then
  # sudo sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"/g' /etc/default/grub
  # sudo grub-mkconfig -o /boot/grub/grub.cfg
# fi

# if [[ $ARCH == "64" ]]; then
  # sudo sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"/g' /etc/default/grub
  # sudo grub-mkconfig -o /boot/grub/grub.cfg
# fi

# echo -e "* Starting Mineitor Service"
# sudo su root -c "/etc/init.d/webserver start"
# sudo su root -c "/etc/init.d/mineitor start"
echo "-----------------------------------------------------------"
echo "Done! The Mineitor controller is up and running."
echo "You can continue with the panel and register your controller/farm"
echo "You can find controller serial number at :"
echo "   http://ip-address-of-this-machine"
echo "To start Controller please reboot the system"
echo "-----------------------------------------------------------"
