#!/bin/bash
# 🚀 Auto-generated by The AltStack
# https://thealtstack.com

echo "🔵 Starting AltStack Deployment..."

# 1. Check/Install Docker
if ! command -v docker &> /dev/null; then
    echo "📦 Docker not found. Installing..."
    curl -fsSL https://get.docker.com -o get-docker.sh
    sh get-docker.sh
    rm get-docker.sh
    echo "✅ Docker installed."
else
    echo "✅ Docker is already installed."
fi

# 2. Setup Directory
APP_DIR="garage-deploy"
mkdir -p $APP_DIR
cd $APP_DIR
echo "📂 Created directory: $APP_DIR"

# 3. Create docker-compose.yml
echo "📄 Writing configuration..."
cat << 'INNER_EOF' > docker-compose.yml

# -------------------------------------------------------------------------
# 🚀 Created and distributed by The AltStack
# 🌍 https://thealtstack.com
# -------------------------------------------------------------------------

version: '3.8'

services:
  garage:
    image: dxflrs/garage:v1.0.1
    container_name: garage
    restart: unless-stopped
    ports:
      - "3900:3900"
      - "3901:3901"
      - "3902:3902"
      - "3903:3903"
    environment:
      - GARAGE_RPC_BIND_ADDR=[::]:3903
      - GARAGE_RPC_PUBLIC_ADDR=127.0.0.1:3903
      - GARAGE_RPC_SECRET=YOUR_SECURE_RPC_SECRET_HERE
      - GARAGE_S3_API_BIND_ADDR=[::]:3900
      - GARAGE_S3_WEB_BIND_ADDR=[::]:3901
      - GARAGE_ADMIN_API_BIND_ADDR=[::]:3902
      - GARAGE_DATA_DIR=/var/lib/garage/data
      - GARAGE_META_DIR=/var/lib/garage/meta
    volumes:
      - garage_data:/var/lib/garage/data
      - garage_meta:/var/lib/garage/meta

volumes:
  garage_data:
  garage_meta:

INNER_EOF

# 4. Start Services
echo "🚀 Starting services..."
docker compose up -d

echo ""
echo "✅ Deployment Complete!"
echo "👉 Your Garage stack is running."
echo "   S3 API:     http://localhost:3900"
echo "   Web API:    http://localhost:3901"
echo "   Admin API:  http://localhost:3902"
