#!/bin/bash
#
# Forum Press Zone - Release Build Script
# Creates a clean zip file for WordPress.org submission
#

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PLUGIN_ROOT="$SCRIPT_DIR"

# Get version from main plugin file
VERSION=$(grep -oP "Version:\s*\K[0-9.]+" "$PLUGIN_ROOT/forum-press-zone.php")
PLUGIN_SLUG="forum-press-zone"
BUILD_DIR="$PLUGIN_ROOT/release-build"
ZIP_NAME="$(dirname "$PLUGIN_ROOT")/${PLUGIN_SLUG}-${VERSION}.zip"
TARGET_DIR="$BUILD_DIR/$PLUGIN_SLUG"

copy_file_if_exists() {
    local source_path="$1"
    local destination_dir="$2"
    mkdir -p "$destination_dir"
    if [ -f "$source_path" ]; then
        cp "$source_path" "$destination_dir/"
    fi
}

copy_dir_if_exists() {
    local source_path="$1"
    local destination_dir="$2"
    mkdir -p "$destination_dir"
    if [ -d "$source_path" ]; then
        cp -r "$source_path" "$destination_dir/"
    fi
}

echo "Building ${PLUGIN_SLUG} v${VERSION}..."

# Step 1: Build production assets
echo "→ Building CSS assets..."
pushd "$PLUGIN_ROOT" > /dev/null
npm run build:css
popd > /dev/null

echo "→ Building admin JS..."
pushd "$PLUGIN_ROOT/admin" > /dev/null
npm run build
popd > /dev/null

# Step 2: Clean previous build
echo "→ Cleaning previous build..."
rm -rf "$BUILD_DIR"
mkdir -p "$TARGET_DIR"

# Step 3: Copy plugin files (excluding dev files)
echo "→ Copying plugin files..."

# Core files
copy_file_if_exists "$PLUGIN_ROOT/forum-press-zone.php" "$TARGET_DIR"
copy_file_if_exists "$PLUGIN_ROOT/readme.txt" "$TARGET_DIR"
copy_file_if_exists "$PLUGIN_ROOT/wpml-config.xml" "$TARGET_DIR"
copy_file_if_exists "$PLUGIN_ROOT/index.php" "$TARGET_DIR"
copy_file_if_exists "$PLUGIN_ROOT/LICENSE" "$TARGET_DIR"
copy_file_if_exists "$PLUGIN_ROOT/LICENSE.txt" "$TARGET_DIR"
# package.json intentionally excluded (build tooling, not runtime)

# Runtime directories
copy_dir_if_exists "$PLUGIN_ROOT/includes" "$TARGET_DIR"
copy_dir_if_exists "$PLUGIN_ROOT/templates" "$TARGET_DIR"
copy_dir_if_exists "$PLUGIN_ROOT/languages" "$TARGET_DIR"
copy_dir_if_exists "$PLUGIN_ROOT/assets" "$TARGET_DIR"

# Admin assets and source (for GPL compliance)
if [ -d "$PLUGIN_ROOT/admin" ]; then
    mkdir -p "$TARGET_DIR/admin"
    copy_dir_if_exists "$PLUGIN_ROOT/admin/build" "$TARGET_DIR/admin"
    copy_dir_if_exists "$PLUGIN_ROOT/admin/src-vanilla" "$TARGET_DIR/admin"
    copy_file_if_exists "$PLUGIN_ROOT/admin/class-presszone-forum-admin-menu.php" "$TARGET_DIR/admin"
    # admin/package.json and webpack.config.js excluded (build tooling, not runtime)
fi

# Step 4: Remove any dev files that might have slipped through
echo "→ Cleaning dev artifacts..."
find "$BUILD_DIR" -name "*.map" -delete
find "$BUILD_DIR" -name ".DS_Store" -delete
find "$BUILD_DIR" -name "Thumbs.db" -delete
find "$BUILD_DIR" -name ".git*" -delete
find "$BUILD_DIR" -name "*.pretty.json" -delete
find "$BUILD_DIR" -name "package-lock.json" -delete
find "$BUILD_DIR" -name "composer.lock" -delete
# Remove SCSS source from compiled assets dir (GPL sources are in admin/src-vanilla)
find "$TARGET_DIR/assets" -name "*.scss" -delete 2>/dev/null || true

# Step 5: Create zip (supports both zip and 7z)
echo "→ Creating zip archive..."
rm -f "$ZIP_NAME"
pushd "$BUILD_DIR" > /dev/null
if command -v zip &> /dev/null; then
    zip -rq "$ZIP_NAME" "$PLUGIN_SLUG" -x "*.git*"
elif command -v 7z &> /dev/null; then
    7z a -tzip -mx=9 "$ZIP_NAME" "$PLUGIN_SLUG" > /dev/null
else
    echo "Error: Neither 'zip' nor '7z' found. Install one with: sudo apt install zip"
    exit 1
fi
popd > /dev/null

# Step 6: Cleanup
rm -rf "$BUILD_DIR"

# Done
ZIP_SIZE=$(du -h "$ZIP_NAME" | cut -f1)
echo ""
echo "✓ Build complete: $(basename "$ZIP_NAME") ($ZIP_SIZE)"
echo ""
echo "Next steps:"
echo "  1. Test the zip by installing on a fresh WordPress site"
echo "  2. Run Plugin Check plugin on it"
echo "  3. Submit to WordPress.org SVN"
