#!/bin/sh
# $Id: Configure,v 1.2 1997/07/06 14:34:49 eilts Exp eilts $
#
#set -x
#
# some parts are from Perl's Configure
#
# List of files to serach for, pathes will be placed in $<name> if found
loclist="gcc cc ls tr sed fgrep rz install chown chgrp chmod cp mkdir rm ctags find xargs"
#
# List of routine:headerfilelist to search for, routine=y if found, if not,
# sedcmd will be prolonged by '-DNO_<NAME>
hdrpglist="regcomp:regex.h:rx.h mmap:mman.h dbopen:db.h
           setsid:unistd.h putenv:stdlib.h tv_usec:time.h:/sys/time.h
           usleep:unistd.h:libc.h memset:string.h random:stdlib.h:libc.h
           lrand48:stdlib.h setproctitle:libutil.h"
#
# List of direcrories to search for headerfiles
headerdirs="/usr/include /usr/include/sys /usr/include/bsd /usr/include/bsd/sys
            /usr/include/ansi /usr/include/linux"
#
# List of libs to search for
liblist="ncurses util"
#
# List of directories to search for libraries
libdirs="/lib /usr/lib /usr/local/lib"
#
srcdir=`pwd`
# directory in which Configure runs
confdir=$srcdir/conf
# directory in which templates are
templ=$confdir/templates
# directory in which terminal type files are
termdir=$confdir/term
# directory in which temporary progs and files are put
tmp=$confdir/tmp
#
# setting defaults
localconffile=local.conf
bbspglist="gunzip kermit sz rz sendmail"
SYSHEADER=empty.h
CC=cc
CFLAGS="-O"
MKDEPFLAG="-MM"
LIBSC=
LIBSD=
bbsdsuid=y
TERMTABLE=none
NNTPSERVER=none
NNTPPOST=y
#

cat > /tmp/c1$$ <<EOF
ARGGGHHHH!!!!!

Your csh still thinks true is false.  Write to your vendor today and tell
them that next year Configure ought to "rm /bin/csh" unless they fix their
blasted shell. :-)

[End of diatribe.  We now return you to your regularly scheduled
programming...]

EOF
cat > /tmp/c2$$ <<EOF
OOPS!  You naughty creature!  You didn't run Configure with sh!
I will attempt to remedy the situation by running sh for you...

EOF

true || cat /tmp/c1$$ /tmp/c2$$
true || exec sh $0

export PATH || cat /tmp/c2$$
export PATH || exec sh $0
rm -f /tmp/c1$$ /tmp/c2$$

PATH=".:$PATH:/bin:/usr/bin:/usr/local/bin:/usr/ucb:/usr/local:/usr/lbin:/etc:/usr/new:/usr/new/bin:/usr/nbin"

if test ! -t 0; then
    echo "Say 'sh Configure', not 'sh <Configure'"
    exit 1
fi

unset CDPATH

boPATH=""
eoPATH="/usr/ucb /bin /usr/bin /usr/local /usr/local/bin /usr/lbin /usr/plx /usr/5bin /vol/local/bin /etc /usr/lib /lib /usr/local/lib /sys5.3/bin /sys5.3/usr/bin /bsd4.3/bin /bsd4.3/usr/bin /bsd4.3/usr/ucb /bsd43/usr/bin /usr/ccs/lib /opt/SUNWste/bin /usr/opt/SUNWste/bin"
#
startsh="#!/bin/sh"
#
if [ ! -d $confdir ]; then
  echo "Cannot cd to $confdir"
  exit 0
fi
cd $confdir
if [ ! -d $tmp ];then
  mkdir $tmp
fi
#
: first determine how to suppress newline on echo command
echo "Checking echo to see how to suppress newlines..."
(echo "hi there\c" ; echo " ") >$tmp/echotmp
if fgrep c $tmp/echotmp >/dev/null 2>&1 ; then
    echo "...using -n."
    n='-n'
    c=''
else
    cat <<'EOM'
...using \c
EOM
    n=''
    c='\c'
fi
#echo $n "Type carriage return to continue.  Your cursor should be here-->$c"
echo $n "Your cursor should be here-->$c"
echo '*'
/bin/rm $tmp/echotmp
#
##############################################################################
#
: creating some auxiliary programs
cd $tmp
defread=$tmp/defread
loc=$tmp/loc
libloc=$tmp/libloc
inhdr=$tmp/inhdr
/bin/rm -f defread loc libloc inhdr
#
: "defread"
cat <<EOSC >defread
$startsh
read ans
if [ -z "\$ans" ]; then
  echo \$@
