#!/bin/sh
#
#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 sed fgrep rz install chown chgrp chmod cp mkdir rm ctags"
#
# List of routine:headerfilelist to search for, routine=y if found, if not,
# sedcmd will be prolonged by '-DNO_<NAME>
hdrpglist="regcomp:regex.h mmap:mman.h vsyslog:syslog.h:libc.h
           setsid:unistd.h putenv:stdlib.h tv_usec:time.h
           usleep:unistd.h:libc.h memset:string.h random:stdlib.h:libc.h
           lrand48:stdlib.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"
#
#
srcdir=`pwd`
# directory in which Configure runs
confdir=$srcdir/conf
# directory in which templates are
templ=$confdir/templates
# directory in which temporary progs and files are put
tmp=$confdir/tmp
#
# setting defaults
localconffile=local.conf
bbspglist="kermit sz rz"
SYSHEADER=empty.h
CC=cc
CFLAGS="-O"
MKDEPFLAG="-MM"
LIBSC="-lcurses -ltermcap"
LIBSD=
bbsdsuid=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

(alias) >/dev/null 2>&1 && \
    echo "(I see you are using the Korn shell.  Some ksh's blow up on Configure," && \
    echo "especially on exotic machines.  If yours does, try the Bourne shell instead.)"

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"
read ans
/bin/rm $tmp/echotmp
#
##############################################################################
#
: creating some auxiliary programs
cd $tmp
defread=$tmp/defread
loc=$tmp/loc
inhdr=$tmp/inhdr
/bin/rm -f defread loc 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
#
: 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
else
  echo "Reading system-configuration..."
  . $conffile
fi
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`"
 : detect existence of some routines
  echo "Some systems lack one ore more functions (y for yes, n for no)."
  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
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 $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) [y] ? $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
#
: 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_CHROOTDIR#$CHROOTDIR#" \
    -e "s#X_BBSD_SUID#$BBSD_SUID#" \
    -e "s#X_SYSOP#$SYSOP#" \
    -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_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_CTAGS#$ctags#" \
    -e "s#X_LIBSC#$LIBSC#" \
    -e "s#X_LIBSD#$LIBSD#" $templ/Makefile-in > Makefile

#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_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 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
