#!/bin/bash

PS3="Your choice ==> "

yorn () {
  echo -n "$1 (y/n) [n] "
  read ANSWER
  if test "$ANSWER" = 'y'; then true; else false; fi
}
nory () {
  echo -n "$1 (y/n) [y] "
  read ANSWER
  if test "$ANSWER" = 'n'; then false; else true; fi
}

message () { echo; echo '#' "$1"; }
info () { echo "$1"; }
warning () { echo '*** Warning:'; echo "$1"; }
ERRORS=false
error () { ERRORS=true; echo '*** Error:'; echo "$1"; }

test -f Makefile.config && \
  ! yorn "--- Override existing Makefile.config?" \
  && exit 1

findcommand () {
  VAR=$1
  VAL=$2
  COM=$3
  ASK=$4
  if [ -z "$VAR" -o -z "$COM" ]; then exit 1; fi
  unset VAL
  for i in $VAL $(which $COM $VAL) \
           /bin/$NAME /usr/bin/$NAME /usr/local/bin/$NAME
  do
    if [ -x "$i" ]; then VAL="$i"; break; fi; 
  done
  while [ "$ASK" != maybe -a ! -x "$VAL" ]
  do
    echo "--- Cannot determine $COM full name" 1>&2
    echo -n "$VAR"= 1>&2
    read $VAL
  done
  echo "$VAL"
  info "Using $VAR=$VAL" 1>&2
}

## Bash configuration

message 'Bash configuration'
BASH=$(findcommand BASH "$BASH" bash)

if [ -x /bin/usleep ]; then SLEEP="/bin/usleep 10"
elif [ -x /bin/sleep ]; then SLEEP="/bin/sleep 1"
else echo 'Command /bin/sleep does not exist'; exit 2; 
fi
info "Using SLEEP="'"'"$SLEEP"'"'


## INSTALL DIR

if [ -n "$LIBDIR" ]
then 
  if [ -d "$LIBDIR" ]; then :
  else echo "nonexistent LIBDIR=$LIBDIR"; fi
elif [ -d /usr/local/lib ]; then LIBDIR=/usr/local/lib/whizzytex
elif [ -d /usr/lib ]; then LIBDIR=/usr/lib/whizzytex
else
  echo '*** Cannot determine reasonable library directory'
  echo '    Give LIBDIR=path explicitly to ./configure'
fi

## Emacs, XEmacs 

message "Emacs or XEmacs configuration"

if test -z "$EMACS"; then EMACS=emacs; else BOTH=true; fi
if test -z "$XEMACS"; then XEMACS=xemacs; else BOTH=true; fi
Emacs=$EMACS
XEmacs=$XEMACS


unset EDITORS
if $EMACS -batch 2>/dev/null 1>/dev/null
  then EDITORS="$EDITORS $EMACS"
  else unset EMACS
fi
if $XEMACS -batch 2>/dev/null 1>/dev/null
  then EDITORS="$EDITORS $XEMACS"
  else unset XEMACS
fi

set xxx $EDITORS
shift


MANY=false
BYTECOMP=false
case $# in
  0)
    warning '
  Neither Emacs [defaulting to $Emacs] nor XEmacs [defaulting to $XEmacs] 
  seem to be installed. 

  If you wish to byte-compile emacs-lisp code, please use

    EMACS=<Emacs name> ./configure 
    XEMACS=<XEmacs name> ./configure 
    EMACS=<Emacs name> XEMACS=<XEmacs name> ./configure 

continuing...
'
  ;;
  1);;
  *) MANY=true;;
esac

if test -n "$USER"; then INIT="-u $USER"; fi
for EXEC in $EDITORS
do 
  case $($EXEC -batch -eval "(princ emacs-version)" 2>/dev/null) in
   *XEmacs*) X=X; DIR=$XEMACSDIR;;
   *) X=''; DIR=$EMACSDIR;;
  esac
  if $MANY
  then ELSEDIR="$LIBDIR/lisp $LIBDIR/$EXEC-lisp"
  else ELSEDIR=$LIBDIR/lisp
  fi
  if test -z "$DIR"
  then
    info "Found $EXEC installed."
    set xxx \
      `$EXEC -batch $INIT -l $(pwd)/configure.el -eval "(printall)" \
        2>/dev/null` \
       $ELSEDIR "none"
    shift
    case $# in
     0) 
        echo "Cannot determine ${X}EMACSDIR ---site-lisp directory for $EXEC "
        echo "Please enter full path name"
        echo -n "${X}EMACSDIR ==> "
        read DIR
        ;;
     1)
        DIR=$1
        ;;
     *)
        echo "--- Please, select one value for ${X}EMACSDIR (for $EXEC)

    none means do not install files for $EXEC"
        if $MANY; then echo "
    If you wish to install byte-compile code for both Emacs and XEmacs
    choose different locations for EMACSDIR and XEMACSDIR. 
    Otherwise, your may choose the same location."; echo; fi
        select DIR in "$@"
        do
          echo "You selected $DIR" 
          case /$DIR/ in
           //) ;;
           /none/) declare ${X}EMACS=''; unset DIR; break;;
           *) break ;;
          esac
        done
        ;;
    esac
  fi
  declare ${X}EMACSDIR=$DIR
  info "Using ${X}EMACSDIR=$DIR"
  echo
