#!/usr/bin/env bash
set -euo pipefail

INPUT="${1:-assets/img/logo/ats-logo-tran.png}"
OUTPUT="${2:-assets/img/logo/ats-logo-tran.svg}"

if [ ! -f "$INPUT" ]; then
  echo "Error: input file not found: $INPUT" >&2
  exit 1
fi

if ! command -v magick >/dev/null 2>&1 && ! command -v convert >/dev/null 2>&1; then
  echo "Error: ImageMagick ('magick' or 'convert') is required. Try: brew install imagemagick" >&2
  exit 1
fi

if ! command -v potrace >/dev/null 2>&1; then
  echo "Error: 'potrace' is required. Try: brew install potrace" >&2
  exit 1
fi

TMPDIR="$(mktemp -d)"
trap 'rm -rf "$TMPDIR"' EXIT

PGM="$TMPDIR/logo.pgm"

if command -v magick >/dev/null 2>&1; then
  magick "$INPUT" -resize 512x512 -colorspace Gray -threshold 50% "$PGM"
else
  convert "$INPUT" -resize 512x512 -colorspace Gray -threshold 50% "$PGM"
fi

potrace "$PGM" -s -o "$OUTPUT" --flat --turdsize 10

echo "Done. SVG written to: $OUTPUT"

