#!/bin/sh
#
# config ---	Script to configure the Portable Forth Environment
#		automagically. Applicable on UNIX systems.
#
# Unix users: please execute `config' and check if the two files
# generated by this script:
#
#	makefile
#	config.h
#
# describe your system adequately.
# Users of non-Unix systems probably can't execute this script.
# If I did it for them :-) they find the result in the files
#
#	makefile.sys
#	config.sys
#
# where sys is your system.
# Simply rename them to `makefile' and `config.h', then type `make'
# if you have make...
#
# (duz 26Apr94)
#


if [ $# -eq 0 ]
then
  system=`uname`
  makefile=makefile
  config_h=config.h
else
  system=$1
  makefile=makefile.$1
  config_h=config.$1
fi

echo "Configuring pfe for $system." >&2

#########################################################################
# Create the makefile:							#
#########################################################################

if [ -f $makefile ]
then
  echo "Keep makefile in makefile.old." >&2
  mv $makefile makefile.old
fi

echo "Creating $makefile" >&2
exec 1>$makefile

cat << EOF
#
# makefile ---	makefile for portable Forth environment.
#		This file was generated automatically
#		  `date`
#		on machine
#		  `uname -a`
#		by script
#		  $0
#

EOF

case $system in
  "template")
	echo "# A comment indicating the type of system detected."
	echo "SYSTEM=	Identifying id for a -Did option"
	echo "CCANS=	Strict ANSI C-Compiler"
	echo "CCENV=	C-Compiler knowing extensions of your environment"
	echo "OPTS=	Additional compiler options"
	echo "OPTIM=	Optimization flags for your C-compiler"
	echo "DEBUG=	Compiler flags to enable debugging"
	echo "STRIP=	Linker flags to strip symbol table from executable"
	echo "LFLAGS=	Any other linker flags neccessary"
	echo "LIBS=	Additional libraries beyond -lm and -lc"
	echo "TERM_O=	Terminal driver object file"
	echo "SYS_O=	System dependent extensions object file"
	;;
  "Linux")
	echo "# Linux version."
	echo "SYSTEM=	Linux"
	case `uname --machine` in
	  i486) echo "CCANS=	gcc -m486 -pipe -Wall"
		echo "CCENV=	$(CCANS)"
	  	;;
	  *)	echo "CCANS=	gcc -Wall"
		echo "CCENV=	$(CCANS)"
		;;
	esac
	echo "OPTS=	"
	echo "OPTIM=	-O2 -fomit-frame-pointer"
	echo "DEBUG=	-g"
	echo "STRIP=	-s"
	echo "LFLAGS=	-N"
	echo "LIBS=	-ltermcap"
	echo "TERM_O=	termcap.o"
	echo "SYS_O=	unix.o"
	;;
  "FreeBSD")
	echo "# FreeBSD version."
	echo "SYSTEM=	FreeBSD"
	echo "CCANS=	gcc -m486 -pipe -Wall -ansi -pedantic"
	echo "CCENV=	gcc -m486 -pipe -Wall"
	echo "OPTS=	-DBSD"
	echo "OPTIM=	-O2 -fomit-frame-pointer"
	echo "DEBUG=	-g"
	echo "STRIP=	-s"
	echo "LFLAGS=	-static"
	echo "LIBS=	-ltermcap"
	echo "TERM_O=	termcap.o"
	echo "SYS_O=	unix.o"
	;;
  "EMX")
	echo "# EMX version."
	echo "SYSTEM=	EMX"
	echo "CCANS=	gcc -m486 -Wall -ansi -pedantic"
	echo "CCENV=	gcc -m486 -Wall"
	echo "OPTS=	"
	echo "OPTIM=	-O2 -fomit-frame-pointer"
	echo "DEBUG=	-g"
	echo "STRIP=	-s"
	echo "LFLAGS=	"
	echo "LIBS=	-lvideo"
	echo "TERM_O=	term-emx.o"
	echo "SYS_O=	emx.o"
	;;
  "AIX")
       case `uname -m` in
        i386|i486)
                echo "# AIX 1 version."
                echo "SYSTEM=   AIX1"
                echo "CCANS=    gcc"
                echo "CCENV=    gcc"
                echo "OPTS=     "
                echo "OPTIM=    -O2 -fomit-frame-pointer"
                echo "DEBUG=    -g"
                echo "STRIP=    -s"
                echo "LFLAGS=   "
                echo "LIBS=     -lcurses"
                echo "TERM_O=   termcap.o"
                echo "SYS_O=    unix.o"
                ;;
        *)      echo "# AIX 3 version."
                echo "SYSTEM=   AIX3"
                echo "CCANS=    c89"
                echo "CCENV=    cc"
                echo "OPTS=     "
                echo "OPTIM=    -O3"
                echo "DEBUG=    -g"
                echo "STRIP=    -s"
                echo "LFLAGS=   "
                echo "LIBS=     -lcurses"
                echo "TERM_O=   termcap.o"
                echo "SYS_O=    unix.o"
                ;;
        esac
        ;;
  "HP-UX")
	echo "# HP-UX version."
	echo "SYSTEM=	HPUX"
	echo "CCANS=	c89 -Aa +w2"
	echo "CCENV=	c89 -Aa -D_HPUX_SOURCE"
	echo "OPTS=	"
	echo "# Most HP-UX have broken -O or +O2, try later if your's is ok."
	echo "OPTIM=	+O1"
	echo "DEBUG=	-g"
	echo "STRIP=	-s"
	echo "LFLAGS=	"
	echo "LIBS=	-lcurses"
	echo "TERM_O=	termcap.o"
	echo "SYS_O=	unix.o"
	;;
  "ULTRIX")
	echo "# ULTRIX version."
	echo "SYSTEM=	ULTRIX"
	echo "CCANS=	cc"
	echo "CCENV=	cc"
	echo "OPTS=	-DBSD"
	echo "OPTIM=	-O"
	echo "DEBUG=	-g"
	echo "STRIP=	-s"
	echo "LFLAGS=	"
	echo "LIBS=	-ltermlib"
	echo "TERM_O=	termcap.o"
	echo "SYS_O=	unix.o"
	;;
  *)
	echo "I don't know your UNIX system. Using defaults." >&2
	echo "You'll have to check the files config.h and makefile." >&2
	echo "Please read the file install!" >&2
	echo "# Generic UNIX version."
	echo "SYSTEM=	UNIX"
	#
	# check for gcc:
	#
	echo "Please ignore a warning about gcc not found!" >&2
	if gcc -v 2>/dev/null
	then
	  echo "CCANS=	gcc -Wall -ansi"
	  echo "CCENV=	gcc -Wall"
	  #
	  # check gcc version:
	  #
	  gcc -v 2>&1 | grep version | read dummy version
	  case $version in
	    2.*) echo "OPTIM=	-O2 -fomit-frame-pointer" ;;
	    *)   echo "OPTIM=	-O" ;;
	  esac
	else
	  #
	  # assume standard cc:
	  #
	  echo "CCANS=	cc"
	  echo "CCENV=	cc"
	  echo "OPTIM=	-O"
	fi
	#
	# defaults for all other options:
	#
	echo "# uncomment this line if your system is BSD-like:"
	echo "# OPTS=	-DBSD"
	echo "DEBUG=	-g"
	echo "STRIP=	-s"
	echo "LFLAGS=	"
	if [ -f /lib/libtermcap.a -o -f /usr/lib/libtermcap.a ]
	then
	  echo "LIBS=	-ltermcap"
	elif [ -f /lib/libtermc.a -o -f /usr/lib/libtermc.a ]
	then
	  echo "LIBS=	-ltermc"
	elif [ -f /lib/libcurses.a -o -f /usr/lib/libcurses.a ]
	then
	  echo "LIBS=	-lcurses"
	fi
	echo "TERM_O=	termcap.o"
	echo "SYS_O=	unix.o"
