#!/bin/sh
# Simple configure script for term.  No feature detection is done; we just
# figure out which OS we are using and modify the Makefile appropriately.
#
# The argument parsing and srcdir code are modified Gnu autoconf output.
# That puts this script under GPL, but there is no configure.in to 
# distribute along with it; so it goes.
#
# Chris Metcalf, June 1994
# Modified June 1994, bcr@physics.purdue.edu

builddir=unknown
prefix=unknown
srcdir=unknown
jump_lib=unknown
jump_dir=unknown
bindir=unknown
libdir=unknown
incdir=unknown
mandir=unknown
shlib=unknown
sharedir=unknown
is_root=unknown
one_client=unknown
wrap=unknown
sockslib=unknown
Makefile=Makefile
STRIP='strip'
workdir=unknown

# Figure out how to echo without newline
if [ x"`echo -n`" = x-n ]; then
       echo_n=
       echo_c='\c'
else
       echo_n=-n
       echo_c=
fi


for arg
do
  # 	Handle arguments with a space
  if [ x$next_Makefile = xyes ] ; then Makefile=$arg; next_Makefile=
  elif [ x$next_prefix = xyes ] ; then prefix=$arg; next_prefix=
  elif [ x$next_srcdir = xyes ] ; then srcdir=$arg; next_srcdir=
  elif [ x$next_jump_lib = xyes ] ; then jump_lib=$arg; next_jump_lib=
  elif [ x$next_mandir = xyes ] ; then mandir=$arg; next_mandir=
  elif [ x$next_bindir = xyes ] ; then bindir=$arg; next_bindir=
  elif [ x$next_incdir = xyes ] ; then incdir=$arg; next_incdir=
  elif [ x$next_libdir = xyes ] ; then libdir=$arg; next_libdir=
  elif [ x$next_sockslib = xyes ] ; then sockslib=$arg; next_sockslib=
  elif [ x$next_sharedir = xyes ] ; then sharedir=$arg; next_sharedir=
  else
    case $arg in
     -Makefile=* | --Makefile=*)
	Makefile=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -M | -Makefile | --Makefile)
	next_Makefile=yes ;;

     -prefix=* | --prefix=*)
	prefix=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -p | -prefix | --prefix)
	next_prefix=yes ;;

     -srcdir=* | --srcdir=*)
	srcdir=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -s | -srcdir | --srcdir)
	next_srcdir=yes ;;

     -jump_lib=* | --jump_lib=*)
	jump_lib=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -j | -jump_lib | --jump_lib)
	next_jump_lib=yes ;;

     -mandir=* | --mandir=*)
	mandir=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -m | -mandir | --mandir)
	next_mandir=yes ;;

     -n | -noclients | --noclients)
	no_clients=yes ;;

     -bindir=* | --bindir=*)
	bindir=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -b | -bindir | --bindir)
	next_bindir=yes ;;

     -incdir=* | --incdir=*)
	incdir=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -i | -incdir | --incdir)
	next_incdir=yes ;;

     -libdir=* | --libdir=*)
	libdir=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -l | -libdir | --libdir)
	next_libdir=yes ;;

     -sharedir=* | --sharedir=*)
	sharedir=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -S | -sharedir | --sharedir)
	next_sharedir=yes ;;

     -sockslib=* | --sockslib=*)
	sockslib=`echo $arg | sed 's/[-A-Za-z_]*=//'` ;;
     -L | -sockslib | --sockslib)
	next_sockslib=yes ; no_clients=yes ;;

     -u | -user | --user)
       is_root=no ;;

     -r | -root | --root)
       is_root=yes ;;

     -h | -usage | --usage | -help | --help)
       echo 'Usage: configure [<OS Type>] [--user] [--root] [--prefix=/usr/local]'
       echo '                 [--mandir=$prefix/man/man1] [--bindir=$prefix/bin]'
       echo '                 [--libdir=$prefix/lib] [--sharedir=$libdir/term]'
       echo '                 [--incdir=$prefix/include] [--sockslib=] [--verbose]'
       echo '                 [--shlib] [--static] [--verbose]'
       exit 0 ;;

     -v | -verbose | --verbose)
       verbose=yes ;;

     -shlib | --shlib)
       shlib=yes ;;

     -static | --static)
       shlib=no ;;

     *) OS=$arg ;;
    esac
  fi
