#!/bin/bash
set -euo pipefail

MIGRATE_VERSION=v4.18.1
REPO_URL=https://git.losandesgames.com/vfs/party

apt-get update -q
apt-get install -y -q git golang-go build-essential wget

# Install golang-migrate (not in Debian repos)
if ! command -v migrate &>/dev/null; then
    echo "Installing migrate ${MIGRATE_VERSION}..."
    wget -qO /tmp/migrate.tar.gz \
        "https://github.com/golang-migrate/migrate/releases/download/${MIGRATE_VERSION}/migrate.linux-amd64.tar.gz"
    tar -xzf /tmp/migrate.tar.gz -C /usr/local/bin migrate
    chmod +x /usr/local/bin/migrate
    rm /tmp/migrate.tar.gz
fi

# Clone repo
if [ ! -d /srv/party ]; then
    git clone "$REPO_URL" /srv/party
fi

echo ""
echo "Setup complete. Next steps:"
echo "  1. make deploy"
echo "  2. ssh root@<server> and edit /etc/party/environment with real credentials"
echo "  3. make deploy/migrate"
echo "  4. systemctl restart party"