esac

echo '


# if you want a final optimized version uncomment these lines:
 CFLAGS =	$(OPTS) $(OPTIM) -D$(SYSTEM)
 LDFLAGS =	$(OPTS) $(STRIP) $(LFLAGS)

# if you want a version for C-level debugging uncomment these lines:
# CFLAGS =	$(OPTS) $(DEBUG) -D$(SYSTEM)
# LDFLAGS =	$(OPTS) $(DEBUG) $(LFLAGS)


#==============================================================================
# dependencies
#==============================================================================

# object files that should be clean ANSI-C:
ANSOBJ =	core.o block.o double.o xception.o facility.o file.o \
		floating.o locals.o memory.o toolkit.o search.o string.o \
		forth-83.o lpf83.o misc.o debug.o dblsub.o support.o \
		dictnry.o vocs.o lined.o term.o version.o

# object files containing environmental dependencies:
ENVOBJ =	main.o $(TERM_O) 4ed.o signals.o sysdep.o $(SYS_O) shell.o \
		yours.o

OBJECTS =	$(ANSOBJ) $(ENVOBJ)

HEADERS =	forth.h const.h types.h macros.h preload.h support.h \
		compiler.h dblsub.h term.h lined.h config.h

all:		pfe

pfe:		$(OBJECTS)
		$(CCENV) $(LDFLAGS) -o pfe $(OBJECTS) $(LIBS) -lm
		rm version.o

CC = $(CCANS)
$(ANSOBJ):	$(HEADERS)

CC = $(CCENV)
$(ENVOBJ):	$(HEADERS)

clean:
		rm -rf pfe core *.o *~ #*#

new:		clean all

veryclean:	clean
		rm -f config.* makefile*

testit:		pfe
		( cd ..; src/pfe testsuite; cd src )
'


#########################################################################
# Create config.h:							#
#########################################################################

case $system in
  EMX) exit 0;;
  # others with fixed config.??
esac

if [ -f $config_h ]
then
  echo "Keep $config_h in config.old." >&2
  mv $config_h config.old
fi

echo "Creating $config_h" >&2
exec 1>$config_h

cat << EOF
/*
 * config.h --	Automatically generated file, don't change.
 *		This one made
 *		  `date`
 *		on
 *		  `uname -a`
 *		by script
 *		  $0
 */