else
  echo \$ans
fi
exit 0
EOSC
chmod +x defread
#
: "loc"
cat <<EOSC >loc
$startsh
case \$# in
0) exit 1;;
esac
thing=\$1
shift
dflt=\$1
shift
for dir in \$*; do
    case "\$thing" in
    .)
	if test -d \$dir/\$thing; then
	    echo \$dir
	    exit 0
	fi
	;;
    *)
	if test -f \$dir/\$thing; then
	    echo \$dir/\$thing
	    exit 0
	fi
	;;
    esac
done
echo \$dflt
exit 1
EOSC
chmod +x loc
#
: "libloc"
cat <<EOSC >libloc
$startsh
case \$# in
0) exit 1;;
esac
thing=\$1
shift
dflt=\$1
shift
for dir in \$*; do
    for ff in \$dir/lib\$thing.*; do
	if test -f \$ff; then
	    echo \$ff
	    exit 0
	fi
    done
done
echo \$dflt
exit 1
EOSC
chmod +x libloc
#
: inhdr
cat <<EOSC > inhdr
$startsh
f=\$1
shift
p=\$*
IFS=:
set \$f
entry=\$1
hdrlist=\$*
IFS=' '
for dir in \$p; do
  for hdr in \$hdrlist; do
    if [ -f \$dir/\$hdr ]; then
      if egrep "^[^/].*[ 	*]\$entry[ ;(]" \$dir/\$hdr > /dev/null 2>&1; then
        echo \$entry
        exit 1
      fi
    fi
  done
done
echo ''
exit 0
EOSC
chmod +x inhdr
#
cd $confdir
#
##################################################################################
#
: find out where common programs are
echo " "
echo "Locating common programs..."
pth=`echo :$boPATH:$PATH:$eoPATH: | sed -e 's/:/ /g'`
for file in $loclist; do
  xxx=`$loc $file $file $pth`
  eval $file=$xxx
  if [ -f $xxx ]; then
    echo "found $file ($xxx)"
  else
    echo "$file not found"
  fi
done
echo " "
#
: find out system
system=none
if [ -f local.conf ]; then
  echo $n "Found a local configuration, shall I use it <y,n> [y]? $c"
  ans=`$defread y`
  if [ $ans = y ]; then
    system=local
    conffile=local.conf
  fi
fi
if [ $system != local ]; then
  echo "Determining System, this make take a while..."
  systemlist=none
  for file in *.sys; do
    echo $n ".$c"
    cmd="`$fgrep IDENT $file | $sed 's/.*IDENT[ 	]*\([^ 	].*\)$/\1/'`"
    if [ -n "`/bin/sh -c "$cmd" 2>/dev/null`" ]; then
      system="`$fgrep IDENT $file | $sed 's/^[# 	]*\([^ 	]*\)[ 	]*IDENT.*/\1/'`"
      systemlist="$systemlist $system"
    fi
  done
  echo " "
  conffile=' '
  while [ ! -f "$conffile" ]; do
    echo $n "Select system (${systemlist}) [${system}]: $c"
    conffile="`$defread $system`.sys"
  done
  system=`basename $conffile | sed 's/\.sys//'`
fi
if [ $system = none ]; then
  echo "Scanning System..."
  for f in $hdrpglist; do
    xxx="`$inhdr $f $headerdirs`"
    prg=`echo $f | sed 's/^\([^:]*\):.*$/\1/'`
    if [ -n "$xxx" ]; then
      echo "Found $prg"
      eval $prg=y
    else
      echo "No $prg (should run without)"
      eval $prg=n
    fi
  done
  echo " "
  for file in $liblist; do
    xxx=`$libloc $file $file $libdirs`
    if [ -f $xxx ]; then
      echo "Found library $file"
      eval $file=y
    else
      echo "Library $file not found (should run without)"
      eval $file=n
    fi
  done
  : curses-library
  if [ $ncurses = y ]; then
    LIBSC="$LIBSC -lncurses"
  else
    LIBSC="$LIBSC -lcurses -ltermcap"
  fi
  : util-library
  if [ $util = y ]; then
    LIBSC="$LIBSC -lutil"
  fi
else
  echo "Reading system-configuration..."
  . $conffile
