#! /bin/sh

case "$1" in

  "obj")

    if test "${GAMBCOMP_VERBOSE}" != ""; then
      echo "cat ${BUILD_OBJ_INPUT_FILENAMES} > \"${BUILD_OBJ_OUTPUT_FILENAME}\""
    fi

    cat ${BUILD_OBJ_INPUT_FILENAMES} > "${BUILD_OBJ_OUTPUT_FILENAME}"
    ;;

  "dyn")

    if test "${GAMBCOMP_VERBOSE}" != ""; then
      echo "cat ${BUILD_DYN_INPUT_FILENAMES} > \"${BUILD_DYN_OUTPUT_FILENAME}\""
    fi

    cat ${BUILD_DYN_INPUT_FILENAMES} > "${BUILD_DYN_OUTPUT_FILENAME}"
    ;;

  "lib")

    if test "${GAMBCOMP_VERBOSE}" != ""; then
      echo "echo BUILD_LIB not yet implemented"
    fi

    echo BUILD_LIB not yet implemented
    ;;

  "exe")

    if test "${GAMBCOMP_VERBOSE}" != ""; then
      echo "mkdir \"${BUILD_EXE_OUTPUT_FILENAME}_temp\""
      echo "javac -d \"${BUILD_EXE_OUTPUT_FILENAME}_temp\" $(echo $BUILD_EXE_INPUT_FILENAMES | sed 's/\.o /.java /g; s/\.o$/.java/') \"${GAMBITDIR_LIB}/_gambit.java\""
      echo "jar cfe \"${BUILD_EXE_OUTPUT_FILENAME}.jar\" $(echo ${BUILD_EXE_OUTPUT_FILENAME} | grep -oP '[^/]*_(?=\.o)') \"${BUILD_EXE_OUTPUT_FILENAME}_temp/*.class\""
      echo "rm -rf \"${BUILD_EXE_OUTPUT_FILENAME}_temp\""

      echo "echo \"#! /bin/sh\" > \"${BUILD_EXE_OUTPUT_FILENAME}\""
      echo "echo \"java -jar ${BUILD_EXE_OUTPUT_FILENAME}.jar\" >> \"${BUILD_EXE_OUTPUT_FILENAME}\""
      echo "chmod +x \"${BUILD_EXE_OUTPUT_FILENAME}\""
    fi

    mkdir "${BUILD_EXE_OUTPUT_FILENAME}_temp"
    javac -d "${BUILD_EXE_OUTPUT_FILENAME}_temp" $(echo $BUILD_EXE_INPUT_FILENAMES | sed 's/\.o /.java /g; s/\.o$/.java/') "${GAMBITDIR_LIB}/_gambit.java"
    jar cfe "${BUILD_EXE_OUTPUT_FILENAME}.jar" $(echo ${BUILD_EXE_OUTPUT_FILENAME} | grep -oP '[^/]*_(?=\.o)') "${BUILD_EXE_OUTPUT_FILENAME}_temp/*.class"
    rm -rf "${BUILD_EXE_OUTPUT_FILENAME}_temp"

    echo "#! /bin/sh" > "${BUILD_EXE_OUTPUT_FILENAME}"
    echo "java -jar ${BUILD_EXE_OUTPUT_FILENAME}.jar" >> "${BUILD_EXE_OUTPUT_FILENAME}"
    chmod +x "${BUILD_EXE_OUTPUT_FILENAME}"
    ;;

  *)
    echo "gambcomp-java unknown operation \"$1\""
    exit 1
    ;;

esac
