#!/usr/bin/env bash
set -euo pipefail

mkdir -p /root/docker/jarvis-api
cd /root/docker/jarvis-api
tar xzf jarvis-api-deploy.tgz

random_text() {
  tr -dc 'A-Za-z0-9' </dev/urandom | head -c "${1:-32}"
}

if [ ! -f .env ]; then
  {
    printf 'JARVIS_API_TOKEN=%s\n' "$(random_text 64)"
    printf 'JARVIS_ADMIN_PASSWORD=%s\n' "$(random_text 28)"
    printf 'JARVIS_SA_PASSWORD=Jarvis%s!A1\n' "$(random_text 16)"
  } > .env
  chmod 600 .env
fi

docker compose -f docker-compose.server.yml --env-file .env up -d --build
docker compose -f docker-compose.server.yml --env-file .env ps
