#!/bin/sh
#
# Guess values for system-dependant variables and create `Makefile'.
# Can be distributed according to GPL.
#
# Usage:
#   configure -ask		(ask all questions)
#
# As soon as some other option is specified, configure
# will not ask questions. Other options:
#
#   configure -d[efault]	(language=en, +fhs)
#   configure -confdir DIR	(directory to hold man.conf)
#   configure +suid/sgid	(make man suid or sgid)
#   configure +lang cs,da,de,el,en,es,fi,fr,hr,it,ko,nl,pl,pt,ro,sl
#                               (language(s) as given)
#				("all" will specify all available languages)
#				("none" will disable NLS support)
#   configure +fhs		(follow FHS: use /var/cache/man)
#   configure +fsstnd		(follow FSSTND: use /var/catman)
#
LANGUAGES="bg,cs,da,de,el,en,es,fi,fr,hr,it,ja,ko,nl,pl,pt,ro,ru,sl"
LANGOPT="+lang {$LANGUAGES|all|none}"
#
# Some people prefer "-irs", but probably only when they have a broken setup.
# (Indeed, -r may cause the terminal to get into funny states.
# Very inconvenient. For viewing pages in strange locales, set LC_*.)
#
DEFAULTLESSOPT="-is"
#
# Note that not creating any cat directories (/var/cache/man or so)
# and not making man suid or sgid is recommended.
# This avoids security problems, the fact that different users have
# different window sizes and different character sets, etc.
# Formatting is fast enough today.

trap 'rm -f conftest conftest.c; exit 1' 1 3 15

set +u # Make sure unset variables are ok.

if [ $# = 0 ]; then
  echo "Usage: configure -ask"
  echo "   or: configure [-d|-default] [-confdir DIR] [+suid] [+sgid] \\"
  echo "                 [+fhs|+fsstnd|+traditional] [+lang none|all|LANGUAGES]"
  echo "   where LANGUAGES is a substring of $LANGUAGES".
  echo
  echo 'The default "configure -d" is equivalent to "configure +fhs +lang en".'
  echo 'The option +suid will make man suid to a user "man". Create this first.'
  echo 'The option +sgid will make man sgid to a group "man". Create this first.'
  echo 'The options +fhs, +fsstnd, +traditional will make man use'
  echo ' /var/cache/man, /var/catman and /usr/man/cat?, respectively.'
  echo 'It is recommended not to use suid/sgid and not to keep cat pages.'
  echo 'The -confdir option specifies where man.conf lives (default /usr/share/misc).'
  echo
  exit 1
fi

default=true
usenls=true

for arg in $*; do
  if [ x$setlang = xtrue ]
  then
    case $arg in
      none)
	usenls=false
	;;
      all)
	languages="??"
	;;
      *)
	languages=`echo $arg | sed -e 's/,/ /g'`
    esac
    setlang=false
  elif [ x$setconfdir = xtrue ]
  then
    confdir=$arg
    confexplicit=1
    setconfdir=false
  else
    # help people used to GNU-style --options
    # e.g. --enable-suid, --disable-suid, --prefix
    case $arg in
      --enable-*)
          arg=`echo $arg | sed -e 's/--enable-/+/'`
	  ;;
      --disable-*)
          arg=`echo $arg | sed -e 's/--disable-/-/'`
	  ;;
      --*)
          arg=`echo $arg | sed -e 's/--/-/'`
    esac
    case $arg in
      -default | -d)
	  usefhs=true
	  ;;
      -ask | -a)
	  default=
	  ;;
      -prefix=*)
	  prefix=`echo $arg | sed -e 's/-prefix=//'`
	  ;;
      -bindir=*)
          bindir=`echo $arg | sed -e 's/-bindir=//'`
	  ;;
      -confdir=*)
          confdir=`echo $arg | sed -e 's/-confdir=//'`
	  confexplicit=1
	  ;;
      -confdir)
          setconfdir=true;
          ;;
      -confprefix=*)
          confprefix=`echo $arg | sed -e 's/-confprefix=//'`
	  ;;
      -mandir=*)
          mandir=`echo $arg | sed -e 's/-mandir=//'`
	  manexplicit=1
	  ;;
      -sbindir=*)
          sbindir=`echo $arg | sed -e 's/-sbindir=//'`
	  ;;
      +lang)
	  setlang=true
	  ;;
      +suid)
	  suid=true
	  ;;
      -suid)
	  suid=false
	  ;;
      +sgid)
	  sgid=true
	  ;;
      -sgid)
	  sgid=false
	  ;;
      +traditional)
          usefsstnd=false
          usefhs=false
          ;;
      +fsstnd)
	  usefsstnd=true
	  ;;
      -fsstnd)
	  usefsstnd=false
          ;;
      +fhs)
	  usefhs=true
	  ;;
      -fhs)
          usefhs=false
          ;;
      -compatibility_mode_for_colored_groff)
          set_compatibility_mode_for_colored_groff=true;
	  ;;
      *)
  echo "Usage: configure -ask"
  echo "   or: configure [-d|-default] [-confdir DIR] [+suid] [+sgid] \\"
  echo "                 [+fhs|+fsstnd|+traditional] [+lang all|LANGUAGES]"
  echo "       where LANGUAGES is a substring of $LANGUAGES".
  echo
  echo 'The default "configure -d" is equivalent to "configure +fhs +lang en".'
  echo 'The option +suid will make man suid to a user "man". Create this first.'
  echo 'The option +sgid will make man sgid to a group "man". Create this first.'
  echo 'The options +fhs, +fsstnd, +traditional will make man use'
  echo ' /var/cache/man, /var/catman and /usr/man/cat?, respectively.'
  echo 'The -confdir option specifies where man.conf lives (default /usr/share/misc).'
  exit 1
	  ;;
    esac
  fi
