#!/bin/sh
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# This attempts to discover things about the current system,  and  do
# as much as it can to set things up so that everything compiles.

# If  you  know  what  type  system  you  are on, set the environment
# variable SYSTYPE to  the  name.   If  the  file  ${SYSTYPE}_local.h
# exists, we will use it.  If not, we will try to create it.

#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

E=/bin/echo

SYSMSG=0
if [ ! -n "$SYSTYPE" ];then
	SYSMSG=1
	$E Trying to figure out what sort of system this is ...
	SYSTYPE=`uname -s`
	export SYSTYPE
fi
if [ ! -n "$SYSTYPE" ];then
	$E Unknown system type, trying hostype script ...
	eval `sh/hostype -she`
fi
if [ ! -n "$SYSTYPE" ];then
	$E Unknown system type, using unix.
	set SYSTYPE unix
	export SYSTYPE
fi
$E System type: $SYSTYPE
if [ ! $SYSMSG ];then
	$E "--- You should probably add SYSTYPE=$SYSTYPE to your environment:"
	$E "    setenv SYSTYPE $SYSTYPE   (for csh/tcsh users)"
	$E "    export SYSTYPE=$SYSTYPE   (for sh/ksh/bash users)"
	$E ""
fi

L=${SYSTYPE}_local.h
if [ -f $L ];then
	$E "$L exists; we'll use it."
	/bin/rm -f local.h
	/bin/ln $L local.h
	exit 0
fi

$E "$L does not exist; let's try to create it ..."

# Standard kludge to prevent #inclusion more than once:
$E >$L "#ifndef local_h"
$E>>$L "#define local_h"
$E>>$L "/* $SYSTYPE local.h definitions:"
$E>>$L "*/"
$E>>$L ""

