#!/bin/bash

DEFAULT_VERSION="1.21.8"
VERSION="$DEFAULT_VERSION"

show_help() {
    echo "Usage: $0 [-v VERSION | --version VERSION]"
    echo ""
    echo "Options:"
    echo "  -v, --version VERSION  Set version for repository base (default: $DEFAULT_VERSION)"
    echo "  -h, --help             Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 --version 1.21.8"
    echo "  $0 -v 1.16.5"
    echo "  $0                     # Uses default version $DEFAULT_VERSION"
}

validate_version() {
    local version="$1"
    if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+([.-][a-zA-Z0-9]+)*$ ]]; then
        echo "Error: Invalid version format: $version"
        echo "Version should be in format: X.Y.Z or X.Y.Z-SNAPSHOT"
        return 1
    fi
    return 0
}

while [[ $# -gt 0 ]]; do
    case $1 in
        -v|--version)
            VERSION="$2"
            if ! validate_version "$VERSION"; then
                exit 1
            fi
            shift 2
            ;;
        -h|--help)
            show_help
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            show_help
            exit 1
            ;;
    esac
done

REPO_BASE_DIR="/var/www/lib.mstn.su"
REPO_BASE="$REPO_BASE_DIR/$VERSION"

echo "Using repository base: $REPO_BASE"
echo "Processing version: $VERSION"

if [ ! -d "$REPO_BASE" ]; then
    echo "Error: Directory $REPO_BASE does not exist!"
    echo "Available versions in $REPO_BASE_DIR:"
    ls -la "$REPO_BASE_DIR" 2>/dev/null | grep '^d' || echo "No directories found"
    exit 1
fi

find "$REPO_BASE" -name "*.jar" | while read jar_file; do
    dir=$(dirname "$jar_file")
    filename=$(basename "$jar_file" .jar)
    
    if [[ $filename =~ (.+)-([0-9].*)$ ]]; then
        artifact="${BASH_REMATCH[1]}"
        version="${BASH_REMATCH[2]}"
        
        relative_path="${jar_file#$REPO_BASE/}"
        group_id=$(dirname "$(dirname "$relative_path")" | tr '/' '.')
        
        cat > "$dir/$artifact-$version.pom" << EOPOM
<?xml version="1.0" encoding="UTF-8"?>
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>$group_id</groupId>
  <artifactId>$artifact</artifactId>
  <version>$version</version>
  <packaging>jar</packaging>
</project>
EOPOM
        
        echo "Generated POM for: $group_id:$artifact:$version"
    fi
done

find "$REPO_BASE" -type d -mindepth 3 | while read artifact_dir; do
    versions=$(ls "$artifact_dir" | grep -E '^[0-9]' | sort -V)
    if [ -n "$versions" ]; then
        latest_version=$(echo "$versions" | tail -1)
        
        relative_path="${artifact_dir#$REPO_BASE/}"
        group_id=$(dirname "$relative_path" | tr '/' '.')
        artifact_id=$(basename "$artifact_dir")
        
        cat > "$artifact_dir/maven-metadata.xml" << EOMETA
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
  <groupId>$group_id</groupId>
  <artifactId>$artifact_id</artifactId>
  <versioning>
    <latest>$latest_version</latest>
    <release>$latest_version</release>
    <versions>
$(for ver in $versions; do echo "      <version>$ver</version>"; done)
    </versions>
    <lastUpdated>$(date +%Y%m%d%H%M%S)</lastUpdated>
  </versioning>
</metadata>
EOMETA
        
        echo "Generated metadata for: $group_id:$artifact_id"
    fi
done

echo "Completed processing version: $VERSION"