#!/bin/sh
# Configure program.

IFS=`printf '\n\t'`

change_makefile() {
  # $1 = value in makefile to change
  # $2 = what to change it to
  sed "s|^${1}=.*|${1}=${2}|" < Makefile > ,1
  mv ,1 Makefile
}

# Create config.status so we can re-run the options later.
# To do this, we simply record the options and re-run configure
# (escaped so that the shell won't mess with them).
# Don't create it if it's already in use, that will fail on Cygwin
# and is a bad idea anyway.
if [ "${DONT_CREATE_CONFIG_STATUS:+true}" != true ] ; then
  rm -f config.status
  echo "#!/bin/sh" > config.status
  echo "export DONT_CREATE_CONFIG_STATUS=true" >> config.status
  printf '%s' './configure' >> config.status
  for option ; do
   escapedoption=`printf '%s' $option | sed -e 's/[^-A-Za-z0-9=/]/\\\&/g'`
   printf " %s" "$escapedoption" >> config.status
  done
  echo >> config.status
  chmod a+x config.status
fi

# Generate Makefile from Makefile.in and the passed-in options.
rm -f Makefile
echo '# DO NOT EDIT THIS FILE BY HAND!' > Makefile
echo '# Generated from Makefile.in by ./configure' >> Makefile
echo '#' >> Makefile
cat Makefile.in >> Makefile

# This does not use getopt(1); option processing for configure is actually
# rather complicated, and we want to permit "junk" options, so getopt(1)
# does not work well for this program.

# If values (inc. directories) contain "=", you MUST use the --ITEM=VALUE or
# ITEM=VALUE formats, since there's no other way to disambiguate the "=".

while [ $# -gt 0 ] ; do
 case "$1" in
   --*=*) left="${1%%=*}"
          right="${1#*=}"
          haseq=1          ;;
   *)     left="$1"
          right=""
          haseq=0          ;;
 esac
 shift
 # echo "got left=<${left}> right=<${right}>"
 case "$left" in
   --destdir)
     echo "Error - do not specify a DESTDIR value during configuration."
     echo "Do that later as part of 'make install'."
     exit 1 ;;
   --prefix|--exec-prefix|--bindir|--sbindir|--libexecdir|--sysconfigdir|--sharedstatedir|--localstatedir|--libdir|--includedir|--oldincludedir|--datarootdir|--datadir|--infodir|--localedir|--mandir|--docdir|--htmldir|--dvidir|--pdfdir|--psdir|--scriptdir|--man1dir|--rpmbuilddir|--build|--host|--target)
     if [ "$haseq" -eq 0 ] ; then
       right="$1"
       shift
     fi
     if [ "--prefix" = "$1" -a -z "$right" ] ; then
       echo "Error! $left must not have an empty value!"
       exit 1
     fi
     # Use printf, not echo; we have leading dashes which may cause trouble
     left=`printf "%s" "$left" | tr -d '-'`
     # Handle exec-prefix specially; it has a hypen in its name.
     if [ "$left" = "execprefix" ] ; then
       left="exec_prefix"
     fi
     change_makefile "$left" "$right" ;;
   *) echo "Ignored configuration parameter $left" ;;
 esac
done

# Remove write permissions, to help avoid accidental editing
chmod a-w Makefile