done

# Default installation paths
#
# prefix="/usr/local" or "/usr"
# confprefix="/usr/local" or "/usr" or "/etc"
# 
# The configuration file has various names and pathnames in the
# various distributions, such as /usr/share/misc/man.conf,
# /usr/lib/man.conf, /etc/man.config.
# The FHS wants it in /usr/share/misc
# All documentation assumes "man.conf" - in case you invent some other name
# (why?), also man pages and other docs need to be patched.
if [ -z "${prefix}" ]; then prefix="/usr"; fi
if [ -z "${confprefix}" ]; then confprefix=${prefix}; fi
if [ -z "${bindir}" ]; then bindir="${prefix}/bin"; fi
if [ -z "${sbindir}" ]; then sbindir="${prefix}/sbin"; fi
if [ -z "${mandir}" ]; then mandir="${prefix}/man"; fi

if [ -z "$manexplicit" -a x"$usefhs" = xtrue ]; then
  mandir="${prefix}/share/man"
fi
if [ -z "$confexplicit" ]; then
  confdir="${confprefix}/lib"
  if [ x"$usefhs" = xtrue ]; then
    confdir="${confprefix}/share/misc"
  fi
fi
conffilename="man.conf"

if test "$RANDOM" = "$RANDOM"; then
  # Plain old Bourne shell.
  echo checking for gcc
  test -z "$CC" -a -n "`gcc 2>&1`" && CC="gcc -O"
else
  # ksh, bash or zsh.  ksh and zsh write "command not found" to stderr.
  echo checking for gcc
  test -z "$CC" && type gcc && CC="gcc -O"
fi

CC=${CC-cc}
BUILD_CC=${BUILD_CC-${CC}}
INSTALL=${INSTALL-install}
INCLUDEDIR=${INCLUDEDIR-/usr/include}

rm -f conftest conftest.c
compile="$CC $DEFS conftest.c -o conftest $LIBS >/dev/null 2>&1"

# Check for various header files.

echo checking for ANSI C header files
echo "#include <stdlib.h>
#include <string.h>
main() { exit(0); strerror(0); }" > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then
  DEFS="$DEFS -DSTDC_HEADERS"
fi
rm -f conftest conftest.c

echo checking for POSIX.1 header files
echo "#include <unistd.h>
main() {
#ifdef _POSIX_VERSION
exit(0);
#else
exit(1);
#endif
}" > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then
  DEFS="$DEFS -DPOSIX"
fi
rm -f conftest conftest.c

echo checking for BSD string and memory functions
echo "#include <strings.h>
main() { exit(0); rindex(0, 0); bzero(0, 0); }" > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then :
  else DEFS="$DEFS -DUSG"
fi
rm -f conftest conftest.c

echo checking whether sys/types.h defines uid_t
echo '#include <sys/types.h>
main() { uid_t x; exit(0); }' > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then :
else
  uid_t=`awk '/pw_uid;/ {print $1}' $INCLUDEDIR/pwd.h`
  DEFS="$DEFS -Duid_t=${uid_t} -Dgid_t=${uid_t}"
fi
rm -f conftest conftest.c

echo checking for Xenix
if test -f /xenix; then
  LIBS="$LIBS -lx"
  case "$DEFS" in
  *SYSNDIR*) ;;
  *) LIBS="-ldir $LIBS" ;; # Make sure -ldir precedes any -lx.
  esac
fi

echo checking how to get alloca
echo '
#ifdef __GNUC__
#define alloca __builtin_alloca
#else
#ifdef sparc
#include <alloca.h>
#else
#ifdef _AIX
 #pragma alloca
