#!/bin/sh

# This file has been placed in the public domain.

set -eu

prefix=/usr/local

usage() {
  cat <<EOF
Usage: ./configure [--prefix=DIR] [VARIABLE=VALUE]...

Configure the installation prefix (default: /usr/local).
Unsupported build variables are ignored with a warning.
EOF
}

while test $# -gt 0; do
  case $1 in
    --prefix=*)
      prefix=${1#*=}
      ;;
    --prefix)
      if test $# -lt 2; then
        echo "configure: option '--prefix' requires an argument" >&2
        exit 2
      fi
      prefix=$2
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *=*)
      variable=${1%%=*}
      case $variable in
        ""|[!A-Za-z_]*|*[!A-Za-z0-9_]*)
          echo "configure: unrecognized option '$1'" >&2
          echo "Try './configure --help' for more information." >&2
          exit 2
          ;;
        *)
          echo "configure: WARNING: unsupported variable '$variable' is ignored" >&2
          ;;
      esac
      ;;
    *)
      echo "configure: unrecognized option '$1'" >&2
      echo "Try './configure --help' for more information." >&2
      exit 2
      ;;
  esac
  shift
done

if test -z "$prefix"; then
  echo "configure: installation prefix must not be empty" >&2
  exit 2
fi

cat >.config.mk <<EOF
# This makefile fragment is generated by configure.
prefix = $prefix
EOF

echo "configured installation prefix: $prefix"
