#!/usr/bin/bash
set -euo pipefail
if [ "$(id -u)" -ne 0 ]; then
    echo 'Run as root: sudo warpgate-initialize' >&2
    exit 1
fi
if [ -e /etc/warpgate.yaml ]; then
    echo '/etc/warpgate.yaml already exists; refusing to overwrite it.' >&2
    exit 1
fi
install -d -o warpgate -g warpgate -m 0700 /var/lib/warpgate
cd /var/lib/warpgate
# Wizard writes its configuration to /etc/warpgate.yaml by default.
# The setup must be interactive, and should select /var/lib/warpgate for data.
/usr/bin/warpgate --config /etc/warpgate.yaml setup
if [ ! -f /etc/warpgate.yaml ]; then
    echo 'Setup did not create /etc/warpgate.yaml.' >&2
    exit 1
fi
chown root:warpgate /etc/warpgate.yaml
chmod 0640 /etc/warpgate.yaml
chown -R warpgate:warpgate /var/lib/warpgate
printf '\nSetup complete. Start with: systemctl enable --now warpgate\n'
