#! /bin/sh

case "$1" in

  "obj")

    if test "${GAMBUILD_VERBOSE}" != ""; then
      echo "cat ${BUILD_OBJ_INPUT_FILENAMES_PARAM} > \"${BUILD_OBJ_OUTPUT_FILENAME_PARAM}\""
    fi

    cat ${BUILD_OBJ_INPUT_FILENAMES_PARAM} > "${BUILD_OBJ_OUTPUT_FILENAME_PARAM}"
    ;;

  "dyn")

    if test "${GAMBUILD_VERBOSE}" != ""; then
      echo "cat ${BUILD_DYN_INPUT_FILENAMES_PARAM} > \"${BUILD_DYN_OUTPUT_FILENAME_PARAM}\""
    fi

    cat ${BUILD_DYN_INPUT_FILENAMES_PARAM} > "${BUILD_DYN_OUTPUT_FILENAME_PARAM}"
    ;;

  "lib")

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

    echo BUILD_LIB not yet implemented
    ;;

  "exe")

    if test "${BUILD_EXE_BASE_LIBRARY_PARAM}" = ""; then
      BUILD_EXE_BASE_LIB="${GAMBITDIR_LIB}/_gambit.js"
    else
      BUILD_EXE_BASE_LIB="${BUILD_EXE_BASE_LIBRARY_PARAM}.js"
    fi

    if test "${GAMBUILD_VERBOSE}" != ""; then
      case "${BUILD_EXE_OUTPUT_FILENAME_PARAM}" in
        *.html) echo "echo '<!doctype html>' > \"${BUILD_EXE_OUTPUT_FILENAME_PARAM}\""
                echo "echo '<html><head><meta charset=\"utf-8\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body><script>' >> \"${BUILD_EXE_OUTPUT_FILENAME_PARAM}\""
                echo cat ${BUILD_EXE_INPUT_FILENAMES_PARAM} "\"${BUILD_EXE_BASE_LIB}\" >> \"${BUILD_EXE_OUTPUT_FILENAME_PARAM}\""
                echo "'</script></body></html>' >> \"${BUILD_EXE_OUTPUT_FILENAME_PARAM}\""
                ;;
          *.js) echo cat ${BUILD_EXE_INPUT_FILENAMES_PARAM} "\"${BUILD_EXE_BASE_LIB}\" > \"${BUILD_EXE_OUTPUT_FILENAME_PARAM}\""
                ;;
             *) echo "echo \"#! /usr/bin/env node\" > \"${BUILD_EXE_OUTPUT_FILENAME_PARAM}\""
                echo cat ${BUILD_EXE_INPUT_FILENAMES_PARAM} "\"${BUILD_EXE_BASE_LIB}\" >> \"${BUILD_EXE_OUTPUT_FILENAME_PARAM}\""
                echo "chmod +x \"${BUILD_EXE_OUTPUT_FILENAME_PARAM}\""
                ;;
      esac
    fi

    case "${BUILD_EXE_OUTPUT_FILENAME_PARAM}" in
      *.html) echo '<!doctype html>' > "${BUILD_EXE_OUTPUT_FILENAME_PARAM}"
              echo '<html><head><meta charset="utf-8"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><script>' >> "${BUILD_EXE_OUTPUT_FILENAME_PARAM}"
              cat ${BUILD_EXE_INPUT_FILENAMES_PARAM} "${BUILD_EXE_BASE_LIB}" >> "${BUILD_EXE_OUTPUT_FILENAME_PARAM}"
              echo '</script></body></html>' >> "${BUILD_EXE_OUTPUT_FILENAME_PARAM}"
              ;;
        *.js) cat ${BUILD_EXE_INPUT_FILENAMES_PARAM} "${BUILD_EXE_BASE_LIB}" > "${BUILD_EXE_OUTPUT_FILENAME_PARAM}"
              ;;
           *) echo "#! /usr/bin/env node" > "${BUILD_EXE_OUTPUT_FILENAME_PARAM}"
              cat ${BUILD_EXE_INPUT_FILENAMES_PARAM} "${BUILD_EXE_BASE_LIB}" >> "${BUILD_EXE_OUTPUT_FILENAME_PARAM}"
              chmod +x "${BUILD_EXE_OUTPUT_FILENAME_PARAM}"
              ;;
    esac
    ;;

  *)
    echo "gambuild-js unknown operation \"$1\""
    exit 1
    ;;

esac
