#!/usr/bin/env zsh

# Parse arguments for --tone and --move options
TONE_ARG=
MOVE_ARG=

while [[ $# -gt 0 ]]; do
  case $1 in
    --tone)
      TONE_ARG="$2"
      shift 2
      ;;
    --move)
      MOVE_ARG=--move
      shift
      ;;
    --help|-h)
      NAME=$(basename "$0")
      echo "Usage: ${NAME} [--tone <tone>] [--move] [<filename.txt> | -]"
      echo
      echo Arguments:
      echo "  --tone <tone>           Speech tone (slow, fast, romantic, etc.)"
      echo "  --move                  Move output files to ZAI directory instead of local output"
      echo "  <filename.txt>          Text file to convert to speech"
      echo "  -                       Read text from stdin instead of file"
      echo
      echo Examples:
      echo "  ${NAME} myfile.txt                       # Basic conversion"
      echo "  ${NAME} --tone romantic love_letter.txt  # Romantic tone"
      echo "  ${NAME} --move myfile.txt                # Move to ZAI directory"
      echo "  ${NAME} --tone slow --move poem.txt      # Slow tone + move"
      echo "  echo 'Hello' | ${NAME} -                 # Convert stdin text"
      echo "  ${NAME} --help                           # Show this help message"
      echo
      echo Encoded speech audio filename will be printed to stdout.
      exit 0
      ;;
    *)
      # Store remaining arguments for processing
      break
      ;;
  esac
done

# Build arguments for talking script, including tone and move if specified
TALKING_ARGS=()
if [[ -n "$TONE_ARG" ]]; then
  TALKING_ARGS+=("$TONE_ARG")
fi
if [[ -n "$MOVE_ARG" ]]; then
  TALKING_ARGS+=("$MOVE_ARG")
fi

# Handle stdin input if needed
if [[ "${@:$#}" == - || -z "$1" ]]; then
  tmp=$(mktemp $TMPDIR/$(basename $0).$$.XXXXXX).txt
  cat > $tmp
  TALKING_ARGS+=("${@[1,-2]}" "$tmp")
else
  TALKING_ARGS+=("$@")
fi

# Call the talking script with all arguments
talking-agents.sh "${TALKING_ARGS[@]}"
