# -*- mode: sh; sh-shell: bash; sh-basic-offset: 2; indent-tabs-mode: nil; -*-

post_install() {

ETC_DIR="/etc/damage"
VAR_DIR="/var/lib/damage"
LOG_DIR="/var/log/damage"
SERVICE_NAME="damage"
INSTALL_DIR="/opt/damage"
SERVICE_FILE="/etc/systemd/system/damage.service"
  umask 022
  cat > "$SERVICE_FILE" <<EOF
[Unit]
Description=An OTP application to run bdd tests.
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=damage
Group=damage
Environment=SHELL=sh
WorkingDirectory=${INSTALL_DIR}
ExecStart=${INSTALL_DIR}/bin/damage foreground
Restart=on-failure
RestartSec=5s
LimitNOFILE=65536
StateDirectory=damage
LogsDirectory=damage


[Install]
WantedBy=multi-user.target
EOF
  chmod 0644 "$SERVICE_FILE"
  chown -R "damage:damage" "$VAR_DIR" "$LOG_DIR" || true
  # Realize sysusers/tmpfiles we ship with the package
  command -v systemd-sysusers >/dev/null 2>&1 && systemd-sysusers damage.conf
  command -v systemd-tmpfiles >/dev/null 2>&1 && systemd-tmpfiles --create damage.conf

  # Reload units in case we ship/change a systemd service
  command -v systemctl >/dev/null 2>&1 && systemctl daemon-reload

KEY_PATH="${VAR_DIR}/ssh_daemon/ssh_host_rsa_key"

# Create directory if it doesn't exist
mkdir -p "$(dirname "$KEY_PATH")"
chmod 700 "$(dirname "$KEY_PATH")"

# Generate key only if missing
if [ ! -f "$KEY_PATH" ]; then
    echo "[INFO] Generating new SSH host key: $KEY_PATH"
    ssh-keygen -t rsa -b 4096 -f "$KEY_PATH" -N ""
    chmod 600 "$KEY_PATH"
    chmod 644 "${KEY_PATH}.pub"
else
    echo "[INFO] SSH host key already exists at $KEY_PATH — skipping generation."
fi

  echo ">>> damage installed."
  echo ">>> If you use systemd: sudo systemctl enable --now damage.service"
}

post_upgrade() {
  post_install
}

pre_remove() {
  # Don’t fail if the service isn't present/running
  if command -v systemctl >/dev/null 2>&1; then
    systemctl disable --now damage.service >/dev/null 2>&1 || true
  fi
}

post_remove() {
  : # keep logs/data; tmpfiles/sysusers handle ownership on next install
}
