#!/bin/bash
# Fix Web3Signer to look in the correct directory for keys
# Usage: ./scripts/fix-web3signer-path.sh

set -e

PROXMOX_HOST="192.168.11.11"
WEB3SIGNER_CONTAINER="107"
WEB3SIGNER_IP="192.168.11.111"

echo "═══════════════════════════════════════════════════════════════"
echo "🔧 FIXING WEB3SIGNER KEY PATH"
echo "═══════════════════════════════════════════════════════════════"
echo ""

# Step 1: Check current systemd service
echo "Step 1: Checking current systemd service configuration..."
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- cat /etc/systemd/system/web3signer.service" 2>&1 | grep -A 5 "ExecStart" || echo "Service file not found"

# Step 2: Check where keys are located
echo ""
echo "Step 2: Checking key locations..."
echo "Keys in /opt/web3signer/data/keys/:"
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- ls -la /opt/web3signer/data/keys/ 2>&1" || echo "Directory doesn't exist or empty"

echo ""
echo "Keys in /opt/web3signer-23.10.0:"
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- ls -la /opt/web3signer-23.10.0/ 2>&1 | head -10" || echo "Directory doesn't exist"

# Step 3: Check Web3Signer version and installation
echo ""
echo "Step 3: Checking Web3Signer installation..."
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- ls -la /opt/ | grep web3signer" 2>&1

# Step 4: Fix the systemd service to use correct data path
echo ""
echo "Step 4: Updating systemd service file..."
# Use the existing binary path (/opt/web3signer-23.10.0/bin/web3signer) but ensure data-path is correct
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- bash -c 'cat > /etc/systemd/system/web3signer.service <<\"ENDOFFILE\"
[Unit]
Description=Web3Signer - Ethereum Signing Service
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/opt/web3signer-23.10.0
ExecStart=/opt/web3signer-23.10.0/bin/web3signer --http-listen-port=9000 --http-listen-host=192.168.11.111 --http-host-allowlist=* --data-path=/opt/web3signer/data eth1 --chain-id=138
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=web3signer

[Install]
WantedBy=multi-user.target
ENDOFFILE
'" 2>&1

# Verify the file was created
echo "  Verifying service file..."
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- cat /etc/systemd/system/web3signer.service | grep -A 2 ExecStart" 2>&1

# Step 5: Reload systemd and restart
echo ""
echo "Step 5: Reloading systemd and restarting Web3Signer..."
ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- systemctl daemon-reload && systemctl restart web3signer && sleep 5" 2>&1

# Step 6: Verify keys are loaded
echo ""
echo "Step 6: Verifying keys are loaded..."
sleep 3
KEYS=$(curl -s http://$WEB3SIGNER_IP:9000/api/v1/eth1/publicKeys 2>&1)
if [ "$KEYS" != "[]" ] && [ -n "$KEYS" ]; then
    KEY_COUNT=$(echo "$KEYS" | jq '. | length' 2>/dev/null || echo "0")
    if [ "$KEY_COUNT" -gt 0 ]; then
        echo "✅ SUCCESS: $KEY_COUNT key(s) loaded!"
        echo "$KEYS" | jq '.'
    else
        echo "⚠️  Keys endpoint returned empty array"
    fi
else
    echo "⚠️  No keys loaded"
    echo "Checking logs..."
    ssh root@$PROXMOX_HOST "pct exec $WEB3SIGNER_CONTAINER -- journalctl -u web3signer.service -n 30 --no-pager" 2>&1 | tail -20
fi

echo ""
echo "═══════════════════════════════════════════════════════════════"
echo "✅ PATH FIX COMPLETE"
echo "═══════════════════════════════════════════════════════════════"
