#!/bin/sh

# This file is a template which gets run through the "/kickstart.sh" route to
# generate the final script file. PLEASE NEVER RUN THIS FILE DIRECTLY, instead
# run me with:
#
# sh <(curl -Ss "http://<SERVER-ADDRESS>/kickstart.sh?tenant_id=<TENANT-ID>")
#
# Where:
# <SERVER-ADDRESS> is the ShellHub server address
# <TENANT-ID> is your tenant ID
#
# List of URL parameters of /kickstart.sh URL:
#
# keepalive_interval = Specifies in seconds the keep alive message interval
# preferred_hostname = The preferred hostname to use rather than generated
#                      value from ethernet MAC address


while true; do
    read -p "Do you want to use docker or native agent  (D/N)" dn
    case $dn in
        [Nn]* )
        #Native install Methode
        mkdir /opt/ShellHub
        wget {{scheme}}://{{host}}/agent-amd64 -O /opt/ShellHub/agent
        chmod +x /opt/ShellHub/agent
        echo "systemctl stop ShellHubAgent; mv /opt/ShellHub/agent /opt/ShellHub/agent.bck;  wget {{scheme}}://{{host}}/agent-amd64 -O /opt/ShellHub/agent; systemctl stop ShellHubAgent" > /opt/ShellHub/UpdateAgent.sh
            while true; do
            read -p "Do you want to use portable or Service install  (P/S)" dn
            case $dn in
                [Pp]* )
                    echo "export SHELLHUB_PRIVATE_KEY=/opt/ShellHub/shellhubAgent.key" >>/opt/ShellHub/RunAgent.sh
                    echo "export SHELLHUB_TENANT_ID={{tenant_id}}" >>/opt/ShellHub/RunAgent.sh
                    echo "export SHELLHUB_SERVER_ADDRESS={{scheme}}://{{host}}" >>/opt/ShellHub/RunAgent.sh
                    {% if keepalive_interval ~= '' and keepalive_interval ~= nil then %}
                        echo "export SHELLHUB_KEEPALIVE_INTERVAL={{keepalive_interval}}" >>/opt/ShellHub/RunAgent.sh
                    {% end %}
                    {% if preferred_hostname ~= '' and preferred_hostname ~= nil then %}
                        echo "export SHELLHUB_PREFERRED_HOSTNAME={{preferred_hostname}}" >>/opt/ShellHub/RunAgent.sh
                    {% end %}
                    echo "/opt/ShellHub/agent" >>/opt/ShellHub/RunAgent.sh
                    echo "To run the Agent use 'bash /opt/ShellHub/RunAgent.sh' as root"
                exit;;
                [Ss]* )
                break;;

                * ) echo "Please answer D(ocker) or N(ative).";;
            esac
        done
        echo "[Unit]" >>/etc/systemd/system/ShellHubAgent.service
        echo "Description=ShellHub Api" >>/etc/systemd/system/ShellHubAgent.service
        echo "Wants=network-online.target" >>/etc/systemd/system/ShellHubAgent.service
        echo "[Service]" >>/etc/systemd/system/ShellHubAgent.service
        echo "Type=simple" >>/etc/systemd/system/ShellHubAgent.service
        echo "Environment=SHELLHUB_PRIVATE_KEY=/opt/ShellHub/shellhubAgent.key" >>/etc/systemd/system/ShellHubAgent.service
        echo "Environment=SHELLHUB_TENANT_ID={{tenant_id}}" >>/etc/systemd/system/ShellHubAgent.service
        echo "Environment=SHELLHUB_SERVER_ADDRESS={{scheme}}://{{host}}" >>/etc/systemd/system/ShellHubAgent.service
       {% if keepalive_interval ~= '' and keepalive_interval ~= nil then %}
        echo "Environment=SHELLHUB_KEEPALIVE_INTERVAL={{keepalive_interval}}" >>/etc/systemd/system/ShellHubAgent.service
       {% end %}
       {% if preferred_hostname ~= '' and preferred_hostname ~= nil then %}
        echo "Environment=SHELLHUB_PREFERRED_HOSTNAME={{preferred_hostname}}" >>/etc/systemd/system/ShellHubAgent.service
       {% end %}

        echo "ExecStart=/opt/ShellHub/agent" >>/etc/systemd/system/ShellHubAgent.service
        echo "[Install]" >>/etc/systemd/system/ShellHubAgent.service
        echo "WantedBy=multi-user.target" >>/etc/systemd/system/ShellHubAgent.service

        if [ "$(grep -oP '(?<=^PLATFORM_ID=).+' /etc/os-release | tr -d '"')" = "platform:el8" ]; then
            ##Resetting reseting service permissions
            restorecon -Rv /etc/systemd/system
        fi
        systemctl enable --now ShellHubAgent
        exit;;
        [Dd]* )
        break;;

        * ) echo "Please answer D(ocker) or N(ative).";;
    esac
done


#Docker install Methode
type docker > /dev/null 2>&1 || { echo "Docker is not instaled"; exit 1; }

if ! docker info > /dev/null 2>&1; then
    cat <<EOF
Docker is not running or your current user is not in docker group.
You need to manually add your current user to docker group or run this installer using sudo.
EOF
    while true; do
	echo -n "Do you want to run the installer using sudo? [y/N] "
	read yn
	case $yn in
            [Yy]|YES|yes) SUDO="sudo"; break;;
            [Nn]|NO|no|"") echo "It cannot proceed, exiting"; exit;;
            *) echo "Please answer 'yes' or 'no'.";;
	esac
    done
fi

$SUDO docker run -d \
       --name=shellhub \
       --restart=on-failure \
       --privileged \
       --net=host \
       --pid=host \
       -v /:/host \
       -v /dev:/dev \
       -v /var/run/docker.sock:/var/run/docker.sock \
       -v /etc/passwd:/etc/passwd \
       -v /etc/group:/etc/group \
       -v /var/run:/var/run \
       -v /var/log:/var/log \
       -e SHELLHUB_SERVER_ADDRESS={{scheme}}://{{host}} \
       -e SHELLHUB_PRIVATE_KEY=/host/etc/shellhub.key \
       -e SHELLHUB_TENANT_ID={{tenant_id}} \
       {% if keepalive_interval ~= '' and keepalive_interval ~= nil then %}
       -e SHELLHUB_KEEPALIVE_INTERVAL={{keepalive_interval}} \
       {% end %}
       {% if preferred_hostname ~= '' and preferred_hostname ~= nil then %}
       -e SHELLHUB_PREFERRED_HOSTNAME={{preferred_hostname}} \
       {% end %}
       shellhubio/agent:{{version}}