done


# A filename unique to this package, relative to the directory that
# configure is in, which we can look for to find out if srcdir is correct.
unique_file=term_putenv.c

# Find the source files, if location was not specified.
if [ "x$srcdir" = xunknown ] ; then
  srcdirdefaulted=yes
  # Try the directory containing this script, then `..'.
  prog=$0
  confdir=`echo $prog|sed 's%/[^/][^/]*$%%'`
  [ "X$confdir" = "X$prog" ] && confdir=.
  srcdir=$confdir
  if [ ! -r $srcdir/$unique_file -a ! -r "$srcdir/$unique_file",v ]; then
    srcdir=..
  fi
fi

if [ ! -r $srcdir/$unique_file -a ! -r "$srcdir/$unique_file",v ] ; then
  if [ x$srcdirdefaulted = xyes ] ; then
    echo "configure: Can not find sources in \`${confdir}' or \`..'." 1>&2
  else
    echo "configure: Can not find sources in \`${srcdir}'." 1>&2
  fi
  exit 1
fi

# Preserve a srcdir of `.' to avoid automounter screwups with pwd.
# But we can't avoid them for `..', to make subdirectories work.
case $srcdir in
  .|/*|~*) ;;
  *) srcdir=`cd $srcdir; pwd` ;; # Make relative path absolute.
esac

# Currently, this is only need for Linux...  Even then only
# for very special cases.
if [ "x$workdir" = xunknown ]; then
  workdir=`dirname $Makefile`
  case "$workdir" in
    "$srcdir"|/*|~*) ;;
    *) workdir=`cd $workdir; pwd` ;; # Try to resolve non-absolute paths.
  esac
fi

# If you cd and need to find the srcdir
abs_srcdir=`cd $srcdir; pwd`
if [ x$jump_lib = xunknown ] ; then
  jump_dir=$abs_srcdir"/jump"
  jump_lib=libtermnet
else
  jump_dir=`pwd`/jump
fi  

if [ x$is_root = xunknown ] ; then
  if [ -w /etc/passwd ] ; then
    is_root=yes  
  elif [ x$USER = xroot ] ; then
    is_root=yes 
  elif [ x$LOGNAME = xroot ] ; then
    is_root=yes
  else 
    ans=
    while [ x$ans != xy -a x$ans != xn ] ; do  
      echo $echo_n "Can you install as root? (y/n) $echo_c"
      read ans
    done
    if [ x$ans = xy ] ; then
      is_root=yes
    else
      is_root=no
    fi
  fi
fi
  
if [ x$is_root = xyes ] ; then
  setid=g+s
  if [ x$prefix = xunknown ] ; then prefix=/usr/local ; fi
  if [ x$libdir = xunknown ] ; then libdir=$prefix/lib ; fi
  if [ x$sharedir = xunknown ] ; then sharedir=$libdir/term ; fi
else
  setid=u+s
  if [ x$prefix = xunknown ] ; then prefix=$HOME ; fi
  if [ x$sharedir = xunknown ] ; then sharedir=$prefix/term ; fi
  if [ x$libdir = xunknown ] ; then libdir=$prefix/term ; fi
fi
if [ x$bindir = xunknown ] ; then bindir=$prefix/bin ; fi
if [ x$mandir = xunknown ] ; then mandir=$prefix/man/man1 ; fi
if [ x$incdir = xunknown ] ; then incdir=$prefix/include ; fi

CC='gcc -Wall'
CPP='$(CC) -E'
DEBUGFLAGS='-O'
if [ -f /usr/bin/ranlib -o -f /bin/ranlib -o -f /usr/ucb/ranlib ]; then
  AR='ar rc'
  RANLIB='ranlib'
else
  AR='ar rcs'
  RANLIB='echo'
fi

# If no OS type provided yet, try to guess

# Probably the best thing to check is uname, so we try that first.	
if [ "x$OS" = x -a \( -f /bin/uname -o -f /usr/bin/uname \) ]; then
	OS=`uname` 
fi
# Next we will try arch.
if [ "x$OS" = x -a \( -f /bin/arch -o -f /usr/bin/arch \) ]; then
	OS=`arch`
fi
# What about bash, if we're running it?
if [ "x$OS" = x -a x$HOSTTYPE != x ]; then
	OS=$HOSTTYPE
fi
# Or running bash, if we're not running it?
if [ "x$OS" = x -a \( -f /bin/bash -o -f /usr/bin/bash \) ]; then
	OS=`bash -c 'echo $HOSTTYPE'` 
fi
# Finally we can try "tcsh".
if [ "x$OS" = x -a \( -f /bin/tcsh -o -f /usr/bin/tcsh \) ]; then
	OS=`tcsh -c 'echo $HOSTTYPE'` 
fi
# OK, try filesystem-specific things.
if [ "x$OS" = x -a -d /NextApps ]; then
	OS=next
elif [ "x$UNIX_SV" != x ]; then
	OS=unixsv
fi
# Give up
if [ "x$OS" = x ]; then
	echo "Can't guess OS; use 'configure os-name'" >&2
	exit 1
fi

# Normalize the OS type
#
case "$OS" in
Linux | i386-linux)	OS=linux ;;
TitanOS)	OS=titan ;;
NetBSD)		OS=netbsd ;;
FreeBSD | freebsd)	OS=netbsd ;;
SunOS)		case `uname -r` in
		4.*)	OS=sun ;;
		5.*)	OS=sol2 ;;
		*)	echo "Unknown Sun OS release!" >&2; exit 1 ;;
		esac ;;
sol21)          OS=sol2 ;;      # compatibility with older term releases
cd4680fs | epix)	OS=epix ;;
ULTRIX)		OS=ultrix ;;
IRIX)		OS=irix ;;
NeXT)		OS=next ;;
CX/UX)		OS=hcx ;;
aix370 | aixESA | rtpc | AIX)	OS=aix ;;
bsd386)		OS=bsdi ;;
dgux)		OS=dgux ;;
sco386)		OS=sco ;;
vistra800)	OS=svr4 ;;
ptx)		OS=dynixptx ;;
iris3d | iris4d)	OS=sgi ;;
OSF1)		OS=osf1 ;;
m68k)           if [ "x$MACHTEN" != x ]; then OS=machten; fi ;;
ConvexOS)	OS=convex ;;
HP-UX)		OS=hpux ;;
ISC)            OS=isc ;;
UNIX_SV)	OS=unixsv ;;

esac

# set OS specific options
#
case $OS in

sgi | convex | mips)	;;

osf1)		CC="gcc -Wall -Wno-implicit" ;;

linux)		if [ x$shlib = xunknown ]; then shlib=$is_root; fi
		if [ x$shlib = xyes ]; then 
			if [ -x /usr/bin/jumpas ]; then
				DLLAS=jumpas
				DLLBIN=
			elif [ -x /usr/dll/jump/as ]; then
				DLLAS=/usr/dll/jump/as
				DLLBIN=/usr/dll/bin/
			else
				echo "You need dll tools 2.11 or greater for"
				echo "dynamic libraries.  Using static instead."
				shlib=no
			fi
		fi
		if [ $shlib = yes ]; then
		  ARCH_VARS="-e 's,@dllas@,$DLLAS,' -e 's,@dllbin@,$DLLBIN,'"
		fi ;;

machten)	CC="gcc" ;;

bsdi)		CPP="cc -E" CC=cc
		OSFLAGS="-funsigned-char -DBSDI" ;;

epix)		CPP="cc -E" CC=cc
		OSFLAGS="-systype bsd43 -DEPIX" ;;

titan) 		CC="cc -43" ;;

aix)		OSFLAGS="-D_BSD" LIBS="-lbsd" CC="xlc -D_ALL_SOURCE" ;;

netbsd)		if [ x$shlib = xunknown ]; then shlib=yes; fi ;;

sun)		CC="gcc -Wall -Wno-implicit"
		if [ x$shlib = xunknown ]; then shlib=yes; fi ;;

unixsv)		OSFLAGS="-DSVR4 -DUNIX_SV" LIBS="-lsocket -lnsl" ;;

sol2)		OSFLAGS="-DSVR4" LIBS="-lsocket -lnsl" 
		if [ x$shlib = xunknown ]; then shlib=yes; fi
		if [ x$wrap = xunknown ]; then wrap=yes; fi ;;

ultrix)		CC="gcc -Wall -Wno-implicit -Wno-comment -Dultrix"
		OSLINK=" -s" STRIP="echo" ;;

irix)		OSFLAGS="-DIRIX" ;;

next)		CPP="cc -E" CC=cc OSFLAGS="-funsigned-char" ;;

# if this doesn't work try c89 instead of cc.
# use only if you don't have gcc; at least hpux 8.x cc is broken.
# Or better, get gcc from hpux.ask.uni-karlsruhe.de:/hpux9/Gnu/
hpux-cc)	CC="cc -Aa" OSLINK=" -s" OSFLAGS="-D_HPUX_SOURCE"
		STRIP="echo" ;;

hpux)		CC="gcc" OSLINK=" -s" OSFLAGS="-D_HPUX_SOURCE" STRIP="echo" ;;

isc)            CC="gcc -posix" DEBUGFLAGS="-g" OSFLAGS="-DISC -DNO_UNIX_DOMAIN -DSVR3 -D_SYSV3"
		LIBS="-linet -lpt -lcposix " ;;

svr4)		OSFLAGS="-funsigned-char -m486 -DSVR4" LIBS="-lsocket -lnsl" ;;

dgux)		OSFLAGS="-DDGUX -DSVR3"
		CC=cc DEBUGFLAGS=-g ;;

sco)		OSFLAGS="-DSCO -DSVR3 -DNO_UNIX_DOMAIN"
		LIBS="-lsocket -lpt" CC=cc DEBUGFLAGS=-g ;;

dynixptx)	CC="cc -DDYNIXPTX" CPP="cc -E" 
		LIBS="-lsocket -linet -lnsl -lseq" ;;

# The HCX/UX 5.1 cc does not understand ANSI C.  To convert the term source
# to K&R on the fly, "unproto" must be compiled and the generated "cpp" 
# binary placed in some directory, specified below with UNPROTO.  This lets the 
# hcx cc be passed de-ANSIfied C code.  "unproto" can be found in archives 
# for comp.sources.misc, volume 27, issue 85.
#
hcx)		if [ "x$UNPROTO" = x ]; then
		  UNPROTO=/path/to/unproto/dir
	        fi
	        if [ ! -f "$UNPROTO"/cpp ]; then
	          echo "hcx: can't find $UNPROTO/cpp.  Please either edit" >&2
	          echo " configure or setenv UNPROTO before running it." >&2
	          exit 1
	  	fi
		CC="cc -B$UNPROTO/ -tp"
		CPP=$UNPROTO/cpp ;;

*)		echo "Couldn't find rules for $OS; try 'configure os-name'"
		exit 1 ;;

esac

if [ $is_root = no ] ; then
	who_prefix='!root'
	who_skip='root'
else
	who_prefix='root'
	who_skip='!root'
fi

if [ $wrap = yes ] ; then
	wrap_prefix='wrap'
	wrap_skip='!wrap'
else
	wrap_prefix='!wrap'
	wrap_skip='wrap'
fi

if [ $shlib = yes ]; then
	lib_prefix='shlib'
	lib_skip='shared'
	install_shlib=install.shlib
	if [ -f /usr/sbin/ldconfig ]; then
		ldconfig=/usr/sbin/ldconfig
	elif [ -f /usr/etc/ldconfig ]; then
		ldconfig=/usr/etc/ldconfig
	elif [ -f /usr/bin/ldconfig ]; then
		ldconfig=/usr/bin/ldconfig
	elif [ -f /sbin/ldconfig ]; then
		ldconfig=/sbin/ldconfig
	elif [ -f /etc/ldconfig ]; then
		ldconfig=/etc/ldconfig
	elif [ -f /bin/ldconfig ]; then
		ldconfig=/bin/ldconfig
	elif [ $OS = sol2 ]; then
		ldconfig=/bin/true
	else
		echo "Can't find ldconfig."
		echo "ldconfig must be in root's PATH at install time."
		ldconfig=ldconfig
	fi
else
	lib_prefix='static'
	lib_skip='shlib'
	install_shlib=
	ldconfig=this_should_never_be_used
	if [ x$is_root = xno ] ; then
		CC="$CC -DONE_CLIENT"
		if [ x$one_client = xunknown ]; then
			one_client=yes;
		fi
	fi
fi

if [ x$no_clients = xyes ]; then
	no_clients_prefix="no_clients"
	no_clients_skip="!no_clients"
else
	no_clients_prefix="!no_clients"
	no_clients_skip="no_clients"
fi

if [ x$one_client = xyes ]; then
	one_client_prefix="one_client"
	one_client_skip="!one_client"
else
	one_client_prefix="!one_client"
	one_client_skip="one_client"
fi

if [ x$sockslib != xunknown ]; then
	sockslib_prefix="socks"
	sockslib_skip="!socks"
else
        sockslib=
	sockslib_prefix="!socks"
	sockslib_skip="socks"
fi

if [ x$jump_lib = xlibc ] ; then
  OSFLAGS="$OSFLAGS -DBUILD_LIBC"
else
  OSFLAGS="$OSFLAGS -DBUILD_LIBTERMNET"
fi

depend=$Makefile.depend

rm -f config.status

echo "#!/bin/sh" > config.status

cat >> config.status << FOO
rm -f $Makefile
if [ ! -f $srcdir/Makefile.in ] ; then
  co $srcdir/Makefile.in
fi

echo "(Re)building the Makefile."
sleep 1

eval \`sed -n -e 's/  *//g' -e '/^VERSION_MAJOR=/p' -e '/^VERSION_MINOR=/p' \\
    -e '/^PATCH_LEVEL=/p' $srcdir/Makefile.in\`
VERSION_MINOR=\`expr \( \$VERSION_MINOR + 100 \) : '.\(..\)'\`
PATCH_LEVEL=\`expr \( \$PATCH_LEVEL + 100 \) : '.\(..\)'\`
VERSION_NUM=\$VERSION_MAJOR\$VERSION_MINOR\$PATCH_LEVEL

echo '# This file was created from Makefile.in by ./configure.  You are ' \\
    > $Makefile
echo '# advised not to edit this file, but instead edit Makefile.in.'  \\
    >> $Makefile


sed -e 's,^#if  *$sockslib_prefix: *,,g' \\
    -e 's,^#if  *$who_prefix: *,,g' \\
    -e 's,^#if  *$wrap_prefix: *,,g' \\
    -e 's,^#if  *$one_client_prefix: *,,g' \\
    -e 's,^#if  *$no_clients_prefix: *,,g' \\
    -e 's,^#if  *$lib_prefix=$OS: *,,g' \\
    -e 's,^#if  *$lib_prefix=any: *,,g' \\
    -e '/^#if  *$who_skip *\$/,/^#endif  *$who_skip *\$/d' \\
    -e '/^#if  *$wrap_skip *\$/,/^#endif  *$wrap_skip *\$/d' \\
    -e '/^#if  *$sockslib_skip *\$/,/^#endif  *$sockslib_skip *\$/d' \\
    -e '/^#if  *$no_client_skip *\$/,/^#endif  *$no_client_skip *\$/d' \\
    -e '/^#if  *$one_client_skip *\$/,/^#endif  *$one_client_skip *\$/d' \\
    -e '/^#if  *$lib_skip=[A-Z0-9a-z]* *\$/,/#endif  *$lib_skip=[A-Z0-9a-z]* *\$/d' \\
    -e '/^#if  *$lib_prefix=$OS *\$/,/^#endif  *$lib_prefix=$OS *\$/p' \\
    -e '/^#if  *$lib_prefix=any *\$/,/^#endif  *$lib_prefix=any *\$/p' \\
    -e '/^#if  *$lib_prefix=[A-Z0-9a-z]* *\$/,/^#endif  *$lib_prefix=[A-Z0-9a-z]* *\$/d' \\
    < $srcdir/Makefile.in | \\
    sed -e 's, *$,,g' \\
    -e "s,@version_num@,\$VERSION_NUM,g" \\
    -e 's,@workdir@,$workdir,g' \\
    -e 's,@srcdir@,$srcdir,g' \\
    -e 's,@abs_srcdir@,$abs_srcdir,g' \\
    -e 's,@jump_dir@,$jump_dir,g' \\
    -e 's,@jump_lib@,$jump_lib,g' \\
    -e 's,@cc@,$CC,g' \\
    -e 's,@cpp@,$CPP,g' \\
    -e 's,@ar@,$AR,g' \\
    -e 's,@OS@,$OS,g' \\
    -e 's,@ranlib@,$RANLIB,g' \\
    -e 's,@strip@,$STRIP,g' \\
    -e 's,@setid@,$setid,g' \\
    -e 's,@libs@,$LIBS,g' \\
    -e 's,@osflags@,$OSFLAGS,g' \\
    -e 's,@incdir@,$incdir,g' \\
    -e 's,@libdir@,$libdir,g' \\
    -e 's,@sockslib@,$sockslib,g' \\
    -e 's,@debugflags@,$DEBUGFLAGS,g' \\
    -e 's,@oslink@,$OSLINK,g' \\
    -e 's,@bindir@,$bindir,g' \\
    -e 's,@mandir@,$mandir,g' \\
    -e 's,@sharedir@,$sharedir,g' \\
    -e 's,@ldconfig@,$ldconfig,g' \\
    -e 's,@install_shlib@,$install_shlib,g' \\
    $ARCH_VARS \\
    -e '/^#if */d' \\
    -e '/^#endif */d' >> $Makefile

if [ -f $depend ] ; then
	cat $depend >> $Makefile
else
	cat $srcdir/Makefile.depend.in >> $Makefile
fi
FOO
chmod +x config.status
./config.status

echo "term configured to build on $OS and install in $prefix"

if [ $shlib = no ]; then
	echo "use of dynamic term libraries suppressed"
fi

if [ $shlib = yes ]; then
	cat << FOO

  Note that if you want to test the binaries before doing a full
  install, you may need to run "make install-shlib" first as root
  so that they are able to find the correct dynamic libraries.

FOO

      if [ $OS = sol2 ]; then
              cat << FOO
  Alternatively, you could include . in your LD_LIBRARY_PATH, eg:

    LD_LIBRARY_PATH=.:\$LD_LIBRARY_PATH export LD_LIBRARY_PATH  # for sh or ksh

    setenv LD_LIBRARY_PATH .:\$LD_LIBRARY_PATH  # for csh or tcsh

FOO
      fi

	if [ $OS = linux ]; then
		cat << FOO

  You can test the binaries by linking them with the static library
  instead: make static.bin will create them. make clean.bin removes
  them for re-linking.

FOO
	fi
fi
