#!/bin/bash

# Script to copy specialist overview SVGs to nginx static directory

# Function to show usage
show_usage() {
    echo "Usage: $0 <config_dir> <static_dir>"
    echo "  config_dir: Path to the config directory containing specialists"
    echo "  static_dir: Path to the nginx static directory"
    exit 1
}

# Check arguments
if [ $# -ne 2 ]; then
    show_usage
fi

CONFIG_DIR="$1"
STATIC_DIR="$2"
SPECIALISTS_DIR="${CONFIG_DIR}/specialists"
OUTPUT_DIR="${STATIC_DIR}/specialists"

# Check if source directory exists
if [ ! -d "$SPECIALISTS_DIR" ]; then
    echo "Error: Specialists directory not found at $SPECIALISTS_DIR"
    exit 1
fi

# Create output directory
mkdir -p "$OUTPUT_DIR"

# Counter for processed files
processed=0

# Process each specialist type directory
for TYPE_DIR in "$SPECIALISTS_DIR"/*/ ; do
    if [ -d "$TYPE_DIR" ]; then
        # Get specialist type from directory name
        SPECIALIST_TYPE=$(basename "$TYPE_DIR")

        # Find and process overview SVG files
        for SVG_FILE in "$TYPE_DIR"*_overview.svg; do
            if [ -f "$SVG_FILE" ]; then
                # Extract version (remove _overview.svg from filename)
                VERSION=$(basename "$SVG_FILE" "_overview.svg")

                # Create new filename
                NEW_FILENAME="${SPECIALIST_TYPE}_${VERSION}_overview.svg"

                # Copy file
                cp -f "$SVG_FILE" "${OUTPUT_DIR}/${NEW_FILENAME}"

                echo "Copied $(basename "$SVG_FILE") -> $NEW_FILENAME"
                ((processed++))
            fi
        done
    fi
done

echo -e "\nProcessed $processed overview SVG files"