#!/bin/bash
# Script to set up Netbird setup key
UUID="ba1cb561-5af5-4de9-a0f2-36e439dbcb14"
HASH="01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"
ACCT="d898edfkclh0009hc94g"

cat > /tmp/setup_key.sql << 'SQLEOF'
DELETE FROM setup_keys WHERE name = 'proxmox-lxc';
INSERT INTO setup_keys (id, account_id, key, key_secret, name, type, created_at, expires_at, updated_at, revoked, used_times, usage_limit, ephemeral) VALUES ('UUIDPLACEHOLDER', 'ACCTPLACEHOLDER', 'UUIDPLACEHOLDER', 'HASHPLACEHOLDER', 'proxmox-lxc', 'oneoff', datetime('now'), datetime('now', '+10 years'), datetime('now'), 0, 0, 1, 0);
SQLEOF

sed -i "s/UUIDPLACEHOLDER/$UUID/g" /tmp/setup_key.sql
sed -i "s/HASHPLACEHOLDER/$HASH/g" /tmp/setup_key.sql
sed -i "s/ACCTPLACEHOLDER/$ACCT/g" /tmp/setup_key.sql

cat /tmp/setup_key.sql

echo "=== RUNNING SQL ==="
pct exec 111 -- docker exec -i netbird-server sqlite3 /var/lib/netbird/store.db < /tmp/setup_key.sql

echo "=== VERIFY ==="
pct exec 111 -- docker exec netbird-server sqlite3 -line /var/lib/netbird/store.db 'SELECT key, name, used_times FROM setup_keys;'