$E>>$L "#define SYSTYPE $SYSTYPE"
$E>>$L "#define $SYSTYPE 1"

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/* Terminal-I/O packages:"
$E>>$L "*/"
$E "These terminal-I/O packages were found:"
USE_sgtty=0
HAS_sgtty=0
USE_termio=0
HAS_termio=0
USE_termios=0
HAS_termios=0
if man sgtty   >/dev/null ;then HAS_sgtty=1  ; $E 1. sgtty  ; which=1; fi
if man termio  >/dev/null ;then HAS_termio=1 ; $E 2. termio ; which=2; fi
if man termios >/dev/null ;then HAS_termios=1; $E 3. termios; which=3; fi
$E -n "Which shall I use? [$which] "
read N
if [ ! -n "$N" ];then N=$which; fi
$E N=$N
if [ $N -eq 1 ];then USE_sgtty=1  ; else USE_sgtty=0  ; fi
if [ $N -eq 2 ];then USE_termio=1 ; else USE_termio=0 ; fi
if [ $N -eq 3 ];then USE_termios=1; else USE_termios=0; fi
$E HAS_sgtty:   $HAS_sgtty
$E USE_sgtty:   $USE_sgtty
$E HAS_termio:  $HAS_termio
$E USE_termio:  $USE_termio
$E HAS_termios: $HAS_termios
$E USE_termios: $USE_termios
$E>>$L "#define HAS_sgtty"   $HAS_sgtty
$E>>$L "#define USE_sgtty"   $USE_sgtty
$E>>$L "#define HAS_termio"  $HAS_termio
$E>>$L "#define USE_termio"  $USE_termio
$E>>$L "#define HAS_termios" $HAS_termios
$E>>$L "#define USE_termios" $USE_termios
$E>>$L "#define DTR_time 3	/* How long to keep DTR low - see DTRdu() */"

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/* How to determine this machine's symbolic name:"
$E>>$L "*/"
$E "How to determine this machine's symbolic name:"
USE_gethostname=0
HAS_gethostname=0
USE_utsname=0
HAS_utsname=0
if man gethostname >/dev/null ;then HAS_gethostname=1 ; $E 1. gethostname ; which=1; fi
if man utsname     >/dev/null ;then HAS_utsname=1     ; $E 2. utsname     ; which=2; fi
$E -n "Which shall I use? [$which] "
read N
if [ ! -n "$N" ];then N=$which; fi
$E N=$N
if [ $N -eq 1 ];then USE_gethostname=1  ; else USE_gethostname=0  ; fi
if [ $N -eq 2 ];then USE_utsname=1; else USE_utsname=0; fi
$E HAS_gethostname:   $HAS_gethostname
$E USE_gethostname:   $USE_gethostname
$E HAS_utsname: $HAS_utsname
$E USE_utsname: $USE_utsname
$E>>$L "#define HAS_gethostname"   $HAS_gethostname
$E>>$L "#define USE_gethostname"   $USE_gethostname
$E>>$L "#define HAS_utsname" $HAS_utsname
$E>>$L "#define USE_utsname" $USE_utsname

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/* How to determine which directory package to use:"
$E>>$L "*/"
$E "How to determine which directory package to use:"
USE_direct=0
HAS_direct=0
USE_dirent=0
HAS_dirent=0
if [ -f /usr/include/direct.h ];then HAS_direct=1; $E 1. direct; which=1; fi
if [ -f /usr/include/dirent.h ];then HAS_dirent=1; $E 2. dirent; which=2; fi
$E -n "Which shall I use? [$which] "
read N
if [ ! -n "$N" ];then N=$which; fi
$E N=$N
if [ $N -eq 1 ];then USE_direct=1  ; else USE_direct=0  ; fi
if [ $N -eq 2 ];then USE_dirent=1; else USE_dirent=0; fi
$E HAS_direct:   $HAS_direct
$E USE_direct:   $USE_direct
$E HAS_dirent: $HAS_dirent
$E USE_dirent: $USE_dirent
$E>>$L "#define HAS_direct"   $HAS_direct
$E>>$L "#define USE_direct"   $USE_direct
$E>>$L "#define HAS_dirent" $HAS_dirent
$E>>$L "#define USE_dirent" $USE_dirent

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/* Does this system have the socket library?"
$E>>$L "*/"
$E "Does this system have the socket library?"
USE_socket=0
HAS_socket=0
if [ -f /usr/include/sys/socket.h ];then HAS_socket=1; USE_socket=1; $E 1. socket; which=1; fi
#if [ $N -eq 1 ];then USE_socket=1  ; else USE_socket=0  ; fi
$E HAS_socket:   $HAS_socket
$E USE_socket:   $USE_socket
$E>>$L "#define HAS_socket"   $HAS_socket
$E>>$L "#define USE_socket"   $USE_socket

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/* The bcopy funtion:"
$E>>$L "*/"
$E "Test for bcopy ..."
USE_bcopy=0
HAS_bcopy=0
if man bcopy >/dev/null ;then HAS_bcopy=1; $E 1. bcopy; which=1; fi
$E -n "Shall I use the library bcopy? [$which] "
read N
if [ ! -n "$N" ];then N=$which; fi
$E N=$N
if [ $N -eq 1 ];then USE_bcopy=1; else USE_bcopy=0; fi
$E HAS_bcopy: $HAS_bcopy
$E USE_bcopy: $USE_bcopy
$E>>$L "#define HAS_bcopy" $HAS_bcopy
$E>>$L "#define USE_bcopy" $USE_bcopy

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/* The bzero funtion:"
$E>>$L "*/"
$E "Test for bzero ..."
USE_bzero=0
HAS_bzero=0
if man bzero >/dev/null ;then HAS_bzero=1; $E 1. bzero; which=1; fi
$E -n "Shall I use the library bzero? [$which] "
read N
if [ ! -n "$N" ];then N=$which; fi
$E N=$N
if [ $N -eq 1 ];then USE_bzero=1; else USE_bzero=0; fi
$E HAS_bzero: $HAS_bzero
$E USE_bzero: $USE_bzero
$E>>$L "#define HAS_bzero" $HAS_bzero
$E>>$L "#define USE_bzero" $USE_bzero

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/* The strtok funtion:"
$E>>$L "*/"
$E "Test for strtok ..."
USE_strtok=0
HAS_strtok=0
if man strtok >/dev/null ;then HAS_strtok=1; $E 1. strtok; which=1; fi
$E -n "Which shall I use? [$which] "
read N
if [ ! -n "$N" ];then N=$which; fi
$E N=$N
if [ $N -eq 1 ];then USE_strtok=1; else USE_strtok=0; fi
$E HAS_strtok: $HAS_strtok
$E USE_strtok: $USE_strtok
$E>>$L "#define HAS_strtok" $HAS_strtok
$E>>$L "#define USE_strtok" $USE_strtok

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/* Whether to prefer poll() or select():"
$E>>$L "*/"
$E "Whether to prefer poll() or select():"
USE_poll=0
HAS_poll=0
USE_select=0
HAS_select=0
if [ -f /usr/include/sys/poll.h ];then HAS_poll=1; $E 1. poll; which=1; fi
if [ -f /usr/include/sys/select.h ];then HAS_select=1; $E 2. select; which=2; fi
$E -n "Which shall I use? [$which] "
read N
if [ ! -n "$N" ];then N=$which; fi
$E N=$N
if [ $N -eq 1 ];then USE_poll=1  ; else USE_poll=0  ; fi
if [ $N -eq 2 ];then USE_select=1; else USE_select=0; fi
$E HAS_poll:   $HAS_poll
$E USE_poll:   $USE_poll
$E HAS_select: $HAS_select
$E USE_select: $USE_select
$E>>$L "#define HAS_poll"   $HAS_poll
$E>>$L "#define USE_poll"   $USE_poll
$E>>$L "#define HAS_select" $HAS_select
$E>>$L "#define USE_select" $USE_select

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/* Whether to prefer varargs() or stdarg():"
$E>>$L "*/"
$E "Whether to prefer varargs() or stdarg():"
USE_varargs=0
HAS_varargs=0
USE_stdarg=0
HAS_stdarg=0
if man varargs >/dev/null ;then HAS_varargs=1 ; $E 1. varargs; which=1; fi
if man stdarg  >/dev/null ;then HAS_stdarg=1  ; $E 2. stdarg ; which=2; fi
$E -n "Which shall I use? [$which] "
read N
if [ ! -n "$N" ];then N=$which; fi
$E N=$N
if [ $N -eq 1 ];then USE_varargs=1 ; else USE_varargs=0 ; fi
if [ $N -eq 2 ];then USE_stdarg=1  ; else USE_stdarg=0  ; fi
$E HAS_varargs:   $HAS_varargs
$E USE_varargs:   $USE_varargs
$E HAS_stdarg: $HAS_stdarg
$E USE_stdarg: $USE_stdarg
$E>>$L "#define HAS_varargs"   $HAS_varargs
$E>>$L "#define USE_varargs"   $USE_varargs
$E>>$L "#define HAS_stdarg" $HAS_stdarg
$E>>$L "#define USE_stdarg" $USE_stdarg

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
$E>>$L ""
$E>>$L "/*"
$E>>$L "*/"

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Terminate the initial #ifndef:
$E>>$L ""
$E>>$L "#endif"


/bin/ln -f $L local.h