#ifndef __CONFIG_H
#define __CONFIG_H

EOF

if [ -z "$INCDIR" ]; then INCDIR=/usr/include;	fi
if [ -z "$LIBDIR" ]; then LIBDIR=/usr/lib;	fi

echo '#define HOST_SYSTEM "'`uname`'"'

# determine Cell type, byte sex and alignment restrictions:
###########################################################

if cc check_c.c -o check_c >&2
then
  ./check_c
  rm check_c
else
  echo compiling test program failed >&2
  echo aborting. >&2
  rm $config_h
  exit 1
fi

# check for fpos_t
##################

if grep fpos_t /usr/include/stdio.h >/dev/null 2>&1
then :
else
  echo "#define fpos_t long"
fi

# assume ISO 8891 character set:
################################

echo "#define ISO_CHARSET"

# check for hyperbolic functions:
#################################

if grep acosh $INCDIR/math.h >/dev/null 2>&1
then
  echo "#define HAVE_AH_TRIG 1"
fi

# check for memmove:
####################

if grep memmove $INCDIR/string.h >/dev/null 2>&1
then
  echo "#define HAVE_MEMMOVE 1"
else
  echo "void memmove (char *, const char *, unsigned);"
fi

# check for stpcpy:
###################
#
#if grep stpcpy $INCDIR/string.h >/dev/null 2>&1
#then :
#else
#  echo "#define stpcpy(DST,SRC) (strcpy(DST,SRC) + strlen(SRC))"
#fi

# check for strdup:
###################

if grep strdup $INCDIR/string.h >/dev/null 2>&1
then
  echo "#define HAVE_STRDUP 1"
else
  echo "char *strdup (const char *);"
fi

# check for strerror:
#####################

if grep strerror $INCDIR/string.h >/dev/null 2>&1
then :
else
  if grep sys_errlist $INCDIR/stdio.h >/dev/null 2>&1
  then :
  else
    echo "extern char *sys_errlist [];"
  fi
  echo "#define strerror(x) sys_errlist [x]"
fi

# check for sys_siglist[]:
##########################
#
# if grep 'sys_siglist' $INCDIR/signal.h >/dev/null 2>&1
# then
#   echo "#define HAVE_SYS_SIGLIST 1"
# fi

# check for incorrect sprintf:
##############################

if grep 'char.*\*.*sprintf' $INCDIR/stdio.h >/dev/null 2>&1
then
  echo "#define WRONG_SPRINTF"
fi

# check for atexit:
###################

if grep atexit $INCDIR/stdlib.h >/dev/null 2>&1
then
  echo "#define HAVE_ATEXIT 1"
else
  echo "typedef void (*atexit_fp) (void);"
  echo "int atexit (atexit_fp);"
  echo "#define exit(X) trick_exit(X)"
fi

# check for raise:
##################

if grep raise $INCDIR/signal.h $INCDIR/sys/signal.h >/dev/null 2>&1
then
  echo "#define HAVE_RAISE 1"
else
  echo "#define raise(X) kill (getpid (), X)"
fi

# check for siglongjmp:
#######################
#
# if grep siglongjmp $INCDIR/setjmp.h >/dev/null 2>&1
# then
#   echo "#define HAVE_SIGLONGJMP 1"
# else
#   echo "#define sigjmp_buf jmp_buf"
#   echo "#define sigsetjmp(BUF) setjmp (BUF)"
#   echo "#define siglongjmp(BUF,N) longjmp (BUF,N)"
# fi

# check for remove:
###################

if grep remove $INCDIR/stdio.h >/dev/null 2>&1
then
  echo "#define HAVE_REMOVE 1"
else
  echo "#define remove unlink"
fi

# check for rename:
###################

if grep rename $INCDIR/stdio.h >/dev/null 2>&1
then
  echo "#define HAVE_RENAME 1"
else
  echo "int rename (const char *, const char *);"
fi

# check for kind of terminal stuff:
###################################

if [ -f $INCDIR/termios.h ]
then
  echo "#define HAVE_TERMIOS_H 1"
fi

if [ -f $INCDIR/termio.h ]
then
  echo "#define HAVE_TERMIO_H 1"
fi

if [ -f $INCDIR/termcap.h ]
then
  echo "#define HAVE_TERMCAP_H 1"
  for i in $LIBDIR/*termcap*
  do
    if [ $i != "$LIBDIR/*termcap*" ]
    then
      echo "#define HAVE_TERMCAP 1"
      break
    fi
  done
fi

# Time related:
###############

if [ -f $INCDIR/unistd.h ]
then
  if grep usleep $INCDIR/unistd.h >/dev/null 2>&1
  then
    echo "#define HAVE_USLEEP 1"
  fi
  if grep select $INCDIR/unistd.h $INCDIR/time.h \
		 $INCDIR/sys/time.h >/dev/null 2>&1
  then
    echo "#define HAVE_SELECT 1"
  fi
fi

if [ -f $INCDIR/poll.h -o -f $INCDIR/sys/poll.h ]
then
  echo "#define HAVE_POLL 1"
fi


echo "#endif"


echo "config finished" >&2
exit 0
