#!/bin/bash
# Create systemd services for Web3Signer and Vault
# Usage: ./create-systemd-services.sh

set -e

echo "Creating systemd services for Web3Signer and Vault..."

# Web3Signer systemd service
ssh root@192.168.11.11 "pct exec 107 -- bash -c '
cat > /etc/systemd/system/web3signer.service <<EOF
[Unit]
Description=Web3Signer
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/opt/web3signer-23.10.0
ExecStart=/opt/web3signer-23.10.0/bin/web3signer --config-file=/opt/web3signer-23.10.0/web3signer.yml
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable web3signer
systemctl start web3signer
sleep 2
systemctl status web3signer --no-pager | head -10
'" 2>&1

echo ""

# Vault systemd service (dev mode)
ssh root@192.168.11.11 "pct exec 108 -- bash -c '
cat > /etc/systemd/system/vault.service <<EOF
[Unit]
Description=HashiCorp Vault (Development)
After=network.target

[Service]
Type=simple
User=root
Environment=\"VAULT_ADDR=http://192.168.11.112:8200\"
ExecStart=/usr/local/bin/vault server -dev -dev-listen-address=192.168.11.112:8200 -dev-root-token-id=root
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable vault
systemctl start vault
sleep 2
systemctl status vault --no-pager | head -10
'" 2>&1

echo ""
echo "✅ Systemd services created and started"
