#!/usr/bin/env bash
set -euo pipefail

APP_DIR="${APP_DIR:-/home/deploy/po_internal_app}"
SERVICE_NAME="${SERVICE_NAME:-po_internal_app}"
SERVICE_TARGET="/etc/systemd/system/${SERVICE_NAME}.service"

if [[ ! -d "$APP_DIR" ]]; then
  echo "App directory not found: $APP_DIR" >&2
  exit 1
fi

if [[ ! -f "$APP_DIR/deploy/po_internal_app.service" ]]; then
  echo "Service template not found in $APP_DIR/deploy/po_internal_app.service" >&2
  exit 1
fi

install -m 0644 "$APP_DIR/deploy/po_internal_app.service" "$SERVICE_TARGET"
systemctl daemon-reload
systemctl enable --now "$SERVICE_NAME"
systemctl restart "$SERVICE_NAME"
systemctl status "$SERVICE_NAME" --no-pager
