#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2026 Andrew Oberstar <andrew@ajoberstar.org>
# SPDX-License-Identifier: CC0-1.0
# Open a staged original scan in GIMP for manual cropping.
#
# Usage:
#     ./manual-crop <item-num> <front|back>
#
# The script opens the original scan in GIMP and prints the path where the
# cropped result should be exported. After cropping, export from GIMP to the
# printed *_cropped.png path, then refresh the Quixote import page.
#
# The staging media directory defaults to /var/lib/quixote/media and can be
# overridden with QUIXOTE_MEDIA_DIR.

set -euo pipefail

ITEM_NUM=$(printf '%04d' "$1")
SIDE="$2"
MEDIA_DIR="${QUIXOTE_MEDIA_DIR:-/var/lib/quixote/media}"
ORIG="$MEDIA_DIR/staging/item-$ITEM_NUM/${SIDE}_original.png"
CROP="$MEDIA_DIR/staging/item-$ITEM_NUM/${SIDE}_cropped.png"

if [[ ! -f "$ORIG" ]]; then
    echo "Original not found: $ORIG" >&2
    exit 1
fi

echo "Opening $ORIG in GIMP..."
echo "When done, export the cropped cover to:"
echo "  $CROP"

gimp "$ORIG" >/dev/null 2>&1 &
