:
#
# Copyright 1993 Brent Townshend (bst@tc.com)
# Townshend Computer Tools
# Montreal, Quebec
#
# Tue Aug 24 15:51:57 EDT 1993
#
# Revision History: $Log: SysConfig,v $
# Revision 1.23  1996/10/11  19:41:07  bst
# Added 'checkinline' calls for ntohl family.
#
# Revision 1.22  1996/03/26  18:31:37  bst
# Check for bcc32 or bcc
#
# Revision 1.21  1996/03/21  16:44:01  bst
# Use checkvar instead of checklibs for sys_errlist.
#
# Revision 1.20  1995/07/06  17:30:46  bst
# Added netinet/in_systm.h
#
# Revision 1.19  1994/11/17  01:15:38  bst
# Added gethostbyname() support.
#
# Revision 1.18  1994/08/30  19:02:46  bst
# Include sys/types.h when checking for macros.h
# Include rpc/types.h when checking for rpc/xdr.h
#
# Revision 1.17  1994/05/12  18:33:55  bst
# Moved endif to end of syscap.h
#
# Revision 1.16  1994/05/12  14:59:43  bst
# Check for srandom() in math.h
#
# Revision 1.15  1994/05/12  03:07:11  bst
# Check for bzero() prototype in string.h
#
# Revision 1.14  1994/04/25  20:31:34  bst
# Added checks for abs(), labs() macros in macros.h.
# Check for sys/systeminfo.h, sysinfo()
#
# Revision 1.13  1994/04/14  19:22:38  bst
# Use HAS_stdarg instead of HAS_stdarg_h
#
# Revision 1.12  1994/03/07  18:41:38  bst
# Group together socket prototypes.
# Group aio prototypes.
# Added checks for labs(), popen(), pclose(), gethostname(), select()
#
# Revision 1.11  1994/03/04  15:25:31  bst
# Additional checks for DOS
# Don't use -lx options under Borland C
# Force prototypes for ntoh*(), hton*() and str*casecmp()
#
# Revision 1.10  1994/01/03  17:23:11  bst
# Added random(), srandom()
#
# Revision 1.9  1993/11/16  01:56:19  bst
# Added sys_errlist
# ..
#
# Revision 1.8  1993/10/26  16:19:19  bst
# Made 3rd arg to read,write unsigned.
#
# Revision 1.7  1993/10/06  03:32:18  bst
# Look for ntoh* and hton* functions in machine/endian.h as well.
#
# Revision 1.6  1993/10/05  21:46:55  bst
# Added ntoh*, hton* support.
#
# Revision 1.5  1993/09/17  13:21:04  bst
# Added checks for sgtty.h, strchr(), strrchr(),memcpy().
#
# Revision 1.4  1993/08/31  17:24:18  bst
# Added check for sys/timers.h
#
# Revision 1.3  1993/08/30  14:43:24  bst
# Don't use stdarg.h on ULTRIX.
#
# Revision 1.2  1993/08/27  00:26:09  bst
# Use exteranl programs.
#
#
#
# Usage: SysConfig [configuration]
if [ $# = 1 ]
then
    config=$1
else
    config=Configuration
fi

if [ ! -r $config ]
then
    echo "File '"$config"' not found."
    echo "Install must be run before $0."
    exit 1
fi

if [ ! -r include/sysdefs.h ]
then
    echo $0 must be run from root of DAT-Link source directory.
    exit 1
fi
# Determine how to suppress newlines with echo
if [ "`echo -n test`" = "-n test" ]
then
   c='\c'
   n=''
else
   c=''
   n='-n'
fi
export c n
CC=`grep '^CC=' $config | sed -e 's/.*= *//' -e 's/ *#.*//'`
CCFLAGS=`grep '^CCFLAGS=' $config | sed -e 's/.*= *//' -e 's/ *#.*//'`
EXTRALIBS=`grep '^EXTRALIBS=' $config | sed -e 's/.*= *//' -e 's/ *#.*//'`

export CC CCFLAGS EXTRALIBS
rm -f SysConfig.errs
rm -f sysdecls.new syscap.new

if [ $CC = bcc -o $CC = bcc32 ]
then
    # Using Borland C
    mathlib=
    curseslib=
else
    mathlib=-lm
    curseslib=-lcurses
    # Make sure scripts are on PATH
    PATH=./scripts:$PATH
    export PATH
fi

echo '#ifndef SYSDECLS_H' >sysdecls.new
echo '#define SYSDECLS_H' >>sysdecls.new
echo '#ifndef SYSCAP_H' >syscap.new
echo '#define SYSCAP_H' >>syscap.new
echo $n "Checking system headers...$c"

DEFHEADERS="stdio.h sys/types.h"	
export DEFHEADERS
checkheader stdlib.h && DEFHEADERS="$DEFHEADERS stdlib.h"
checkheader unistd.h && DEFHEADERS="$DEFHEADERS unistd.h"
checkheader io.h && DEFHEADERS="$DEFHEADERS io.h"
checkheader alloc.h
checkheader malloc.h
checkheader memory.h
checkheader time.h
checkheader syslog.h
checkheader sys/time.h
checkheader sys/timeb.h time.h
checkheader sys/resource.h sys/time.h
checkheader dirent.h
checkheader sys/dir.h sys/types.h
checkheader sys/asynch.h
checkheader macros.h sys/types.h
checkheader bstring.h
checkheader getopt.h
checkheader assert.h
checkheader timers.h
checkheader sys/timers.h
checkheader sgtty.h
checkheader netinet/in.h sys/types.h
checkheader netinet/in_systm.h sys/types.h
checkheader machine/endian.h
checkheader curses.h
checkheader rpc/types.h
checkheader rpc/xdr.h rpc/types.h
checkheader string.h
checkheader sys/socket.h sys/types.h
checkheader sys/systeminfo.h
checkheader sys/select.h
checkheader netdb.h

echo done.
echo $n "Checking library functions present...$c"

checklibs _doprnt
checklibs _flsbuf
checkdefined abs math.h macros.h || checklibs abs
checkdefined labs math.h macros.h || checklibs labs
checklibs bzero
checklibs getopt
checklibs gettimeofday
checkdefined htonl netinet/in.h machine/endian.h || checkinline htonl netinet/in.h machine/endian.h || checklibs htonl
checkdefined htons netinet/in.h machine/endian.h || checkinline htons netinet/in.h machine/endian.h || checklibs htons
checklibs isinf $mathlib
checklibs isnan $mathlib
checklibs memset
checkdefined ntohl netinet/in.h machine/endian.h || checkinline nothl netinet/in.h machine/endian.h || checklibs ntohl
checkdefined ntohs netinet/in.h machine/endian.h || checkinline noths netinet/in.h machine/endian.h || checklibs ntohs
checkdefined strchr string.h || checklibs strchr
checkdefined strrchr string.h || checklibs strrchr
checkdefined memcpy memory.h || checklibs memcpy
checkdefined noecho curses.h || checklibs noecho $curseslib
checklibs openlog
checklibs random
checklibs srandom
checklibs setpriority
checklibs setsid
checklibs strcasecmp
checklibs strncasecmp
checklibs syslog
checkdefined tell || checklibs tell
checklibs usleep
checklibs vfprintf
checklibs vsyslog
checklibs vsprintf
checklibs xdr_free
checklibs stty
checklibs aiowrite
checklibs aioread
checklibs aiowait
checklibs ftime
checklibs getclock
checkvar sys_errlist
checklibs popen
checklibs pclose
checklibs gethostname
checklibs select
checklibs sysinfo
checklibs gethostbyname
echo done.

echo $n "Checking varargs/stdargs implementation...$c"
checkvarargs
echo done
echo '#endif /* !SYSCAP_H */' >>syscap.new

echo $n "Checking for function prototypes...$c"
declare "" char _flsbuf '(char,FILE*)'
declare "macros.h math.h" int abs '(int)'
declare "macros.h math.h" long labs '(long)'
declare "" double atof '(const char *)'
declare "bstring.h string.h" void bzero '(void *,size_t)'
declare "" int close '(int)'
declare "" void exit '(int)'
declare "" int fclose '(FILE*)'
declare "" int fflush '(FILE*)'
declare "" int fgetc '(FILE*)'
declare "" int fprintf '(FILE*,const char *fmt,...)'
declare "" int fputc '(int,FILE*)'
declare "" size_t fread '(void *ptr, size_t size, size_t nitems, FILE *stream)'
declare "" int fscanf '(FILE*,const char*,...)'
declare "" int fseek '(FILE*,long,int)'
declare "" long ftell '(FILE*)'
declare "" size_t fwrite '(const void *ptr, size_t size, size_t nitems,FILE *stream)'
declare "" char* getenv '(const char *)'
declare "" int gethostname '(char *name, int namelen)'
declare -f "getopt.h" int getopt '(int argc,char *argv[],const char *opts)'
declare "" int ioctl '(int,int,char*)'
declare "math.h" int isinf '(double)'
declare "math.h" int isnan '(double)'
declare "" long lseek '(int fd,long offset, int whence)'
declare "malloc.h" void* malloc '(size_t)'
declare "string.h memory.h" int memcmp '(const void *,const void *,size_t)'
declare "string.h memory.h" void* memcpy '(void *,const void *,size_t)'
declare "memory.h" void* memset '(void *,int, size_t)'
declare -f "sys/types.h netinet/in.h machine/endian.h" 'unsigned short' ntohs '(unsigned int)'
declare -f "sys/types.h netinet/in.h machine/endian.h" 'unsigned short' htons '(unsigned int)'
declare -f "sys/types.h netinet/in.h machine/endian.h" 'unsigned int32' ntohl '(unsigned int32)'
declare -f "sys/types.h netinet/in.h machine/endian.h" 'unsigned int32' htonl '(unsigned int32)'
declare "syslog.h" void openlog '(const char *ident, int logopt, int facility)'
declare "" int pclose '(FILE *stream)'
declare "" void perror '(const char*)'
declare "" FILE* popen '(const char *cmd, const char *type)'
declare "" int printf '(const char *,...)'
declare "" int putenv '(const char *)'
declare "" long random '()'
declare "" int read '(int fd, char *buf, unsigned int len)'
declare "alloc.h" void* sbrk '(size_t)'
declare "" int scanf '(const char*,...)'
declare "sys/time.h sys/resource.h" int setpriority '(int,int,int)'
declare "" int setsid '()'
declare "math.h" void srandom '(int seed)'
declare "" int sscanf '(const char *,const char*,...)'
declare -f "string.h" int strcasecmp '(const char *s1, const char *s2)'
declare -f "string.h" int strncasecmp '(const char *s1, const char *s2, size_t)'
declare "syslog.h" int syslog '(int priority, const char *format,...)'
declare "" int system '(const char*)'
declare "" long tell '(int fd)'
declare "" int unlink '(const char *)'
declare "" int usleep '(long)'
declare "" int write '(int fd, const char *buf, unsigned int len)'
declare "sys/systeminfo.h" long sysinfo '(int cmd, char *buf, long cnt)'

if grep 'HAS_sys_socket_h$' syscap.new >/dev/null
then
    declare "sys/socket.h" int accept '(int s,void *addr,int *addrlen)'
    declare "sys/socket.h" int bind '(int s, const void *name, int namelen)'
    declare "sys/socket.h" int connect '(int s, const void *name, int namelen)'
    declare "sys/socket.h" int getsockname '(int s, void *name, int *namelen)'
    declare "sys/socket.h" int getsockopt '(int s,int level,int optname, void *optval,int *optlen)'
    declare "sys/socket.h" int listen '(int s, int backlog)'
    declare "sys/socket.h" int setsockopt '(int s,int level,int optname, const void *optval,int optlen)'
    declare "sys/socket.h" int socket '(int domain, int type, int protocol)'
fi

echo "#ifdef NEEDS_NETDB" >>sysdecls.new
declare "netdb.h" 'struct hostent *' gethostbyname '(const char *)'
echo "#endif /* NEEDS_NETDB */" >>sysdecls.new

echo "#ifdef NEEDS_XDR" >>sysdecls.new
declare "rpc/xdr.h" int xdr_free '(xdrproc_t proc, char *objp)'
echo "#endif /* NEEDS_XDR */" >>sysdecls.new

echo "#ifdef NEEDS_VA" >>sysdecls.new
declare "" int _doprnt '(const char *format, va_list ap, FILE *stream)'
declare "" int vfprintf '(FILE *, const char *format, va_list ap)'
declare "" int vsprintf '(char *buf, const char *format, va_list ap)'
declare "syslog.h" int vsyslog '(int priority, const char *fmt, va_list ap)'
echo "#endif /* NEEDS_VA */" >>sysdecls.new

echo "#ifdef NEEDS_TIME" >>sysdecls.new
declare "time.h" char* ctime '(const time_t *tm)'
declare "time.h sys/timeb.h" int ftime '(struct timeb *)'
declare "timers.h" int getclock '(int clock_type, struct timespec *tp)'
declare "time.h sys/time.h" int gettimeofday '(struct timeval *tp, struct timezone *tzp)'
declare "sys/time.h sys/resource.h" int select '(int width,fd_set *readfds,fd_set *writefds,fd_set *exceptfds, struct timeval *Timeout)'
declare "time.h" time_t time '(time_t *tm)'
echo "#endif /* NEEDS_TIME */" >>sysdecls.new

if grep 'HAS_curses_h$' syscap.new >/dev/null
then
    echo "#ifdef NEEDS_CURSES" >>sysdecls.new
    declare curses.h int endwin '(void)'
    declare curses.h int mvprintw '(int,int,char*,...)'
    declare curses.h int noecho '(void)'
    declare curses.h int printw '(char*,...)'
    echo "#ifdef ISMACRO_noecho" >>sysdecls.new
    # noecho is sometimes declared as stty, but don't always include because
    # of sgttyb struct, which may not be defined.
    declare curses.h int stty '(int fd, struct sgttyb *buf)'
    echo "#endif /* ISMACRO_noecho */" >>sysdecls.new
    declare curses.h int waddch '(WINDOW*,char)'
    declare curses.h int wclrtoeol '(WINDOW*)'
    declare curses.h int wmove '(WINDOW*,int,int)'
    declare curses.h int wrefresh '(WINDOW*)'
    echo "#endif /* NEEDS_CURSES */" >>sysdecls.new
fi

if grep 'HAS_aioread$' syscap.new >/dev/null
then
    echo "#ifdef NEEDS_ASYNCH" >>sysdecls.new
    declare sys/asynch.h int aioread '(int fd, char *bufp, int bufs, int offset, int whence,aio_result_t *resultp)'
    declare sys/asynch.h aio_result_t* aiowait '(struct timeval *timeout)'
    declare sys/asynch.h int aiowrite '(int fd, const char *bufp, int bufs, int offset, int whence,aio_result_t *resultp)'
    echo "#endif /* NEEDS_ASYNCH */" >>sysdecls.new
fi
echo done.

echo '#endif /* !SYSDECLS_H */' >>sysdecls.new

# Install files if they have changed
if cmp -s syscap.new include/syscap.h
then
    echo Current version of syscap.h unchanged.
else
    [ -r include/syscap.h ] && mv include/syscap.h include/syscap.old
    mv syscap.new include/syscap.h
    echo Installed new version of syscap.h in include directory.
fi

if cmp -s sysdecls.new include/sysdecls.h
then
    echo Current version of sysdecls.h unchanged.
else
    [ -r include/sysdecls.h ] && mv include/sysdecls.h include/sysdecls.old
    mv sysdecls.new include/sysdecls.h
    echo Installed new version of sysdecls.h in include directory.
fi
echo Configuration complete.
exit 0
