#!/bin/sh

# Note: future version will have a syntax something like
#   ./configure [OPTIONS]
#   Where OPTIONS is any of:
#     --help
#     --codes CODE1 ...
#     --sizes SIZE1 ...
#     --jargons JARGON1 ...
#     --extras EXTRA1 ...
#     --vars VAR1=VAL1 ...
# which is why I warn when --vars is not used before VAR1=VAL1

# Avoid depending upon Character Ranges.
# Taken from autoconf 2.50
cr_az='abcdefghijklmnopqrstuvwxyz'
cr_AZ='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
cr_09='0123456789'
cr_alnum=$cr_az$cr_AZ$cr_09

# also taken form autoconf
case `echo "testing\c"; echo 1,2,3`,`echo -n testing; echo 1,2,3` in
  *c*,-n*) ECHO_N= ECHO_C='
' ECHO_T='	' ;;
  *c*,*  ) ECHO_N=-n ECHO_C= ECHO_T= ;;
  *)       ECHO_N= ECHO_C='\c' ECHO_T= ;;
esac


mode=none
for option
do
  case $option in
    --vars) 
      mode=vars
      ;;
    *=*)
      if test $mode != vars; then
        echo "Warning: future versions will require --vars before variables are set"
        mode=vars
      fi	
      # Taken from autoconf 2.50
      envvar=`expr "x$option" : 'x\([^=]*\)='`
      # Reject names that are not valid shell variable names.
      expr "x$envvar" : ".*[^_$cr_alnum]" >/dev/null &&
        { echo "$as_me: error: invalid variable name: $envvar" >&2
      { (exit 1); exit 1; }; }
      optarg=`echo "$optarg" | sed "s/'/'\\\\\\\\''/g"`
      eval "$envvar='$optarg'"
      export $envvar
      ;;
    --help)
      echo "Usage: ./configure [--help | --vars VAR1=VAL1 ...]"
      echo "  Note: Variables may also be set in the environment brefore running config"
      echo "  Useful vars: ASPELL ASPELL_PARMS WORD_LIST_COMPRESS DESTDIR"
      exit 0
      ;;
    *)
      echo "Error: unrecognized option $option";
      exit 1 
    ;;
  esac
done

if test x = "x$ASPELL"; then
    ASPELL=aspell;
fi

nok=false
echo $ECHO_N "Finding aspell program ... $ECHO_C"
if test -x "$ASPELL"; then
    if "$ASPELL" --version 1> /dev/null 2>&1; then
	echo "$ASPELL"
    else
	echo "not found"
	nok=true
    fi
else
    echo "not found"
    nok=true
fi

if test x = "x$WORD_LIST_COMPRESS"; then
    WORD_LIST_COMPRESS=word-list-compress;
fi


echo $ECHO_N "Finding word-list-compress program ... $ECHO_C"
if test -x "$WORD_LIST_COMPRESS"; then
    if "$WORD_LIST_COMPRESS" 1> /dev/null --version 2>&1; then
        echo "$WORD_LIST_COMPRESS"
    else
	# some word list compress have 1 return status on --version !!
	if test $? -eq 1; then
            echo "$WORD_LIST_COMPRESS"
	else
            echo "not found"
            nok=true
	fi
    fi
else	    
    echo "not found"
    nok=true
fi

echo $ECHO_N "Finding Dictionary file location ... $ECHO_C"
dictdir=`$ASPELL dump config dict-dir`
echo $dictdir

echo $ECHO_N "Finding Data file location ... $ECHO_C"
datadir=`$ASPELL dump config data-dir`
echo $datadir

echo "ASPELL = $ASPELL" > Makefile
echo "ASPELL_FLAGS = $ASPELL_FLAGS" >> Makefile
echo "WORD_LIST_COMPRESS = $WORD_LIST_COMPRESS" >> Makefile
echo "DESTDIR = $DESTDIR" >> Makefile
echo "dictdir = $dictdir" >> Makefile
echo "datadir = $datadir" >> Makefile
echo                      >> Makefile
cat Makefile.pre >> Makefile

if $nok; then
    echo "Some problem have been found, solve them and run again configure before running make" > /dev/stderr
fi
