#!/usr/bin/env bash set -euo pipefail SCRIPT_DIR="$(cd -- "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT_DIR="$(cd -- "${SCRIPT_DIR}/.." && pwd)" SERVICE_NAME="${SERVICE_NAME:-jhm-service}" SERVICE_USER="${SERVICE_USER:-root}" INSTALL_DIR="${INSTALL_DIR:-$ROOT_DIR}" TEMPLATE_PATH="${SCRIPT_DIR}/jhm-service.service.tpl" TARGET_PATH="/etc/systemd/system/${SERVICE_NAME}.service" TEMP_PATH="$(mktemp)" if [[ ! -f "${ROOT_DIR}/jhm-service" ]]; then echo "Executable not found: ${ROOT_DIR}/jhm-service" >&2 exit 1 fi if [[ ! -f "${ROOT_DIR}/runtime/config.json" ]]; then echo "Config file not found: ${ROOT_DIR}/runtime/config.json" >&2 exit 1 fi chmod +x "${ROOT_DIR}/jhm-service" sed \ -e "s|__INSTALL_DIR__|${INSTALL_DIR}|g" \ -e "s|__SERVICE_NAME__|${SERVICE_NAME}|g" \ -e "s|__SERVICE_USER__|${SERVICE_USER}|g" \ "${TEMPLATE_PATH}" > "${TEMP_PATH}" sudo install -m 0644 "${TEMP_PATH}" "${TARGET_PATH}" rm -f "${TEMP_PATH}" sudo systemctl daemon-reload sudo systemctl enable "${SERVICE_NAME}" sudo systemctl restart "${SERVICE_NAME}" sudo systemctl status "${SERVICE_NAME}" --no-pager