#else
char *alloca ();
#endif
#endif
#endif
main() { char *p = (char *) alloca(1); exit(0); }' > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then :
elif test -d /usr/ucblib; then LIBS="$LIBS -L/usr/ucblib -lucb"
elif test -f /usr/lib/libPW.a; then LIBS="$LIBS -lPW"
else DEFS="$DEFS -DALLOCA_MISSING"
fi
rm -f conftest conftest.c

if [ $usenls = true ]; then
  echo checking for nls
  echo '#include <nl_types.h>
  main() {nl_catd catfd; exit(0); }' > conftest.c
  eval $compile
  if test -s conftest && ./conftest 2>/dev/null; then :
  else
    usenls=false
  fi
  rm -f conftest conftest.c
fi

if [ $usenls = false ]; then
  DEFS="$DEFS -DNONLS"
fi

echo checking for getopt.h
echo '#define _GNU_SOURCE
#include <getopt.h>
#include <stdio.h>
struct option long_opts[] = { { "", no_argument, NULL, 0 } };
main() { exit(0); }' > conftest.c
eval $compile
if test -s conftest && ./conftest 2>/dev/null; then
  manpathoption="--path"
else
  manpathoption="-w"
  DEFS="$DEFS -DNOGETOPT"
fi
rm -f conftest conftest.c

# first determine how to suppress newline on echo command (stolen from
# Perl's Configure) ...

echo "Checking echo to see how to suppress newlines..."
(echo "hi there\c" ; echo " ") >conftest
if grep c conftest >/dev/null 2>&1 ; then
    echo "...using -n."
    n='-n'
    c=''
else
    cat <<'EOM'
...using \c
EOM
    n=''
    c='\c'
fi
rm -f conftest

