# Complete All Explorer Restoration Tasks

**This guide completes ALL remaining restoration tasks automatically.**

---

## Step 1: Run Complete Restoration Script (Inside Container)

**You are currently in the container (root@blockscout-1). Run this script:**

```bash
bash <(cat << 'SCRIPT'
#!/bin/bash
# Complete Explorer Restoration - All Tasks

echo "=== Complete Blockscout Restoration ==="
echo ""

# Check status
echo "1. Checking installation..."
systemctl list-unit-files | grep blockscout || echo "No systemd service"
test -f /opt/blockscout/docker-compose.yml && echo "✓ docker-compose.yml exists" || echo "✗ docker-compose.yml NOT found"
docker ps -a | head -5

# Start Blockscout
echo ""
echo "2. Starting Blockscout..."
systemctl start blockscout 2>&1 || true
sleep 5
if ! systemctl is-active --quiet blockscout 2>/dev/null; then
    if [ -f /opt/blockscout/docker-compose.yml ]; then
        echo "Starting via docker-compose..."
        cd /opt/blockscout && docker-compose up -d 2>&1 || docker compose up -d 2>&1
        sleep 15
    fi
fi
docker ps -a --filter "status=exited" -q | xargs -r docker start 2>&1 || true
sleep 10

# Wait
echo ""
echo "3. Waiting for startup (30 seconds)..."
sleep 30

# Verify
echo ""
echo "4. Verifying..."
echo "Port 4000:" && ss -tlnp | grep :4000 || echo "NOT listening"
echo "" && echo "API:" && curl -s http://127.0.0.1:4000/api/v2/status | head -10 || echo "NOT responding"
echo "" && echo "Containers:" && docker ps | grep -E "blockscout|postgres" || echo "None running"

# Restart Nginx
echo ""
echo "5. Restarting Nginx..."
systemctl restart nginx 2>&1 || true
sleep 3
nginx -t 2>&1 | grep -E "syntax is ok|test is successful" && echo "✓ Nginx config valid" || echo "✗ Nginx config issues"

# Check Cloudflared
echo ""
echo "6. Checking Cloudflared..."
systemctl is-active cloudflared 2>/dev/null && echo "✓ Cloudflared running" || (systemctl start cloudflared 2>&1 || echo "✗ Cloudflared not available")

# Final test
echo ""
echo "7. Final API Test..."
curl -s http://127.0.0.1:4000/api/v2/status | head -5 || echo "Not responding"
curl -s http://127.0.0.1/api/v2/stats | head -5 || echo "Proxy not working"

echo ""
echo "=== Complete ==="
SCRIPT
)
```

**OR copy the script from:**
```bash
cat /home/intlc/projects/proxmox/scripts/complete-all-restoration.sh
```

---

## Step 2: Exit Container and Verify from pve2

**After the script completes, exit the container:**

```bash
exit
```

**Then on pve2, run verification:**

```bash
# Quick test
curl http://192.168.11.140:4000/api/v2/status
curl http://192.168.11.140/api/v2/stats

# Or run full verification script
bash /home/intlc/projects/proxmox/scripts/verify-from-pve2.sh
```

---

## Step 3: Test Public URL

**From any machine:**

```bash
curl https://explorer.d-bis.org/api/v2/stats
```

**Expected:** JSON response with chain_id, not 404 or 502

---

## What Gets Completed

✅ **Task 1**: Check current status  
✅ **Task 2**: Start Blockscout service  
✅ **Task 3**: Wait for initialization  
✅ **Task 4**: Verify Blockscout is running  
✅ **Task 5**: Verify and restart Nginx  
✅ **Task 6**: Check Cloudflare tunnel  
✅ **Task 7**: Final status report  

---

## Troubleshooting

### If Blockscout doesn't start:

```bash
# Check logs inside container
journalctl -u blockscout -n 50
docker-compose -f /opt/blockscout/docker-compose.yml logs --tail=50
```

### If Nginx returns 502:

```bash
# Wait longer (Blockscout can take 1-2 minutes)
sleep 60
curl http://192.168.11.140/api/v2/stats
```

### If public URL returns 404:

```bash
# Check Cloudflare tunnel
systemctl status cloudflared
cat /etc/cloudflared/config.yml
```

---

## Success Criteria

✅ Port 4000 is listening  
✅ Blockscout API responds with JSON  
✅ Nginx proxy works (not 502)  
✅ Public URL accessible (if Cloudflare configured)  

---

**All scripts are ready. Run Step 1 inside the container to complete everything!**

