#!/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="gpt4all-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
# -------------------------------------------------------------------------

# Docker Compose for GPT4All
version: '3.8'

services:
  gpt4all:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: gpt4all-server
    ports:
      - "4891:4891"
    volumes:
      - gpt4all_models:/app/models
    networks:
      - gpt4all_net
    healthcheck:
      test: [ "CMD", "curl", "-f", "http://localhost:4891/v1/models" ] # GPT4All local API endpoint
      interval: 30s
      timeout: 10s
      retries: 3
    restart: unless-stopped

networks:
  gpt4all_net:
    driver: bridge

volumes:
  gpt4all_models:
    name: gpt4all_models

INNER_EOF

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

echo ""
echo "✅ Deployment Complete!"
echo "👉 Your gpt4all stack is running."