# Ask the installer where various things are located.
# (A separate variable is needed since various shells do
# word-splitting at different moments, which means that
#   for i in /bin:/usr/bin:$PATH
# does not work everywhere.)
# Some people might prefer having $PATH first in PREPATH
PREPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:$PATH
IFS0="$IFS"
IFS=":$IFS"
for i in $PREPATH
do
  case $i in
    /*)
      DEFPATH="$DEFPATH $i"
      ;;
  esac
done
IFS="$IFS0"

if [ x$default = x ]; then
  echo ""
  echo $n "Do you have the nroff source for man pages?  [yes] $c"
  ans=
  while [ x$ans = x ]
  do
    read yesno
    if test "$yesno" = "" || test "$yesno" = "yes"
    then
      ans=true
    elif test "$yesno" = "no"
    then
      ans=false
    else
      echo "You must enter yes or no"
    fi
  done
else
  echo ""
  echo "Assuming you want to be able to handle the nroff source for man pages."
  ans=true
fi

if test "$ans" = "false"
then
  for i in more less cmp cat awk gawk mawk
  do
    eval F$i="missing"
    for j in $DEFPATH
    do
      if test -f $j/$i
      then
	eval F$i=$j/$i
	break
      fi
    done
  done
  troff=""
  nroff=""
  jnroff=""
  eqn=""
  neqn=""
  jneqn=""
  tbl=""
  col=""
  vgrind=""
  refer=""
  grap=""
  pic=""
  if test $Fless = "missing"
  then
    pager="$Fmore"
  else
    pager="$Fless $DEFAULTLESSOPT"
  fi
  if test $Fawk = "missing"
  then
    Fawk="$Fgawk"
  fi
  if test $Fawk = "missing"
  then
    Fawk="$Fmawk"
  fi
  cmp="$Fcmp -s"
  cat="$Fcat"
  awk="$Fawk"

  FILTERS="pager cmp cat awk"
else
  for i in nroff groff geqn gtbl col vgrind grefer grap gpic more less lynx cmp cat awk gawk mawk
  do
    eval F$i="missing"
    for j in $DEFPATH
    do
      if test -f $j/$i
      then
	eval F$i=$j/$i
	break
      fi
    done
  done
  for i in eqn tbl refer pic
  do
    if test `eval echo \\$Fg$i` = "missing"
    then
      for j in $DEFPATH
      do
        if test -f $j/$i
        then
	  eval Fg$i=$j/$i
	  break
        fi
      done
    fi
  done
# -Tlatin1 is bad when utf8 is used, but needed for groff, not for nroff
# Hmm - also needed for nroff, to prevent double conversion on uxterm
  if test $Fgroff = "missing"
  then
    if test $Fnroff = "missing"
    then
      nroff="nroff -Tlatin1 -mandoc"
    else
      nroff="$Fnroff -Tlatin1 -mandoc"
    fi
    troff="troff -mandoc"
    echo "Warning: could not find groff"
  else
    nroff="$Fgroff -Wall -mtty-char -Tascii -mandoc"
    troff="$Fgroff -Tps -mandoc"
    jnroff="$Fgroff -Tnippon -mandocj"
  fi
  eqn="$Fgeqn -Tps"
  neqn="$Fgeqn -Tascii"
  jneqn="$Fgeqn -Tnippon"
  tbl="$Fgtbl"
  col="$Fcol"
  vgrind="$Fvgrind"
  refer="$Fgrefer"
  grap="$Fgrap"
  pic="$Fgpic"
  if test $Fless = "missing"
  then
    pager="$Fmore"
  else
    pager="$Fless $DEFAULTLESSOPT"
  fi
  if test $Flynx = "missing"
  then
    browser=$pager
    htmlpager=$Fcat
  else
    browser="$Flynx"
    htmlpager="$Flynx -dump"
  fi
  if test $Fawk = "missing"
  then
    Fawk="$Fgawk"
  fi
  if test $Fawk = "missing"
  then
    Fawk="$Fmawk"
  fi
  cmp="$Fcmp -s"
  cat="$Fcat"
  awk="$Fawk"

  FILTERS="troff nroff jnroff eqn neqn jneqn tbl col vgrind refer grap pic pager browser htmlpager cmp cat awk"
fi

# Note: older nroff gives an error message for -c
# Do this only when you have troff 1.18.1 or so.
if [ "x$set_compatibility_mode_for_colored_groff" = "xtrue" ]; then
    troff="$troff -c"
    nroff="$nroff -c"
    jnroff="$jnroff -c"
fi

if [ x$default = x ]; then
  echo ""
  echo "If you don't have one of the following programs, enter \`missing'."
  echo "It's best to enter absolute pathnames so that man won't have to"
  echo "depend on the user's path to work properly.  Default values are"
  echo "given in square brackets.  The answers given are only checked to"
  echo "see if the programs exist, not to see see if the given flags are"
  echo "correct."
  
  for filter in $FILTERS
  do
    found=false
    while test "$found" = "false"
    do
      echo ""
      echo $n "$filter command to use [`eval echo \\$$filter`] $c"
      read tmp
      if test -n "$tmp"
      then
        if test "$tmp" = "missing"
        then
          if test "$filter" = "pager"
          then
            found=false
            echo "You must have a pager!"
          else
            found=true
            eval $filter=""
          fi
        elif test -f `echo $tmp | awk '{print $1}'`
        then
          found=true
          eval $filter=\""$tmp"\"
        else
          found=false
          echo ""
          echo "`echo $tmp | awk '{print $1}'` doesn't seem to exist!  Try again or enter \`missing'"
        fi
      elif test `eval echo \\$$filter | awk '{print $1}'` = "missing"
      then
        found=true
        eval $filter=""
      elif test -f `eval echo \\$$filter | awk '{print $1}'`
      then
        found=true
      else
        found=false
        echo ""
        echo "`eval echo \\$$filter | awk '{print $1}'` doesn't seem to exist!  Try again or enter \`missing'"
      fi
    done
  done
else
  echo ""
  echo "Assuming the following ways to invoke the various commands and filters":
  for filter in $FILTERS
  do
    echo "$filter command:	`eval echo \\$$filter`"
    if test `eval echo \\$$filter | awk '{print $1}'` = "missing"
    then
      eval $filter=""
    fi
  done
fi

case $nroff in
  *roff*)
    echo ""
    echo "It seems that no col is needed, since you use groff."
    pcol=
    nocol="# " ;;
  *)
    pcol=$col
    nocol= ;;
esac
  
fhs="# "
fsstnd="# "
FHSDIR=/var/cache/man
FSSTNDDIR=/var/catman
if [ x$usefhs = x -a x$usefsstnd = x ]; then
  if [ x$default = x ]; then
    echo ""
    echo $n "Do you want to put the cat pages under $FHSDIR ? [no] $c"
    ans=
    while [ x$ans = x ]
    do
      read yesno
      if test "$yesno" = "" || test "$yesno" = "no"
      then
        ans=false
      elif test "$yesno" = "yes"
      then
        ans=true
      else
        echo "You must enter yes or no"
      fi
    done
    if [ $ans = true ]; then
      fhs=
    else
      echo $n "Do you want to put the cat pages under $FSSTNDDIR ? [no] $c"
      ans=
      while [ x$ans = x ]
      do
        read yesno
        if test "$yesno" = "" || test "$yesno" = "no"
        then
          ans=false
        elif test "$yesno" = "yes"
        then
          ans=true
        else
          echo "You must enter yes or no"
        fi
      done
      if [ $ans = true ]; then
        fsstnd=
      fi
    fi
  else
    echo ""
    if [ -d $FHSDIR ]; then
      echo "Using $FHSDIR for cat pages, following FHS."
      fhs=
    elif [ -d $FSSTNDDIR ]; then
      echo "Using $FSSTNDDIR for cat pages, following FSSTND."
      fsstnd=
    else
      echo "Using /usr/man/cat* (if that exists) for cat pages."
    fi
  fi
else
  if [ x$usefsstnd = xtrue ]; then
    fsstnd=
  elif [ x$usefhs = xtrue ]; then
    fhs=
  fi
fi

# How should man be installed?

if [ x$default = x ]; then
  for d in bindir sbindir mandir confdir
  do
    case $d in
      bindir)
        echo ""
        echo $n "Where should executables be installed?  [${bindir}] $c" ;;
      sbindir)
        echo ""
        echo $n "Where should makewhatis be installed?  [${sbindir}] $c" ;;
      confdir)
        echo ""
        echo $n "Where should $conffilename be installed?  [${confdir}] $c" ;;
      mandir)
        echo ""
        echo $n "Where should man pages be installed? [${mandir}] $c" ;;
    esac
    dd=`eval echo \\$$d`
    dir=$prefix/$dd
    ok=false
    while test "$ok" = "false"
    do
      read tmpdir
      if test "$tmpdir" = "" && test "$dir" != ""
      then
        if test -d $dir
        then
          ok=true
          eval $d=$dir
        else
          echo ""
          echo "$dir doesn't seem to exist!  Try again!"
        fi
      elif test -d "$tmpdir"
      then
        ok=true
        eval $d=$tmpdir
        newprefix=`echo $tmpdir | sed -e "s:/$dd::"`
        if test "$tmpdir" = "$newprefix/$dd"
        then
          prefix=$newprefix
        fi
      else
        ok=false
        echo ""
        echo "$tmpdir doesn't seem to exist!  Try again!"
      fi
    done
  done

  tmpext=1
  echo ""
  echo $n "What extension should installed man(1) pages have? [${tmpext}] $c"
  read man1ext
  if test "$man1ext" = ""; then man1ext=$tmpext; fi

  tmpext=5
  echo ""
  echo $n "What extension should installed man(5) pages have? [${tmpext}] $c"
  read man5ext
  if test "$man5ext" = ""; then man5ext=$tmpext; fi

  tmpext=8
  echo ""
  echo $n "What extension should installed man(8) pages have? [${tmpext}] $c"
  read man8ext
  if test "$man8ext" = ""; then man8ext=$tmpext; fi
else
  echo ""
  echo "Assuming that binaries go to $bindir, the configuration file"
  echo "goes to $confdir, makewhatis to $sbindir, and the man pages"
  echo "to $mandir/man*."
  man1ext=1
  man5ext=5
  man8ext=8
fi

if [ x$default = x ]; then
  echo ""
  echo $n "Do you want to install non-english man pages? [no] $c"
  ok=false
  while test "$ok" = "false"
  do
    read yesno
    if test "$yesno" = "" || test "$yesno" = "no"
    then
      ok=true
      ans=false
    elif test "$yesno" = "yes"
    then
      ok=true
      ans=true
    else
      ok=false
      echo "You must enter yes or no"
    fi
  done
else
  if [ "x$languages" = x ]; then
    echo ""
    echo "Assuming that you do not want to install non-english man pages."
  fi
  ans=false
fi

if test "$ans" = "true"
then
  cd man
  languages=""
  for i in ??; do
    langname=`cat $i.txt`
    echo $n "Install $langname pages? [no] $c"
    ok=false
    while test "$ok" = "false"
    do
      read yesno
      if test "$yesno" = "" || test "$yesno" = "no"
      then
        ok=true
        ans=false
      elif test "$yesno" = "yes"
      then
        ok=true
        ans=true
      else
        ok=false
        echo "You must enter yes or no"
      fi
    done
    if test "$ans" = "true"
    then
      languages="$languages $i"
    fi
  done
  cd ..
  if [ "x$languages" = x ]; then echo "Not installing any man pages, then."; fi
else
  if [ "x$languages" = x ]
  then
    languages="en"
  fi
fi

if [ "x$languages" != x -a "x$languages" != xen ]
then
  DEF_NLSPATH="/usr/share/locale/%L/%N:/usr/lib/locale/%N/%L"
  deflocalepath=`echo $NLSPATH:$DEF_NLSPATH | sed 's/:/ /g'`
  for i in $deflocalepath; do
    if [ x$deflocale = x ]; then
      tstdir=`echo $i | sed -e 's/%N//; s/%L//'`
      if [ -d "$tstdir" ]; then
	deflocale=$i
#      else
#	tstdir=`echo $i | sed -e 's/%N/*/; s/%L/*/'`
#	if [ -d "$tstdir" ]; then
#	  deflocale=$i
#	fi
      fi
    fi
  done
  if [ x$deflocale = x ]; then
    deflocale="/usr/lib/locale/%N/%L"
  fi
  if [ x$default = x ]; then
    echo ""
    echo $n "Do you want to install non-english message catalogs for man? [yes] $c"
    ok=false
    while test "$ok" = "false"
    do
      read yesno
      if test "$yesno" = "" || test "$yesno" = "yes"
      then
        ok=true
        ans=true
      elif test "$yesno" = "no"
      then
        ok=true
        ans=false
      else
        ok=false
        echo "You must enter yes or no"
      fi
    done
    if [ $ans = true ]; then
      echo $n "Where? [$deflocale] $c"
      read locale
      if [ x$locale = x ]; then
        locale=$deflocale
      fi
    else
      locale=
    fi
  else
    locale=$deflocale
  fi
  echo
  echo "Using localedir $locale"
fi

if [ x$suid = x -a x$sgid = x ]; then
  if [ x$default = x ]; then
    echo ""
    echo "Do you want to install man setuid or setgid to some user"
    echo $n "who owns all the formatted man pages?  [no] $c"
    ok=false
    while test "$ok" = "false"
    do
      read yesno
      if test "$yesno" = "" || test "$yesno" = "no"
      then
        ok=true
        ans=false
      elif test "$yesno" = "yes"
      then
        ok=true
        ans=true
      else
        ok=false
        echo "You must enter yes or no"
      fi
    done
  else
    echo "Assuming that you do not want man to be suid or sgid."
    echo "(But you can always do _chown man man; chmod 04555 man_ later.)"
    ans=false
  fi
else
  ans=true
fi

if test "$ans" = "true"
then
  # prefer uid/gid man above root, but prefer not to give man the
  # ability to write into its own binary; never install man suid root

  if [ x$sgid = x ]; then
    man_user=man
    man_mode=4555
    if grep -q '^man:' /etc/group >/dev/null 2>/dev/null;
    then
      man_group=man
    else
      man_group=root
    fi
    if grep -q '^man:' /etc/passwd >/dev/null 2>/dev/null;
    then
      :
    else
      echo "Warning: you still have to create a user \"man\""
    fi
  else
    man_user=root
    man_group=man
    man_mode=2555
    if grep -q '^man:' /etc/group >/dev/null 2>/dev/null;
    then
      :
    else
      echo "Warning: you still have to create a group \"man\""
    fi
  fi
  if [ x$default = x ]; then
    echo ""
    echo $n "What owner should man have? [${man_user}] $c"
    read user
    if test "$user" != ""; then man_user=$user; fi
    echo ""
    echo $n "What group should man have? [${man_group}] $c"
    read group
    if test "$group" != ""; then man_group=$group; fi
    echo ""
    echo $n "What mode should the installed man have? [${man_mode}] $c"
    read mode
    if test "$mode" != ""; then man_mode=$mode; fi
  fi
  man_install_flags="-m $man_mode -o $man_user -g $man_group"
fi

# What sections do we anticipate?

tmpsections="1 1p 8 2 3 3p 4 5 6 7 9 0p tcl n l p o"

if [ x$default = x ]; then
  echo ""
  echo "Enter the man page sections your system uses, separated by"
  echo "spaces, and in the order they should be searched."
  echo $n "[${tmpsections}] $c"
  read sects
  if test "$sects" != ""; then tmpsections="$sects"; fi
else
  echo
  echo "Setting the default section search order to $tmpsections."
fi

for sect in $tmpsections
do
  if [ x$sections = x ]; then
    sections=$sect
  else
    sections=$sections:$sect
  fi
done

if test "$nroff" != ""
then
  if [ x$default = x ]; then
    echo ""
    echo $n "Compress formatted pages? [yes] $c"
    ok=false
    while test "$ok" = "false"
    do
      read yesno
      if test "$yesno" = "" || test "$yesno" = "yes"
      then
        ok=true
        ans=true
      elif test "$yesno" = "no"
      then
        ok=true
        ans=false
      else
        ok=false
        echo "You must enter yes or no"
      fi
    done
  else
    echo ""
    echo "Assuming that formatted pages should be compressed."
    ans=true
  fi

  if test "$ans" = "true"
  then
    DO_COMPRESSION=true
    compress=
    for i in bzip2 gzip bzip tzip pack compress freeze yabba
    do
      eval F$i=missing
      for j in $DEFPATH
      do
        if test -f $j/$i
        then
	  eval F$i=$j/$i
	  if [ x$compress = x ]; then compress=$j/$i; fi
	  break
        fi
      done
    done

    if [ x$default = x ]; then
      echo ""
      echo "What program should be used to compress the formatted pages?"
      echo $n "Note that it must work as a filter! [${compress}] $c"
      found=false
      while test "$found" = "false"
      do
        read tmp
        if test -n "$tmp"
        then
          if test -f `echo $tmp | awk '{print $1}'`
          then
            found=true
            compress=$tmp
          else
            found=false
            echo ""
            echo "`echo $tmp | awk '{print $1}'` doesn't seem to exist!  Try again!"
          fi
        elif test -f `echo "$compress" | awk '{print $1}'`
        then
          found=true
        else
          found=false
          echo ""
          echo "`echo "$compress" | awk '{print $1}'` doesn't seem to exist!  Try again!"
        fi
      done
    else
      if [ x$compress = x ]
      then
	DO_COMPRESSION=false
	echo "Could not find any compression programs."
      else
        echo "Using $compress for compression."
      fi
    fi

    case $compress in
      *bzip2*) ext=".bz2" ;;
      *gzip*) ext=".gz" ;;
      *bzip*) ext=".bz" ;;
      *tzip*) ext=".tz" ;;
      *pack*) ext=".z" ;;
      *compress*) ext=".Z" ;;
      *freeze*) ext=".F" ;;
      *yabba*) ext=".Y" ;;
      *) ext="" ;;
    esac
    if [ x$default = x ]; then
      echo ""
      echo "What extension should be added to"
      if test "$ext" = ""
      then
        echo "compressed files?"
      else
        echo $n "compressed files?  [${ext}] $c"
      fi
      ok=false
      while test "$ok" = "false"
      do
        read tmp_ext
        if test "$tmp_ext" = "" && test "$ext" != ""
        then
          ok=true
          compress_ext=$ext
        elif test "$tmp_ext" != ""
        then
          ok=true
          compress_ext=$tmp_ext
        fi
      done
    else
      compress_ext=$ext
    fi
  else
    DO_COMPRESSION=false
  fi
fi

# unconditionally handle uncompression
UNCOMPRESSORS="gunzip bzip2 pcat zcat fcat unyabba"
for i in $UNCOMPRESSORS
do
  eval F$i=missing
  for j in $DEFPATH
  do
    if test -f $j/$i
    then
      eval F$i=$j/$i
      break
    fi
  done
done
gunzip=missing
if [ $Fgunzip != missing ]; then
  gunzip="$Fgunzip -c"
fi
bzip2=missing
if [ $Fbzip2 != missing ]; then
  bzip2="$Fbzip2 -c -d"
fi
pcat="$Fpcat"
zcat="$Fzcat"
fcat="$Ffcat"
unyabba="$Funyabba"

if [ x$default = x ]; then
  echo ""
  echo "Now let us look at programs for uncompressing compressed man pages."
  echo ""
  echo "If you don't have one of the following programs, enter \`missing'."
  echo "It's best to enter absolute pathnames so that man won't have to"
  echo "depend on the user's path to work properly.  Default values are"
  echo "given in square brackets.  The answers given are only checked to"
  echo "see if the programs exist, not to see see if the given flags are"
  echo "correct."
  for filter in $UNCOMPRESSORS
  do
    found=false
    while test "$found" = "false"
    do
      echo ""
      case $filter in
	gunzip)
	  echo "Command to use for .gz files (standard gzip)"
	  echo $n "[`eval echo \\$$filter`] $c" ;;
	bzip2)
	  echo "Command to use for .bz2 files (standard bzip2)"
	  echo $n "[`eval echo \\$$filter`] $c" ;;
        pcat)
          echo "Command to use for .z files (pack/unpack)"
          echo $n "[`eval echo \\$$filter`] $c" ;;
        zcat)
          echo "Command to use for .Z files (standard compress)"
          echo $n "[`eval echo \\$$filter`] $c" ;;
        fcat)
          echo "Command to use for .F files (freeze/melt from net posting)"
          echo $n "[`eval echo \\$$filter`] $c" ;;
        unyabba)
          echo "Command to use for .Y files (yabba/unyabba from net posting)"
          echo $n "[`eval echo \\$$filter`] $c" ;;
        esac
      read tmp
      if test -n "$tmp"
      then
        if test "$tmp" = "missing"
        then
          found=true
          eval $filter=""
        elif test -f `echo $tmp | awk '{print $1}'`
        then
          found=true
          eval $filter=\""$tmp"\"
        else
          found=false
          echo ""
          echo "`echo $tmp | awk '{print $1}'` doesn't seem to exist!  Try again or enter \`missing'"
        fi
      elif test `eval echo \\$$filter | awk '{print $1}'` = "missing"
      then
        found=true
        eval $filter=""
      elif test -f `eval echo \\$$filter | awk '{print $1}'`
      then
        found=true
      else
        found=false
        echo ""
        echo "`eval echo \\$$filter | awk '{print $1}'` doesn't seem to exist!  Try again or enter \`missing'"
      fi
    done
  done
else
  echo ""
  echo "Assuming the following ways to invoke the various decompressors:"
  for filter in $UNCOMPRESSORS
  do
    echo "$filter command:   	`eval echo \\$$filter`"
    if test `eval echo \\$$filter | awk '{print $1}'` = "missing"
    then
      eval $filter=""
    fi
  done
fi

# set $decompress to the program that decompresses things
# with the $compress_ext extension.
case $compress_ext in
  .gz) decompress=$gunzip ;;
  .bz2) decompress=$bzip2 ;;
  .z) decompress=$pcat ;;
  .Z) decompress=$zcat ;;
  .F) decompress=$fcat ;;
  .Y) decompress=$unyabba ;;
  *) decompress= ;;
esac

if test "$DO_COMPRESSION" = "true"
then
  DEFS="$DEFS -DDO_COMPRESS"
fi

man=$bindir/man
apropos=$bindir/apropos
whatis=$bindir/whatis
man2dvi=$bindir/man2dvi
makewhatis=$sbindir/makewhatis
man_config_dir=$confdir
man_config_file=$confdir/$conffilename

if [ x$locale = x ]; then
  sed -e 's/%.*//' < Makefile.in > Makefile
