#!/usr/bin/env bash
#
# Wrapper for the OFFICIAL UVtoolsCmd release, using the generic `run` command
# with the new FileArray-capable PCBExposure operation.
#
# Mirrors the interface of pcb-expose-patched.sh, but maps everything onto:
#
#   UVtoolsCmd run <dummy-file> PCBExposure \
#       -p FileArray=<gerber1> -p FileArray=<gerber2> ... \
#       -p InvertColor=true -p Mirror=true -p ExposureTime=120 ... \
#       --output <output-file>
#
# Usage:
#   pcb-expose-official.sh <dummy-file> <gerber-files...> [options]
#
# Example:
#   pcb-expose-official.sh Dummy.pm4n \
#       gerbers/Flow_Controller_Panel-Front.gtl \
#       --invert --mirror --exposure 120 \
#       --output Flow_Controller_Panel-Front.pm4n
#
# Options:
#   -e, --exposure <seconds>   Sets ExposureTime
#   -i, --invert               Sets InvertColor=true
#   -m, --mirror               Sets Mirror=true
#       --merge                Sets MergeFiles=true
#       --aa                   Sets EnableAntiAliasing=true
#       --offset-x <mm>        Sets OffsetX
#       --offset-y <mm>        Sets OffsetY
#   -o, --output <file>        Output file (default: overwrite dummy file)
#
# NOTE: --invert-polarity (per-file InvertPolarity) is NOT supported by this
#       wrapper, because the official FileArray property only accepts plain
#       file paths, not the InvertPolarity flag per file. If you need that,
#       use pcb-expose-patched.sh instead.
#
# Configure the path to your official build below, or override via:
#   UVTOOLSCMD=/path/to/UVtoolsCmd pcb-expose-official.sh ...

set -euo pipefail

UVTOOLSCMD="${UVTOOLSCMD:-$HOME/UVtools/UVtoolsCmd}"

if [ ! -x "$UVTOOLSCMD" ]; then
    echo "Error: UVtoolsCmd not found or not executable at: $UVTOOLSCMD" >&2
    echo "Set UVTOOLSCMD=/path/to/UVtoolsCmd or edit this script." >&2
    exit 1
fi

if [ $# -lt 2 ]; then
    echo "Usage: $0 <dummy-file> <gerber-files...> [options]" >&2
    exit 1
fi

DUMMY_FILE="$1"
shift

GERBERS=()
PROPS=() # FileArray entries + scalar properties, built below
OUTPUT_FILE=""

EXPOSURE=""
INVERT="false"
MIRROR="false"
MERGE="false"
AA="false"
OFFSET_X=""
OFFSET_Y=""
INVERT_POLARITY="false"

while [ $# -gt 0 ]; do
    case "$1" in
        -e|--exposure)
            EXPOSURE="$2"; shift 2 ;;
        -i|--invert)
            INVERT="true"; shift ;;
        -m|--mirror)
            MIRROR="true"; shift ;;
        --merge)
            MERGE="true"; shift ;;
        --aa)
            AA="true"; shift ;;
        --offset-x)
            OFFSET_X="$2"; shift 2 ;;
        --offset-y)
            OFFSET_Y="$2"; shift 2 ;;
        --invert-polarity)
            INVERT_POLARITY="true"; shift ;;
        -o|--output)
            OUTPUT_FILE="$2"; shift 2 ;;
        -*)
            echo "Unknown option: $1" >&2
            exit 1 ;;
        *)
            GERBERS+=("$1"); shift ;;
    esac
done

if [ "$INVERT_POLARITY" = "true" ]; then
    echo "Warning: --invert-polarity is not supported via the official FileArray" >&2
    echo "         property and will be ignored. Use pcb-expose-patched.sh instead." >&2
fi

if [ ${#GERBERS[@]} -eq 0 ]; then
    echo "Error: specify at least one gerber file." >&2
    exit 1
fi

for g in "${GERBERS[@]}"; do
    if [ ! -f "$g" ]; then
        echo "Error: gerber file not found: $g" >&2
        exit 1
    fi
done

# Build -p FileArray=... entries
for g in "${GERBERS[@]}"; do
    PROPS+=(-p "FileArray=$g")
done

[ -n "$EXPOSURE" ]  && PROPS+=(-p "ExposureTime=$EXPOSURE")
[ "$INVERT" = "true" ] && PROPS+=(-p "InvertColor=true")
[ "$MIRROR" = "true" ] && PROPS+=(-p "Mirror=true")
[ "$MERGE"  = "true" ] && PROPS+=(-p "MergeFiles=true")
[ "$AA"     = "true" ] && PROPS+=(-p "EnableAntiAliasing=true")
[ -n "$OFFSET_X" ] && PROPS+=(-p "OffsetX=$OFFSET_X")
[ -n "$OFFSET_Y" ] && PROPS+=(-p "OffsetY=$OFFSET_Y")

CMD=("$UVTOOLSCMD" run "$DUMMY_FILE" PCBExposure "${PROPS[@]}")
[ -n "$OUTPUT_FILE" ] && CMD+=(--output "$OUTPUT_FILE")

echo "+ ${CMD[*]}" >&2
exec "${CMD[@]}"
