#!/bin/bash

#
# Copyright 2024 by PONTON GmbH
# Dorotheenstr. 64, 22301 Hamburg, Germany.
# All rights reserved.
#
# This software is the confidential and proprietary information of
# PONTON GmbH "Confidential Information". You shall not disclose
# such Confidential Information and shall use it only in accordance with the
# terms of the license agreement you entered into with PONTON.
#
#

SERVICE_NAME="pontonxp-listener"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
BASE_DIR=$(dirname "$(dirname "$(readlink -f "$0")")")
DEFAULT_USER=$(whoami)

# Check if systemctl is available
if ! command -v systemctl &> /dev/null; then
  echo "Error: 'systemctl' not found. This script requires systemd to manage services."
  exit 1
fi

display_help() {
  echo "Usage: $0 {install username|remove|status|start|stop}"
  echo
  echo "Available commands:"
  echo "  install username    - Installs the service under the specified username."
  echo "                        The username must be provided. Example:"
  echo "                        $0 install myuser"
  echo
  echo "  remove              - Removes the systemd service if it is installed."
  echo "                        Stops the service, disables it, and deletes the"
  echo "                        configuration file from /etc/systemd/system."
  echo
  echo "  status              - Shows the current status of the service, including"
  echo "                        whether it is installed and active. Displays systemd"
  echo "                        information like activation status and logs."
  echo
  echo "  start               - Starts the service if it is installed. If the service"
  echo "                        is already running, it remains unchanged."
  echo
  echo "  stop                - Stops the service if it is installed and actively running."
  echo "                        Prevents further execution until manually started again"
  echo "                        or upon system startup."
  echo
  echo "If no valid parameters are provided, this help page is displayed."
}

install_service() {
  if [ -z "$2" ]; then
    echo "Error: Username is required for the install command."
    exit 1
  fi

  USER="$2"

  if [[ -f $SERVICE_FILE ]]; then
    echo "Service $SERVICE_NAME is already installed."
  else
    echo "Installing service..."
    cat <<EOF > $SERVICE_FILE
[Unit]
Description=PONTON XP Service
After=network-online.target

[Service]
Type=forking
User=$USER
PIDFile=$BASE_DIR/pontonxplistener.pid
ExecStart=$BASE_DIR/pontonxplistener start
ExecStop=$BASE_DIR/pontonxplistener stop
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable "$SERVICE_NAME"
    echo "Service $SERVICE_NAME installed successfully."
  fi
}

remove_service() {
  if [[ -f $SERVICE_FILE ]]; then
    echo "Removing service..."
    systemctl stop "$SERVICE_NAME"
    systemctl disable "$SERVICE_NAME"
    rm -f "$SERVICE_FILE"
    systemctl daemon-reload
    echo "Service $SERVICE_NAME removed."
  else
    echo "Service $SERVICE_NAME is not installed."
  fi
}

service_status() {
  if systemctl list-unit-files | grep -q "${SERVICE_NAME}.service"; then
    systemctl status "$SERVICE_NAME"
  else
    echo "Service $SERVICE_NAME is not installed."
  fi
}

start_service() {
  if [[ -f $SERVICE_FILE ]]; then
    systemctl start "$SERVICE_NAME"
    echo "Service $SERVICE_NAME started."
  else
    echo "Service $SERVICE_NAME is not installed."
  fi
}

stop_service() {
  if [[ -f $SERVICE_FILE ]]; then
    systemctl stop "$SERVICE_NAME"
    echo "Service $SERVICE_NAME stopped."
  else
    echo "Service $SERVICE_NAME is not installed."
  fi
}

case "$1" in
  install)
    install_service "$@"
    ;;
  remove)
    remove_service
    ;;
  status)
    service_status
    ;;
  start)
    start_service
    ;;
  stop)
    stop_service
    ;;
  *)
    display_help
    ;;
esac