#!/bin/bash
set -e
if [ $EUID -eq 0 ]; then
echo "Error: This script should NOT be executed as root. Please run as the designated service user."
exit 1
fi
SERVICE_NAME="atf"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
CURRENT_USER=$(whoami)
if [ ! -f "${SERVICE_FILE}" ]; then
echo "Error: Service file ${SERVICE_FILE} not found. Has the ATF service been installed?"
exit 1
fi
AUTHORIZED_USER=$(grep -Po '^User=\K.*' "${SERVICE_FILE}" | tr -d '[:space:]')
if [ -z "${AUTHORIZED_USER}" ]; then
echo "Error: Could not determine the authorized user from ${SERVICE_FILE}."
exit 1
fi
if [ "${CURRENT_USER}" != "${AUTHORIZED_USER}" ]; then
echo "Error: This script can only be run by the authorized user '${AUTHORIZED_USER}' (current user is '${CURRENT_USER}')"
exit 1
fi
echo "Reloading systemd configuration..."
sudo systemctl daemon-reload
echo "Starting ATF service (user: ${CURRENT_USER})..."
sudo systemctl restart "${SERVICE_NAME}"
sleep 2
if systemctl is-active --quiet "${SERVICE_NAME}"; then
echo -e "\n ATF service started successfully!"
echo "Service status: $(systemctl status "${SERVICE_NAME}" --no-pager | grep 'Active:' | awk '{print $2, $3}')"
INSTALLED_PATH=$(which atf || echo "not found")
echo "Running binary path: ${INSTALLED_PATH}"
if [ -f "${INSTALLED_PATH}" ]; then
echo "Binary modification time: $(stat -c %y "${INSTALLED_PATH}" | cut -d'.' -f1)"
fi
else
echo -e "\n ATF service failed to start!"
echo "Please check logs: journalctl -u ${SERVICE_NAME} -e"
exit 1
fi