fi
echo " "
echo "I will use conf/$SYSHEADER for system depending includes"
echo " "
#
: control configuration
ctl=y
if [ $system != none ]; then
  echo $n "Control configurtion <y,n> [n] $c"
  ctl=`$defread n`
fi
if [ $ctl = y ]; then
#
 : select compiler
  if [ -x "$cc" -a -x "$gcc" ]; then
    echo $n "Found cc (${cc}) and gcc (${gcc}). Select compiler [${CC}]: $c"
    CC=`$defread $CC`
  elif [ -x "$cc" ]; then
    CC=cc
  elif [ -x "$gcc" ]; then
   CC=gcc
  else
    echo "No compiler found, I will try cc"
    CC=cc
  fi
 : compiler-options
  echo $n "Compiler-Options (no libraries) [${CFLAGS}]: $c"
  CFLAGS="`$defread $CFLAGS`"
 : link-libraries
  echo $n "Libraries to link bbs with [${LIBSC}]: $c"
  LIBSC="`$defread $LIBSC`"
  echo $n "Libraries to link bbsd with [${LIBSD}]: $c"
  LIBSD="`$defread $LIBSD`"
  echo "Some systems lack one ore more functions/libraries (y for yes, n for no)."
 : use of ncurses
  echo $n "Use (new) ncurses instead of (old) curses <y,n> [${ncurses}]? $c"
  ncurses=`$defread $ncurses`
 : detect existence of some routines
  for f in $hdrpglist; do
    prg=`echo $f | sed 's/:.*$//'`
    eval ans=\$$prg
    echo $n "Have $prg <y,n> [${ans}]? $c"
    ans=`$defread $ans`
    eval $prg=$ans
  done
  : check binary-flag of rz
  if [ ! -x "$rz" ]; then
    echo "rz NOT FOUND, MUST BE FIXED"
    rz_bflag=n
  else
    if $rz -b </dev/null 2>/dev/null | $fgrep '**' >/dev/null
    then
      ans=y
    else
      ans=n
    fi
    echo $n "rz has option '-b' <y,n> [${ans}]? $c"
    rz_bflag=`$defread $ans`
  fi
fi
#
: translate and save results
/bin/rm -f local.conf
echo "#local	IDENT	echo local" > local.conf
echo "SYSHEADER=$SYSHEADER" >> local.conf
echo "CC=$CC" >> local.conf
echo "CFLAGS=\"$CFLAGS\"" >> local.conf
echo "MKDEPFLAG=\"$MKDEPFLAG\"" >> local.conf
echo "LIBSC=\"$LIBSC\"" >> local.conf
echo "LIBSD=\"$LIBSD\"" >> local.conf
echo "ncurses=$ncurses" >> local.conf
if [ $ncurses != y ]; then
  opts="$opts -DNO_NCURSES"
fi
for f in $hdrpglist; do
  prg=`echo $f | sed 's/:.*$//'`
  eval ans=\$$prg
  echo "$prg=$ans" >> local.conf
  if [ "$ans" != y ]; then
    upperprg=`echo $prg | $tr '[a-z]' '[A-Z]'`
    opts="$opts -DNO_$upperprg"
  fi
done
echo "rz_bflag=$rz_bflag" >> local.conf
if [ $rz_bflag != y ]; then
  opts="$opts -DNO_RZ_BFLAG"
fi
CFLAGS="$CFLAGS $opts"
echo " "
#
: installdir, owner, group, ...
echo "Now we want the location for the bbs-installation and the chroot-directory,"
echo "last will appear as '/' for the clients."
echo "For best security, the installation-directory should not be inside from"
echo "chroot-directory".
echo $n "Install-Directory [$INSTALLDIR]: $c"
INSTALLDIR=`$defread $INSTALLDIR`
echo "INSTALLDIR=$INSTALLDIR" >> local.conf
echo $n "Chroot-Directory [$CHROOTDIR]: $c"
CHROOTDIR=`$defread $CHROOTDIR`
echo "CHROOTDIR=$CHROOTDIR" >> local.conf
echo " "

