#!/bin/sh

# Insert annotations (from an annotation file) into a class file.
# For usage information, run: insert-annotations --help
# See the Annotation File Utilities documentation for more information.

# If the very first argument is "--debug-script", debug this script (but
# don't pass --debug-script to the underlying program).
DEBUG=0
if [ "$1" = "--debug-script" ]; then
  DEBUG=1
  shift 1
fi

AFU=${AFU:-$(dirname $0)/..}
ANNOTATION_FILE_UTILS=${AFU}/bin:${AFU}/../scene-lib/bin:${AFU}/../asmx/bin:${AFU}/annotation-file-utilities.jar
LANGTOOLS=${LANGTOOLS:-${AFU}/../../jsr308-langtools}
JAVAC_JAR=${JAVAC_JAR:-${LANGTOOLS}/dist/lib/javac.jar}

if [ "$DEBUG" = "1" ]; then
  echo "--- start of insert-annotations debugging output"
  echo "AFU=${AFU}"
  echo "ANNOTATION_FILE_UTILS=${ANNOTATION_FILE_UTILS}"
  echo "LANGTOOLS=${LANGTOOLS}"
  echo "JAVAC_JAR=${JAVAC_JAR}"
  # Keep this in sync with the actual command below.
  echo "java -ea -cp ${JAVAC_JAR}:${AFU}/lib/plume-lib.jar:${ANNOTATION_FILE_UTILS}:${CLASSPATH} annotations.io.classfile.ClassFileWriter $@"
  echo "--- end of insert-annotations debugging output"
fi

# Needs CLASSPATH to find user files
java -ea -cp ${JAVAC_JAR}:${AFU}/lib/plume-lib.jar:${ANNOTATION_FILE_UTILS}:${CLASSPATH} annotations.io.classfile.ClassFileWriter "$@"