#!/usr/bin/env bash
#!/bin/bash

set -e

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
self=`basename "$0"`
apiSecure=
apiDomain=
authenticationToken=
timestamp=$(date +%s)
wrapperDownloadUri="https://download.gamedash.io/daemon/wrapper/versions/latest.jar"
installDir="/usr/lib/gamedash/daemon"
systemdFile="/etc/systemd/system/gamedash-daemon.service"
keyStoreAlias=$timestamp
keyStoreFile="${installDir}/keystore.jks"
keyStorePassword="gamedash-daemon-${timestamp}"

for ARGUMENT in "$@"
do

    KEY=$(echo $ARGUMENT | cut -f1 -d=)
    VALUE=$(echo $ARGUMENT | cut -f2 -d=)

    case "$KEY" in
            apiDomain)              apiDomain=${VALUE} ;;
            authenticationToken)    authenticationToken=${VALUE} ;;
            apiSecure)    			apiSecure=${VALUE} ;;
            *)
    esac


done

if [[ ! -n $apiDomain ]]; then
	echo "API domain must be set"
	exit 1
fi

if [[ ! -n $authenticationToken ]]; then
	echo "Authentication token must be set"
	exit 1
fi

if [[ ! -n $apiSecure ]]; then
	apiSecure=true
fi

if [[ $(getconf LONG_BIT) -ne 64 ]]; then
	echo "Only 64 bit operating systems are currently supported"
	exit 1
fi

if [[ -f /etc/redhat-release ]]; then

	#REDHAT

	sudo yum -y update

	if [[ ! -f "/usr/bin/curl" ]]; then

		echo "Installing cURL..."

	    sudo yum -y install curl

	    echo "${green}Installed cURL${reset}"

	fi

	if [[ ! -f "/usr/lib/jvm/jre-11-openjdk/bin/java" ]]; then

		echo "Installing Java..."

	    sudo yum -y install java-11-openjdk

	    echo "${green}Java installed${reset}"

	fi

fi

if [[ -f /etc/lsb-release ]]; then

	#DEBIAN

	sudo apt-get -y update

	if [[ ! -f "/usr/bin/curl" ]]; then

		echo "Installing cURL..."

	    sudo apt-get -y install curl

	    echo "${green}Installed cURL${reset}"

	fi

	if [[ ! -f "/usr/lib/jvm/java-11-openjdk-amd64/bin/java" ]]; then

		echo "Installing Java..."

	    sudo apt-get -y install openjdk-11-jre-headless

	    echo "${green}Java installed${reset}"

	fi

fi

if [[ -f ${keyStoreFile} ]]; then
	rm ${keyStoreFile}
fi

mkdir -p ${installDir}

cd ${installDir}

curl --fail --show-error -s ${wrapperDownloadUri} > ${installDir}/wrapper.jar

if [[ -f /etc/redhat-release ]]; then

	#REDHAT

	javaPath=/usr/lib/jvm/jre-11-openjdk/bin/java

fi

if [[ -f /etc/lsb-release ]]; then

	#UBUNTU

	javaPath=/usr/lib/jvm/java-11-openjdk-amd64/bin/java

fi

echo "Creating gamedash-daemon service..."

sudo touch ${installDir}/gamedash-daemon.service

sudo cat > ${installDir}/gamedash-daemon.service << EOL
[Unit]
Description=GameDash Daemon
[Service]
User=root
WorkingDirectory=${installDir}
ExecStart=${javaPath} -Xms512M -Xmx2048M -jar ${installDir}/wrapper.jar
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
EOL

sudo chmod 644 ${installDir}/gamedash-daemon.service

if [[ ! -f "$systemdFile" ]]; then
	ln -s ${installDir}/gamedash-daemon.service ${systemdFile}
fi

sudo systemctl daemon-reload

sudo keytool -genkey -alias ${keyStoreAlias} -keyalg RSA -keystore ${keyStoreFile} -dname "CN=gamedash.daemon" -storepass ${keyStorePassword}

sudo "${javaPath}" -jar "${installDir}/wrapper.jar" -install-only \
	-installer-keystore-file "${keyStoreFile}" \
	-installer-keystore-password "${keyStorePassword}" \
	-installer-keystore-self-signed \
	-installer-api-secure "${apiSecure}" \
	-installer-api-domain "${apiDomain}" \
	-installer-authentication-token "${authenticationToken}"

echo "Starting service..."

sudo systemctl start gamedash-daemon

echo "Service started"

echo "Cleaning up..."

echo "${green}GameDash Daemon has been installed successfully${reset}"