echo "Next we want owner and group for clients (bbs) and the daemon (bbsd)."
echo "For best security, the owners should be different (groups are not important)."
echo "The clients are started from getty normally with its uid and the first client"
echo "launch by fork+exec the daemon, so bbsd should be run suid to his uid."
echo "This is also necessary in case bbsd is launched by bbsmail."
echo $n "Client-Owner [$BBS_OWNER]: $c"
BBS_OWNER=`$defread $BBS_OWNER`
echo "BBS_OWNER=$BBS_OWNER" >> local.conf
echo $n "Client-Group [$BBS_GROUP]: $c"
BBS_GROUP=`$defread $BBS_GROUP`
echo "BBS_GROUP=$BBS_GROUP" >> local.conf
echo $n "Daemon-Owner [$BBSD_OWNER]: $c"
BBSD_OWNER=`$defread $BBSD_OWNER`
echo "BBSD_OWNER=$BBSD_OWNER" >> local.conf
echo $n "Daemon-Group [$BBSD_GROUP]: $c"
BBSD_GROUP=`$defread $BBSD_GROUP`
echo "BBSD_GROUP=$BBSD_GROUP" >> local.conf
echo $n "Running daemon suid $BBSD_OWNER (y for yes, n for no) [$bbsdsuid] ? $c"
ans=`$defread $bbsdsuid`
echo "bbsdsuid=$bbsdsuid" >> local.conf
if [ "$ans" = n ]; then
  BBSD_SUID=''
else
  BBSD_SUID=$BBSD_OWNER
fi
echo " "

echo $n "Sysopname: [$SYSOP] $c"
SYSOP=`$defread $SYSOP`
echo "SYSOP=$SYSOP" >> local.conf
echo " "

echo "BBS may use a menu table for terminal types. You may create one or use"
echo "one of it already included (directory conf/term)."
echo "Enter 'none' for no table."
termtablelist="`$ls term/term-* | $sed 's#term/##g' | $tr '\012' ' '`"
if [ $TERMTABLE = none ]; then
  for ttf in $termtablelist; do
    case $ttf in
      term-${system}) TERMTABLE=$ttf; break;;
    esac
  done
fi
termtablefile=/
while [ ! -f $termtablefile ]; do
  echo $n "Table of terminal types: (none ${termtablelist}) [$TERMTABLE] $c"
  TERMTABLE=`$defread $TERMTABLE`
  termtablefile=$termdir/$TERMTABLE
done
echo "TERMTABLE=$TERMTABLE" >> local.conf
if [ $TERMTABLE = none ]; then
  TERMTABLE=
  TERMTABPATH=
else
  TERMTABPATH=$INSTALLDIR/etc/termtable
fi

echo "BBS may support NetNews. If you not want it, enter 'none' for your NNTP"
echo "Newsserver"
if [ -z "$NNTPSERVER" ]; then
  NNTPSERVER=none
fi
echo $n "NNTP server: [$NNTPSERVER] $c"
NNTPSERVER=`$defread $NNTPSERVER`
if [ $NNTPSERVER != none ]; then
  CFLAGS="$CFLAGS -DNNTPNEWS"
  echo "Send NetNews postings by NNTP (enter 'y') or by inews (enter 'n') ?"
  echo $n "Post by NNTP: [$NNTPPOST] $c"
  NNTPPOST=`$defread $NNTPPOST`
  echo "If you only use NNTP to post, you may ignore a not found inews later."
  bbspglist="$bbspglist inews"
else
  NNTPSERVER=
fi
echo "NNTPSERVER=$NNTPSERVER" >> local.conf
echo "NNTPPOST=$NNTPPOST" >> local.conf
echo " "

#
: locate some programs needed for bbs
echo " "

echo "Searching vor some programms called by bbs:"
pth=`echo :$boPATH:$PATH:$eoPATH: | sed -e 's/:/ /g'`
for file in $bbspglist; do
  path=`$loc $file $file $pth`
  def="`echo $file | $tr '[a-z]' '[A-Z]' `"
  if [ -x $path ]; then
    echo "found $file ($path)"
    set $def $path
    sedcmd="$sedcmd -e s#X_$def#$path#"
  else
    echo "$file NOT FOUND, SHOULD BE FIXED"
    missingbbspg=t
    sedcmd="$sedcmd -e s#X_$def#/bin/false#"
  fi