else
  sed -e 's/%//' < Makefile.in > Makefile
fi

# some definitions to avoid extensive quoting in the script below
allargs='$@'
infiles='$infiles'
infile='$infile'
cb='$cb'
cs='$cs'
ce='$ce'
mancomment='.\\\"'

# Note: the script below only works when none of the variables
# contains a comma.
version=`cat version`

cat > conf_script << EOS
#!/bin/sh
infiles=$allargs
trap 'rm -f $infiles; exit 1' 1 3 15

# echo ""
for infile in $infiles
do
  case $infile in
  *.h)
    cb="/*"; cs=" *"; ce=" */";;
  *.1|*.5|*.8|*.man)
    cb=$mancomment; cs=$mancomment; ce=$mancomment;;
  *)
    cb="#"; cs="#"; ce="#";;
  esac
  echo "$cb"                                                    > $infile
  echo "$cs Generated automatically from $infile.in by the"     >> $infile
  echo "$cs configure script."                                  >> $infile
  echo "$ce"                                                    >> $infile
  echo "Creating $infile from $infile.in"
  sed -e '
s,@version@,$version,
s,@CC@,$CC,
s,@BUILD_CC@,$BUILD_CC,
s,@INSTALL@,$INSTALL,
s,@DEFS@,$DEFS,
s,@LIBS@,$LIBS,
s,@LIBOBJS@,$LIBOBJS,
s,@troff@,$troff,
s,@nroff@,$nroff,
s,@jnroff@,$jnroff,
s,@eqn@,$eqn,
s,@neqn@,$neqn,
s,@jneqn@,$jneqn,
s,@tbl@,$tbl,
s,@nocol@,$nocol,
s,@pcol@,$pcol,
s,@col@,$col,
s,@vgrind@,$vgrind,
s,@refer@,$refer,
s,@grap@,$grap,
s,@pic@,$pic,
s,@fcat@,$fcat,
s,@pcat@,$pcat,
s,@zcat@,$zcat,
s,@gunzip@,$gunzip,
s,@bzip2@,$bzip2,
s,@unyabba@,$unyabba,
s,@compress@,$compress,
s,@compress_ext@,$compress_ext,
s,@decompress@,$decompress,
s,@pager@,$pager,
s,@browser@,$browser,
s,@htmlpager@,$htmlpager,
s,@cmp@,$cmp,
s,@cat@,$cat,
s,@awk@,$awk,
s,@bindir@,$bindir,
s,@sbindir@,$sbindir,
s,@mandir@,$mandir,
s,@locale@,$locale,
s,@fhs@,$fhs,
s,@fsstnd@,$fsstnd,
s,@man1ext@,$man1ext,g
s,@man5ext@,$man5ext,g
s,@man8ext@,$man8ext,g
s,@man_install_flags@,$man_install_flags,
s,@man_user@,$man_user,
s,@languages@,$languages,
s,@man@,$man,
s,@apropos@,$apropos,
s,@whatis@,$whatis,
s,@man2dvi@,$man2dvi,
s,@makewhatis@,$makewhatis,
s,@man_config_dir@,$man_config_dir,
s,@man_config_file@,$man_config_file,
s,@manpathoption@,$manpathoption,
s/@sections@/$sections/
' $infile.in >> $infile
done
EOS

chmod +x conf_script

echo ""
echo 'Created Makefile and conf_script. Now do "make" and "make install".'
echo ""
