#!/usr/bin/bash

set -euo pipefail

# Configuration - edit these defaults as needed
PACKAGE_NAME="repogoon"
DEFAULT_OUTPUT_ROOT="/home/burningpho3nix/Documents/Development/Setup-Tool/normal"

VERSION=""
OUTPUT_ROOT="$DEFAULT_OUTPUT_ROOT"

usage() {
    echo "Usage: $0 -v VERSION [-o OUTPUT_ROOT]"
    echo "Options:"
    echo "  -v VERSION       Version number to append to the release folder (required)"
    echo "  -o OUTPUT_ROOT   Directory where the release folder and tarball are created"
    echo "                   Default: $DEFAULT_OUTPUT_ROOT"
    echo "  -h               Show this help message"
    echo ""
    echo "Example: $0 -v 0.12.9"
    exit 1
}

create_tarball() {
    local folder="$1"
    local parent_dir
    local folder_name

    parent_dir="$(dirname "$folder")"
    folder_name="$(basename "$folder")"

    echo "Compressing $folder to ${folder}.tar.gz"
    if tar -czf "${folder}.tar.gz" -C "$parent_dir" "$folder_name"; then
        echo "  Created: ${folder}.tar.gz"
    else
        echo "  Error: failed to create ${folder}.tar.gz" >&2
        exit 1
    fi
}

read_package_version() {
    sed -n 's/^[[:space:]]*"version":[[:space:]]*"\([^"]*\)".*/\1/p' package.json | head -n 1
}

read_spec_version() {
    sed -n 's/^Version:[[:space:]]*\([^[:space:]]*\).*/\1/p' SPEC/repogoon.spec | head -n 1
}

while getopts "v:o:h" opt; do
    case "$opt" in
        v)
            VERSION="$OPTARG"
            ;;
        o)
            OUTPUT_ROOT="$OPTARG"
            ;;
        h)
            usage
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            usage
            ;;
    esac
done

if [ -z "$VERSION" ]; then
    echo "Error: Version number is required" >&2
    usage
fi

if [ ! -d .git ]; then
    echo "Error: run this script from the RepoGoon repository root" >&2
    exit 1
fi

PACKAGE_VERSION="$(read_package_version)"
SPEC_VERSION="$(read_spec_version)"

if [ "$PACKAGE_VERSION" != "$VERSION" ]; then
    echo "Warning: package.json version is $PACKAGE_VERSION, requested $VERSION" >&2
fi

if [ "$SPEC_VERSION" != "$VERSION" ]; then
    echo "Warning: SPEC/repogoon.spec version is $SPEC_VERSION, requested $VERSION" >&2
fi

VERSIONED_FOLDER="$OUTPUT_ROOT/${PACKAGE_NAME}-${VERSION}"

if [ -e "$VERSIONED_FOLDER" ]; then
    echo "Error: $VERSIONED_FOLDER already exists" >&2
    echo "Remove it first or choose another output root with -o." >&2
    exit 1
fi

mkdir -p "$VERSIONED_FOLDER"

echo "Copying tracked RepoGoon files to $VERSIONED_FOLDER"
git ls-files -z | tar --null -T - -cf - | tar -xf - -C "$VERSIONED_FOLDER"

echo "Removing local-only packaging artifacts from staged tree"
rm -rf \
    "$VERSIONED_FOLDER/.git" \
    "$VERSIONED_FOLDER/dist" \
    "$VERSIONED_FOLDER/node_modules" \
    "$VERSIONED_FOLDER/coverage" \
    "$VERSIONED_FOLDER/lfs-objects" \
    "$VERSIONED_FOLDER/public/avatars"

create_tarball "$VERSIONED_FOLDER"

echo "Release preparation completed!"
echo "Version: $VERSION"
echo "Files copied to:"
echo "  - $VERSIONED_FOLDER"
echo "Tarball created:"
echo "  - ${VERSIONED_FOLDER}.tar.gz"