done

if test "/$EMACS/$XEMACS/$EMACSDIR/$XEMACSDIR/" = "/////"
then 
  echo "Using default library $LIBDIR/lisp for emacs-lisp"
  EMACSDIR='$(LIBDIR)/lisp'
fi

if test -n "$EMACS" -a -n "$XEMACS" 
then
  if test "$EMACSDIR" = "$XEMACSDIR"
  then
    warning "
  Will not byte-compile lisp for $EMACS and $XEMACS because both
  EMACSDIR and XEMACSDIR  are identical and equal to

    $EMACSDIR  

  Will only install source lisp code. 
  This is not a problem if this is what you meant.
  Otherwise, start again and choose different directories.

continuing...
"
    unset EMACS XEMACS XEMACSDIR
  else 
    if ! nory '--- Do you wish to byte-compile lisp code?'
    then unset EMACS; unset XEMACS; fi
  fi
fi

# Other directories
message "Configuring other directories"

if test -z "$BINDIR"; then BINDIR='$(LIBDIR)/bin'; fi
if test -z "$LATEXDIR"; then LATEXDIR='$(LIBDIR)/latex'; fi
if test -z "$DOCDIR"; then DOCDIR='$(LIBDIR)/doc'; fi

# initex
message "Checking initex and latex"

INITEX=$(findcommand INITEX "$INITEX" initex)
LATEX=$(findcommand LATEX "$LATEX" latex)
LATEXFMT=$(basename $LATEX)

if ! kpsewhich latex.fmt > /dev/null; then
  echo "--- Cannot find default format $LATEXFMT for $LATEX." 
  echo -n $LATEXFMT=
  read $LATEXFMT
  if [ -z "$LATEXFMT" ]; then 
     LATEXFMT=$LATEX
     info "Using default value LATEXFMT=$LATEXFMT"
  fi
fi


# viewers
message "Configuring viewers"

ADVI=$(findcommand ADVI "$ADVI" advi maybe)
XDVI=$(findcommand XDVI "XDVI" xdvi maybe)
GV=$(findcommand GV "$GV" gv maybe)
DVIPS=$(findcommand DVIPS "DVIPS" dvips maybe)

message "Building Makefile.config"

sed \
 -e "s%^\(BASH *=\).*%\1 $BASH%" \
 -e "s%^\(LIBDIR *=\).*%\1 $LIBDIR%" \
 -e "s%^\(EMACS *=\).*%\1 $EMACS%" \
 -e "s%^\(XEMACS *=\).*%\1 $XEMACS%" \
 -e "s%^\(EMACSDIR *=\).*%\1 $EMACSDIR%" \
 -e "s%^\(XEMACSDIR *=\).*%\1 $XEMACSDIR%" \
 -e "s%^\(BINDIR *=\).*%\1 $BINDIR%" \
 -e "s%^\(LATEXDIR *=\).*%\1 $LATEXDIR%" \
 -e "s%^\(DOCDIR *=\).*%\1 $DOCDIR%" \
 -e "s%^\(ADVI *=\).*%\1 $ADVI%" \
 -e "s%^\(XDVI *=\).*%\1 $XDVI%" \
 -e "s%^\(GV *=\).*%\1 $GV%" \
 -e "s%^\(DVIPS *=\).*%\1 $DVIPS%" \
 -e "s%^\(INITEX *=\).*%\1 $INITEX%" \
 -e "s%^\(LATEX *=\).*%\1 $LATEX%" \
 -e "s%^\(LATEXFMT *=\).*%\1 $LATEXFMT%" \
  < Makefile.config.in > Makefile.config

message Summary:
echo "
 BASH = $BASH

 LIBDIR = $LIBDIR
 EMACS = $EMACS
 EMACSDIR = $EMACSDIR
 XEMACS = $XEMACS
 XEMACSDIR = $XEMACSDIR
 BINDIR = $BINDIR
 LATEXDIR = $LATEXDIR
 DOCDIR = $DOCDIR
 
 ADVI = $ADVI
 XDVI = $XDVI
 GV = $GV
 DVIPS = $DVIPS
 
 INITEX = $INITEX
 LATEX = $LATEX
 LATEXFMT = $LATEXFMT
"
if $ERRORS
then echo "
   ************ They were serious Errors ************

   Makefile.config was still build but is probably incorrect.
   You should check you configuration and restart config.
   You may also edit Makefile.config if you know what you are doing.

"
else echo "
Configuration suceeded
See Makefile.config for details
"
fi
