#!/bin/bash

# Detects OS architecture and pulls appropriate Dotnet 6 for your OS
arm64=$(uname -a | grep 64 | grep -i 'armv\|aarch64' | wc -l)
linux64=$(uname -a | grep 64 | grep -vi armv | grep -vi aarch64 | wc -l)

if [ $arm64 -eq 1 ]; then
 short='aspnetcore-runtime-6.0.12-linux-arm64.tar.gz'
 full='https://download.visualstudio.microsoft.com/download/pr/8072e219-57e4-48c3-b138-2b4067844ab2/b0712ad06fd0740963bf4ba2eff7f5ea/aspnetcore-runtime-6.0.12-linux-arm64.tar.gz'
fi
if [ $linux64 -eq 1 ]; then
 short='aspnetcore-runtime-6.0.12-linux-x64.tar.gz'
 full='https://download.visualstudio.microsoft.com/download/pr/4ba0f30d-0a77-4997-8d8d-1b113d60253b/5caeeb07572b0b6a26f2a82f7a4eb31d/aspnetcore-runtime-6.0.12-linux-x64.tar.gz'
fi
whodis=$(whoami)

CWD=$(pwd)

# Check for screen
havescreen=$(screen -version | wc -l)

if [ $havescreen -eq 0 ]; then
 echo 'Missing screen. Please install screen with "apt install screen" or "dnf install screen"'
 return 1
 exit 1
fi


# Build a systemd service file
echo '[Unit]' > etherFAX.ERIS.service
echo 'Description=etherFAX Remote Integration Service (ERIS) for Linux' >> etherFAX.ERIS.service
echo 'After=network.target' >> etherFAX.ERIS.service
echo '' >> etherFAX.ERIS.service
echo '[Service]' >> etherFAX.ERIS.service
echo 'Type=simple' >> etherFAX.ERIS.service
echo "User=$whodis" >> etherFAX.ERIS.service
echo "Group=$whodis" >> etherFAX.ERIS.service
echo "WorkingDirectory=$CWD/EtherFax.Integration.Service" >> etherFAX.ERIS.service
echo "ExecStart=$CWD/StartERIS.sh" >> etherFAX.ERIS.service
echo "Environment=\"DOTNET_ROOT=$HOME/dotnet\"" >> etherFAX.ERIS.service
echo "Environment=\"PATH=$PATH:$HOME/dotnet\"" >> etherFAX.ERIS.service
echo 'Restart=on-failure' >> etherFAX.ERIS.service
echo 'RemainAfterExit=yes' >> etherFAX.ERIS.service
echo "AmbientCapabilities=CAP_NET_BIND_SERVICE" >> etherFAX.ERIS.service
echo '' >> etherFAX.ERIS.service
echo '[Install]' >> etherFAX.ERIS.service
echo 'WantedBy=multi-user.target' >> etherFAX.ERIS.service


echo '#!/bin/sh' > $CWD/StartERIS.sh
echo 'cd /root/EtherFax.Integration.Service' >> $CWD/StartERIS.sh
echo 'screen -d -m dotnet EtherFax.Integration.Service.dll' >> $CWD/StartERIS.sh

sudo chmod +x $CWD/StartERIS.sh


# Installs Dotnet determined from above to /dotnet, sets permissions if not root, links in /usr/bin and runs a dotnet --info to test output

if [ $arm64 -eq 1 ]; then
 wget https://downloads.etherfax.net/installers/etherfax/eris/eris-linux-arm64_1.1.162.tgz
 tar -xvzf eris-linux-arm64_1.1.162.tgz
 mv eris-linux-arm64_1.1.162 EtherFax.Integration.Service
fi
if [ $linux64 -eq 1 ]; then
 wget https://downloads.etherfax.net/installers/etherfax/eris/eris-linux-x64_1.1.162.tgz
 tar -xvzf eris-linux-x64_1.1.162.tgz
 mv eris-linux-x64_1.1.162 EtherFax.Integration.Service
fi


chmod u+x EtherFax.Integration.Service
sudo cp etherFAX.ERIS.service /etc/systemd/system
wget $full
mkdir $HOME/dotnet
tar zxf $short -C "$HOME/dotnet"

echo 'export DOTNET_ROOT=$HOME/dotnet' >> ~/.bashrc
echo 'export PATH=$PATH:$HOME/dotnet' >> ~/.bashrc

# set capabilities for services files to run as non-root user
sudo setcap 'cap_net_admin,cap_net_bind_service,cap_dac_override+eip' $HOME/dotnet/dotnet
sudo setcap 'cap_net_admin,cap_net_bind_service,cap_dac_override+eip' $CWD/EtherFax.Integration.Service.dll

# trigger systemd reload and enable our service
sudo systemctl daemon-reload
sudo systemctl enable etherFAX.ERIS.service

cd /usr/bin
sudo ln -s $HOME/dotnet/dotnet


dotnet --info


echo ""
echo ""
echo ""
echo " --------------------------------------------------------------------- "
echo "ERIS has been Installed to $CWD. It has also been registered as a systemd service named etherFAX.ERIS"
echo " - To start ERIS   : sudo systemctl start etherFAX.ERIS"
echo " - To stop ERIS    : sudo systemctl stop etherFAX.ERIS"
echo " - To check status : sudo systemctl status etherFAX.ERIS"
echo " - To disable ERIS : sudo systemctl disable etherFAX.ERIS.service"
echo " - By default, logs write to $CWD/shared/logs/eris.log"
echo " --------------------------------------------------------------------- "
echo " - To log into ERIS, visit http://localhost:8000 if on this same machine, or http://thismachinesIP:8000 from another machine"