done
echo " "
#
: creating key and password
BBS_AUTHKEY_FIX="`date | $tr ' ' '-'`"
BBS_AUTHKEY_VAR=`/bin/sh -c 'echo P$$'`$$
#
cd $srcdir
#
: creating files
echo $n "Creating local.h... $c"
test -f local.h && mv local.h local.h.old
sed -e "s#X_SYSHEADER#$SYSHEADER#" \
    -e "s#X_INSTALLDIR#$INSTALLDIR#" \
    -e "s#X_NNTPSERVER#$NNTPSERVER#" \
    -e "s#X_NNTPPOST#$NNTPPOST#" \
    -e "s#X_CHROOTDIR#$CHROOTDIR#" \
    -e "s#X_BBSD_SUID#$BBSD_SUID#" \
    -e "s#X_SYSOP#$SYSOP#" \
    -e "s#X_TERMTABPATH#$TERMTABPATH#" \
    -e "s#X_BBS_AUTHKEY_FIX#$BBS_AUTHKEY_FIX#" \
    -e "s#X_BBS_AUTHKEY_VAR#$BBS_AUTHKEY_VAR#" \
    $sedcmd $templ/local-in.h > local.h && echo ok

echo $n "Creating Makefile... $c"
test -f Makefile && mv Makefile Makefile.old
sed -e "s#X_CC#$CC#" \
    -e "s#X_CFLAGS#$CFLAGS#" \
    -e "s#X_MKDEPFLAG#$MKDEPFLAG#" \
    -e "s#X_INSTALLDIR#$INSTALLDIR#" \
    -e "s#X_CHROOTDIR#$CHROOTDIR#" \
    -e "s#X_BBS_OWNER#$BBS_OWNER#" \
    -e "s#X_BBS_GROUP#$BBS_GROUP#" \
    -e "s#X_BBSD_OWNER#$BBSD_OWNER#" \
    -e "s#X_BBSD_GROUP#$BBSD_GROUP#" \
    -e "s#X_BBSD_SUID#$BBSD_SUID#" \
    -e "s#X_TERMTABLE#$TERMTABLE#" \
    -e "s#X_TERMTABPATH#$TERMTABPATH#" \
    -e "s#X_INSTALL#$install#" \
    -e "s#X_CHOWN#$chown#" \
    -e "s#X_CHGRP#$chgrp#" \
    -e "s#X_CHMOD#$chmod#" \
    -e "s#X_CP#$cp#" \
    -e "s#X_MKDIR#$mkdir#" \
    -e "s#X_RM#$rm#" \
    -e "s#X_FIND#$find#" \
    -e "s#X_XARGS#$xargs#" \
    -e "s#X_CTAGS#$ctags#" \
    -e "s#X_LIBSC#$LIBSC#" \
    -e "s#X_LIBSD#$LIBSD#" $templ/Makefile-in > Makefile && echo ok

#echo $n "dependices... $c"
#make depend
#echo ok

echo $n "Creating bbs.conf... $c"
test -f bbs.conf && mv bbs.conf bbs.conf.old
sed -e "s#X_INSTALLDIR#$INSTALLDIR#" \
    -e "s#X_BBSD_SUID#$BBSD_SUID#" \
    -e "s#X_SYSOP#$SYSOP#" \
    -e "s#X_TERMTABPATH#$TERMTABPATH#" \
    -e "s#X_NNTPSERVER#$NNTPSERVER#" \
    -e "s#X_NNTPPOST#$NNTPPOST#" \
    -e "s#X_BBS_AUTHKEY_VAR#$BBS_AUTHKEY_VAR#" \
    -e "s#X_CHROOTDIR#$CHROOTDIR#" $templ/bbs.conf-in > bbs.conf && echo "ok"

echo $n "Creating kermrc...$c"
test -f kermrc && mv kermrc kermrc.old
cp $templ/kermrc-in kermrc && echo "ok"

echo $n "Creating bbsmail.m4...$c"
test -f bbsmail.m4 && mv bbsmail.m4 bbsmail.m4.old
sed -e "s#X_INSTALLDIR#$INSTALLDIR#" $templ/bbsmail.m4-in > bbsmail.m4 && echo "ok"

echo $n "Creating users... $c"
test -f users && mv users users.old
sed -e "s#X_CHROOTDIR#$CHROOTDIR#" \
    -e "s#X_SYSOP#$SYSOP#" $templ/users-in > users && echo "ok"

echo
echo "cleaning up..."
make clean

if [ -n "$missingbbspg" ]; then
  echo
  echo "Your system lacks one ore more programs used by 'bbs'"
  echo "causing some loss of functionality."
  echo "Try to get it and run 'Configure' again to gain full functionality."
fi

echo
echo "Check local.h and Makefile before compiling"
echo "** Don't forget to change passwords after install **"
echo
