# Remembering the number of arguments to this script, in order
# to parse them properly.
argc="$#"

INSTALL_PREFIX=/usr/local

usage () {
    echo "usage: ./configure [options] [<directory>]"
    echo
    echo "  [-]-install_prefix <directory>"
    echo "    set <directory> as the directory prefix for installation of the"
    echo "    Tiny QCMl stuff."
    echo "    (Default value for <directory> is $INSTALL_PREFIX.)"
}

touch .depend

# #############################################################################
# Parsing arguments.
case ${argc} in
  0) ;;
  *)
  while : ; do
    # If nothing else to parse then end the loop.
    case $# in 0) break;; *);; esac
    case "$1" in
      -install_prefix | --install_prefix)
       INSTALL_PREFIX="$2";
       shift 2;;
      -help | --help)
       usage;
       shift;
       exit 0;;
      -*)
       option="$1";
       echo "./configure: bad option '${option}'" >&2 && \
       echo "For help, use ./configure -help" >&2 && \
       shift;
       exit 2;;
    esac;
  done
  ;;
esac

# At this point, if INSTALL_PREFIX has been overriden while parsing options,
# both the 4 following variables will take benefit of this overriding.
INSTALL_BIN_DIR=${INSTALL_PREFIX}/bin
INSTALL_LIB_DIR=${INSTALL_PREFIX}/lib/tiny_qcml
INSTALL_MAN_DIR=${INSTALL_PREFIX}/man
INSTALL_DOC_DIR=${INSTALL_PREFIX}/share/tiny_qcml

# #############################################################################
# Generate the installation related source file.
CONFIG_SUMMARY_DOT_ML=installation.ml

rm -f $CONFIG_SUMMARY_DOT_ML
echo "(* This file is automatically generated by configure. Do not manually edit. *)" \
     > $CONFIG_SUMMARY_DOT_ML
echo "let install_lib_dir = \"$INSTALL_LIB_DIR\" ;;" >> $CONFIG_SUMMARY_DOT_ML
echo "(* End of generated source file. *)" >> $CONFIG_SUMMARY_DOT_ML
chmod a-w $CONFIG_SUMMARY_DOT_ML

# #############################################################################
# Generate the installation vars.
INSTALLATION_VARS_FILE=.install_var

rm -f $INSTALLATION_VARS_FILE
echo "INSTALL_BIN_DIR=$INSTALL_BIN_DIR" > $INSTALLATION_VARS_FILE
echo "INSTALL_LIB_DIR=$INSTALL_LIB_DIR" >> $INSTALLATION_VARS_FILE
echo "INSTALL_MAN_DIR=$INSTALL_MAN_DIR" >> $INSTALLATION_VARS_FILE
echo "INSTALL_DOC_DIR=$INSTALL_DOC_DIR" >> $INSTALLATION_VARS_FILE
chmod a-w $INSTALLATION_VARS_FILE


# #############################################################################
# Summary feedback.
echo "Configuration summary for Tiny QCMl:"
echo "  binaries -> $INSTALL_BIN_DIR"
echo "  libraries -> $INSTALL_LIB_DIR"
echo "  man pages -> $INSTALL_MAN_DIR"
echo "  other documentation -> $INSTALL_DOC_DIR"
