#!/usr/bin/env bash
# Corré esto en una máquina nueva. Clona el repo STP y lanza stp.sh.
#
# Uso:
#   bash <(curl -fsSL https://gitea.mateosaldain.uy/mateo/stp/raw/branch/main/bootstrap.sh)
#
# O descargá y ejecutá manualmente:
#   curl -fsSLo bootstrap.sh https://gitea.mateosaldain.uy/mateo/stp/raw/branch/main/bootstrap.sh
#   bash bootstrap.sh

set -euo pipefail

readonly giteaHost="gitea.mateosaldain.uy"
readonly giteaUser="mateo"
readonly repoName="stp"
readonly cloneUrl="https://${giteaHost}/${giteaUser}/${repoName}.git"
cloneDir="${stpDir:-$HOME/.stp}"

echo "=== Sistema de Transferencia Personal ==="
echo ""

if ! command -v git &>/dev/null || ! command -v curl &>/dev/null; then
    echo "-> Instalando git y curl..."
    sudo apt-get update -qq
    sudo apt-get install -y git curl
fi

if [[ -d "$cloneDir/.git" ]]; then
    echo "-> Actualizando repositorio STP en $cloneDir..."
    git -C "$cloneDir" pull --ff-only
else
    echo "-> Clonando repositorio STP en $cloneDir..."
    git clone "$cloneUrl" "$cloneDir"
fi

chmod +x "$cloneDir/stp.sh"

echo ""
echo "-> Lanzando STP..."
exec bash "$cloneDir/stp.sh" "$@"
