#!/bin/bash

# TSYS Cloudron Packaging - Repository Clone Script
# This script clones all upstream repositories for Cloudron packaging
#
# Usage: ./clone-repos.sh
# Author: TSYS Group Development Stack
# Version: 1.0.0

set -e  # Exit on any error

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to print colored output
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if we're in the right directory
if [ ! -f "GitUrlList.txt" ]; then
    print_error "GitUrlList.txt not found. Please run this script from the Cloudron directory."
    exit 1
fi

# Create Package-Workspace directory if it doesn't exist
if [ ! -d "Package-Workspace" ]; then
    print_status "Creating Package-Workspace directory..."
    mkdir -p Package-Workspace
fi

# Create functional directories
print_status "Creating functional directories..."
cd Package-Workspace

FUNCTIONAL_DIRS=(
    "API-Gateway"
    "Automation" 
    "Business-Apps"
    "Collaboration"
    "Communication"
    "Data-Management"
    "Development"
    "DevOps-Tools"
    "Infrastructure"
    "Legal"
    "Low-Code"
    "Monitoring"
    "Project-Management"
    "Scientific-Computing"
    "Security"
    "System-Administration"
    "Voice-Audio"
)

for dir in "${FUNCTIONAL_DIRS[@]}"; do
    mkdir -p "$dir"
done

cd ..

# Function to determine functional category for an app
get_functional_category() {
    local appname="$1"
    
    case "$appname" in
        "apisix"|"webhook") echo "API-Gateway" ;;
        "runme"|"rundeck"|"huginn"|"windmill") echo "Automation" ;;
        "hyperswitch") echo "Financial-Payments" ;;
        "nautilus_trader") echo "Financial-Trading" ;;
        "PayrollEngine"|"openboxes"|"pimcore"|"InvenTree"|"killbill"|"midday"|"elabftw"|"PLMore") echo "Business-Apps" ;;
        "grist-core"|"consuldemocracy") echo "Collaboration" ;;
        "fonoster"|"craig") echo "Communication" ;;
        "datahub"|"seatunnel") echo "Data-Management" ;;
        "AutoBOM"|"reviewboard"|"docker-drawio"|"puter"|"warp"|"policies") echo "Development" ;;
        "fx") echo "DevOps-Tools" ;;
        "database-gateway"|"netbox"|"rathole"|"easy-gate"|"chirpstack"|"sdrangel") echo "Infrastructure" ;;
        "docassemble") echo "Legal" ;;
        "openblocks"|"no-code-architects-toolkit"|"corteza") echo "Low-Code" ;;
        "goalert"|"healthchecks"|"fleet"|"langfuse"|"sentry"|"signoz") echo "Monitoring" ;;
        "Core") echo "Project-Management" ;;
        "boinc"|"jamovi") echo "Scientific-Computing" ;;
        "tirreno"|"gophish"|"SniperPhish"|"security-awareness-training"|"comply") echo "Security" ;;
        "mender"|"mendersoftware"|"slurm") echo "System-Administration" ;;
        "WireViz"|"wireviz-web") echo "Documentation-Tools" ;;
        *) echo "Development" ;;  # Default category
    esac
}

# Clone repositories
print_status "Starting repository cloning process..."
total_repos=0
successful_clones=0
failed_clones=0

while IFS= read -r url; do
    url=$(printf '%s' "$url" | tr -d '[:space:]')
    if [ -n "$url" ]; then
        total_repos=$((total_repos + 1))
        
        # Extract repository name from URL
        reponame=$(basename "$url" .git)
        
        # Determine functional category
        category=$(get_functional_category "$reponame")
        
        print_status "Cloning $reponame into $category/..."
        
        # Create app directory
        app_dir="Package-Workspace/$category/$reponame"
        mkdir -p "$app_dir"
        
        # Clone repository with timeout and shallow clone
        if timeout 300 git clone --depth 1 "$url" "$app_dir/repo" 2>/dev/null; then
            print_success "✓ Successfully cloned $reponame"
            successful_clones=$((successful_clones + 1))
        else
            print_error "✗ Failed to clone $reponame (timeout or error)"
            failed_clones=$((failed_clones + 1))
            # Remove empty directory on failure
            rmdir "$app_dir" 2>/dev/null || true
        fi
    fi
done < GitUrlList.txt

# Summary
print_status "Cloning completed!"
echo -e "${GREEN}Successfully cloned: $successful_clones/$total_repos repositories${NC}"
echo -e "${RED}Failed to clone: $failed_clones/$total_repos repositories${NC}"

if [ $failed_clones -gt 0 ]; then
    print_warning "Some repositories failed to clone. This could be due to:"
    echo "  - Network connectivity issues"
    echo "  - Private repositories requiring authentication"
    echo "  - Invalid or moved repositories"
    echo "  - Rate limiting from Git providers"
    echo ""
    print_status "You can retry failed clones by running this script again."
fi

print_success "Repository cloning process completed!"
print_status "You can now start packaging applications for Cloudron."
print_status "Navigate to: Package-Workspace/<category>/<app-name>/repo"