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

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$repo_root"

if [[ -f .env ]]; then
  set -a
  # shellcheck source=/dev/null
  source .env
  set +a
else
  echo "Missing .env. Copy .env.example to .env and edit it first." >&2
  exit 1
fi

if [[ "$(id -u)" != "0" ]]; then
  echo "Run as root: sudo bash scripts/install-systemd.sh" >&2
  exit 1
fi

VEX_POOL_ROOT="${VEX_POOL_ROOT:-/opt/vexon-pool}"
VEX_BIN_DIR="${VEX_BIN_DIR:-$VEX_POOL_ROOT/bin}"
VEX_WEB_DIR="${VEX_WEB_DIR:-$VEX_POOL_ROOT/web}"
VEX_STRATUM_DIR="${VEX_STRATUM_DIR:-$VEX_POOL_ROOT/stratum}"
VEX_STRATUM_CONFIG_DIR="${VEX_STRATUM_CONFIG_DIR:-$VEX_POOL_ROOT/stratum-config}"
VEX_NODE_APPDATA="${VEX_NODE_APPDATA:-/root/.vexond}"
VEX_ENABLE_LOCAL_PEER="${VEX_ENABLE_LOCAL_PEER:-1}"
VEX_PEER_APPDATA="${VEX_PEER_APPDATA:-/root/.vexond-peer}"
VEX_START_WALLET="${VEX_START_WALLET:-0}"
VEX_WALLET_APPDATA="${VEX_WALLET_APPDATA:-/root/.vexonwallet}"
VEX_RPC_PORT="${VEX_RPC_PORT:-18556}"

enable_now=0
if [[ "${1:-}" == "--enable-now" ]]; then
  enable_now=1
fi

install_unit() {
  local name="$1"
  local content="$2"
  printf '%s\n' "$content" > "/etc/systemd/system/${name}"
  chmod 0644 "/etc/systemd/system/${name}"
  echo "Installed /etc/systemd/system/${name}"
}

install_unit vexon-node.service "[Unit]
Description=Vexon Core node
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=${VEX_BIN_DIR}/vexond --appdata=${VEX_NODE_APPDATA} --configfile=${VEX_NODE_APPDATA}/vexond.conf
Restart=always
RestartSec=5
LimitNOFILE=65535
SyslogIdentifier=vexon-node

[Install]
WantedBy=multi-user.target"

if [[ "$VEX_ENABLE_LOCAL_PEER" == "1" ]]; then
  install_unit vexon-peer.service "[Unit]
Description=Vexon local peer node
After=network-online.target vexon-node.service
Wants=network-online.target

[Service]
Type=simple
ExecStart=${VEX_BIN_DIR}/vexond --appdata=${VEX_PEER_APPDATA} --configfile=${VEX_PEER_APPDATA}/vexond.conf
Restart=always
RestartSec=5
LimitNOFILE=65535
SyslogIdentifier=vexon-peer

[Install]
WantedBy=multi-user.target"
fi

install_unit vexon-stratum.service "[Unit]
Description=Vexon Stratum server
After=network-online.target mariadb.service vexon-node.service
Wants=network-online.target

[Service]
Type=simple
WorkingDirectory=${VEX_STRATUM_CONFIG_DIR}
ExecStart=${VEX_STRATUM_CONFIG_DIR}/VexonStratum ${VEX_STRATUM_CONFIG_DIR}/vexon
Restart=always
RestartSec=3
LimitNOFILE=65535
SyslogIdentifier=vexon-stratum

[Install]
WantedBy=multi-user.target"

install_unit vexon-pool-main.service "[Unit]
Description=Vexon Pool main backend loop
After=mariadb.service memcached.service

[Service]
Type=simple
WorkingDirectory=${VEX_WEB_DIR}
ExecStart=${VEX_WEB_DIR}/main.sh
Restart=always
RestartSec=5
SyslogIdentifier=vexon-pool-main

[Install]
WantedBy=multi-user.target"

install_unit vexon-pool-loop2.service "[Unit]
Description=Vexon Pool stats backend loop
After=mariadb.service memcached.service

[Service]
Type=simple
WorkingDirectory=${VEX_WEB_DIR}
ExecStart=${VEX_WEB_DIR}/loop2.sh
Restart=always
RestartSec=5
SyslogIdentifier=vexon-pool-loop2

[Install]
WantedBy=multi-user.target"

install_unit vexon-pool-blocks.service "[Unit]
Description=Vexon Pool block backend loop
After=mariadb.service memcached.service vexon-node.service

[Service]
Type=simple
WorkingDirectory=${VEX_WEB_DIR}
ExecStart=${VEX_WEB_DIR}/blocks.sh
Restart=always
RestartSec=5
SyslogIdentifier=vexon-pool-blocks

[Install]
WantedBy=multi-user.target"

if [[ "$VEX_START_WALLET" == "1" ]]; then
  install_unit vexon-wallet.service "[Unit]
Description=Vexon wallet RPC
After=network-online.target vexon-node.service
Wants=network-online.target

[Service]
Type=simple
WorkingDirectory=${repo_root}
ExecStart=/usr/bin/env bash ${repo_root}/scripts/run-wallet-rpc-systemd.sh
Restart=always
RestartSec=5
SyslogIdentifier=vexon-wallet

[Install]
WantedBy=multi-user.target"
else
  echo "Skipped vexon-wallet.service because VEX_START_WALLET=$VEX_START_WALLET"
fi

systemctl daemon-reload
systemctl enable vexon-node.service vexon-stratum.service vexon-pool-main.service vexon-pool-loop2.service vexon-pool-blocks.service
if [[ "$VEX_ENABLE_LOCAL_PEER" == "1" ]]; then
  systemctl enable vexon-peer.service
fi
if [[ "$VEX_START_WALLET" == "1" ]]; then
  systemctl enable vexon-wallet.service
fi

if (( enable_now == 1 )); then
  scripts/stop-pool.sh || true
  systemctl restart vexon-node.service
  if [[ "$VEX_ENABLE_LOCAL_PEER" == "1" ]]; then
    systemctl restart vexon-peer.service
  fi
  if [[ "$VEX_START_WALLET" == "1" ]]; then
    systemctl restart vexon-wallet.service
  fi
  systemctl restart vexon-stratum.service vexon-pool-main.service vexon-pool-loop2.service vexon-pool-blocks.service
fi

echo "Systemd units installed."
echo "Use this command when ready to switch from screen to systemd:"
echo "  sudo bash scripts/install-systemd.sh --enable-now"
