#!/bin/sh
#
# ~ppr/src/Configure
# Copyright 1995, 1996, 1997, Trinity College Computing Center.
# Written by David Chappell.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation.  This software is provided "as is" without express or
# implied warranty.
#
# Last modified 2 April 1997.
#

# Find an echo that will support "echo -n"
findNECHO ()
    {
    for i in echo /bin/echo /usr/ucb/echo
	do
	if [ -x $i ]
	    then
	    if [ `$i -n | wc -l` -eq 0 ]
		then
		echo $i
		return
		fi
	    fi
	done
    echo echo
    }
NECHO=`findNECHO`

# Function to find a program
findprog ()
    {
    echo "Searching for $1" >&2
    for i in `echo $PATH | tr ':' ' '`
	do
	if [ `echo $i | cut -c1` = '/' ]
	    then
	    # echo "Trying $i/$1" >&2
	    if [ -x "$i/$1" ]
		then
		echo "Found $i/$1" >&2
		echo "$i/$1"
		return
		fi
	    fi
	done
    echo "Not found" >&2
    }

#
# Present the user with a menu
#
clear
echo
echo "What operating system is this?"
echo
echo "1) System V Release 4 for AT&T WGS series 80[34]86"
echo
echo "2) SunOS 5.x (Solaris 2.x)"
echo
echo "3) Linux (Principle development platform)"
echo
echo "4) NetBSD (Probably broken)"
echo
echo "5) SunOS 4.1.3_U1 (Not one test report received)"
echo
echo "6) OSF/1 3.2 (DEC Alpha)"
echo
echo "7) SGI IRIX 6.3 (Very new, little testing)"
echo
echo "8) Cygnus Win32 (Doesn't work)"
echo 
$NECHO -n "? "
read answer

#
# If the answer was valid, set the SYSTEM variable to accord
# with the answer.
#
case "$answer" in
	1 )
		SYSTEM=SYSVR4
		ACTUAL_SYSTEM=""
		;;
	2 )
		SYSTEM=SUNOS5
		ACTUAL_SYSTEM=`uname -sr | sed -e 's/^SunOS 5\..*$/SUNOS5/'`
		;;
	3 )
		SYSTEM=LINUX
		ACTUAL_SYSTEM=`uname -s | sed -e 's/^Linux$/LINUX/'`
		;;
	4 )
		SYSTEM=NETBSD
		ACTUAL_SYSTEM=""
		;;
	5 )
		SYSTEM=SUNOS4
		ACTUAL_SYSTEM=""
		;;
	6 )
		SYSTEM=OSF1
		ACTUAL_SYSTEM=`uname -s`
		;;
    	7 )
    	    	SYSTEM=IRIX
    	    	ACTUAL_SYSTEM=`uname -s`
    	    	;;
	8 )
		SYSTEM=WIN32
		ACTUAL_SYSTEM=""
		;;
	* )
		echo "That is not a valid choice."
		exit 1
		;;
	esac

if [ -n "$ACTUAL_SYSTEM" -a "$SYSTEM" != "$ACTUAL_SYSTEM" ]
  then
  echo
  echo "This system does not appear to be a $SYSTEM system,"
  echo "it appears to be a $ACTUAL_SYSTEM system."
  echo
  echo "Press RETURN to continue anyway."
  read junk
  fi

clear
echo "Good, will set up to compile for operating system "$SYSTEM"."
echo

# If this is a system for which we support the native C compiler,
# ask the user if he wants to use it.
NATIVE_CC="no"
if [ $SYSTEM = OSF1 -o $SYSTEM = IRIX ]
  then
  echo "Normally, GCC is used to compile PPR.  For this operating system"
  echo "you have the option of using the C compiler that comes with the"
  echo "operating system (if you have it)."
  echo
  NATIVE_CC="?"
  while [ "$NATIVE_CC" = "?" ]
    do
    $NECHO -n "Use native cc? "
    read NATIVE_CC
    case "$NATIVE_CC" in
	[yY]* )
	    NATIVE_CC="yes"
	    ;;
	[nN]* )
	    NATIVE_CC="no"
	    ;;
	* )
	    NATIVE_CC="?";
	    ;;
        esac
    done
  fi

if [ "$NATIVE_CC" = "yes" ]
  then
  echo "The system's own C compiler will be used."
  else
  echo "GCC will be used."
  fi
echo

#
# Ask about gdbm:
#
echo "Do you wish PPR to use the GNU database library (GDBM)?"
echo "(If you say no, the user database and conversion of"
echo "MS-Windows TrueType fonts to PostScript fonts will not"
echo "work.  See the ppuser(8) and indexttf(8) man pages for"
echo "details.  If you don't have GDBM and say no here, you"
echo "probably will never miss these features.)"
echo
USE_GDBM="?"
while [ "$USE_GDBM" = "?" ]
    do
    $NECHO -n "Use gdbm? [yes] "
    read USE_GDBM
    case "$USE_GDBM" in
	[yY]* )
	    USE_GDBM="yes"
	    ;;
	[nN]* )
	    USE_GDBM="no"
	    ;;
	'' )
	    USE_GDBM="yes"
	    ;;
	* )
	    USE_GDBM="?";
	    ;;
        esac
done

if [ "$USE_GDBM" = "yes" ]
  then
  echo "PPR will use GDBM."
  else
  echo "PPR features which require GDBM will be disabled."
  fi
echo

#
# Ask the user if he would like to change any of the PPR
# install directories.
#
echo "Please choose where to install PPR.  It is suggested that"
echo "you accept the defaults as all the documentation assumes"
echo "that you are using the default directories."
echo
$NECHO -n "PPR home directory [/usr/ppr]: "
read HOMEDIR
if [ -z "$HOMEDIR" ]; then HOMEDIR="/usr/ppr"; fi
echo
$NECHO -n "PPR configuration directory [/etc/ppr]: "
read CONFDIR
if [ -z "$CONFDIR" ]; then CONFDIR="/etc/ppr"; fi
echo
$NECHO -n "PPR spool files directory [/var/spool/ppr]: "
read VAR_SPOOL_PPR
if [ -z "$VAR_SPOOL_PPR" ]; then VAR_SPOOL_PPR="/var/spool/ppr"; fi
echo
$NECHO -n "PPR temporary files directory [/tmp]: "
read TEMPDIR
if [ -z "$TEMPDIR" ]; then TEMPDIR="/tmp"; fi
echo
$NECHO -n "Directory for symbolic links to PPR binaries [/usr/bin]: "
read BINDIR
if [ -z "$BINDIR" ]; then BINDIR="/usr/bin"; fi
echo

#
# Ask where the X Windows binaries are
#
echo
echo
echo "You must indicate the directory where X-Windows binaries such as"
echo "xterm may be found.  It is recomended that if the default is not"
echo "correct, you accept it anyway and make a symbolic link from"
echo "your X-Windows bin directory, (/usr/openwin/bin, /usr/X11R6/bin,"
echo "etc.) to /usr/bin/X11."
echo
$NECHO -n "X-Windows bin [/usr/bin/X11]: "
read XWINBINDIR
if [ -z "$XWINBINDIR" ]; then XWINBINDIR="/usr/bin/X11"; fi
echo

#
# Ask about A4
#
echo
echo
echo "Do you want the banner pages, the line printer emulator, the dot"
echo "matrix printer emulator, the hex dump filter, and the fortran"
echo "filter compiled to format their pages for A4 paper instead of"
echo "U.S. letter?  (Note:  A4 paper is not to be had in the U.S., so"
echo "I have no way of testing this.)"
echo
A4="?"
while [ "$A4" = "?" ]
    do

    # prompt
    $NECHO -n "Use A4? [no] "
    read A4

    # if yes
    if echo $A4 | grep '^[yY]' >/dev/null
	then
	A4=""		# don't comment out
	else
	# if explicit no
	if echo $A4 | grep '^[nN]' >/dev/null
	    then
	    A4="#"	# comment out
	    else
	    # blank line, implicit no
	    if [ -z "$A4" ]
		then
		A4="#"	# comment out
		# unrecognized
		else
		A4="?"
		fi
	    fi
	fi
    done

#
# Look for various AppleTalk libraries
#
echo
echo "Looking for the AT&T/NCR AppleTalk Network Program."
LIBSANP="";
if [ -r /usr/lib/libanp.so ]
	then
	echo "Found \"/usr/lib/libanp.so\"."
	USE_ATALK="yes"
	ATALKTYPE="ali"
	LIBSANP="-lanp"
	else
	echo "Not found"
	fi

echo
echo "Looking for the Columbia AppleTalk Program."
LIBSCAP="";
for dir in /usr/lib /usr/local/lib /usr/local/cap/lib /usr/local/lib/cap
	do
	if [ -r $dir/libcap.a ]
		then
		echo "Found \"$dir/libcap.a\"."
		USE_ATALK="yes"
		ATALKTYPE="cap"
		LIBSCAP="$dir/libcap.a"
		fi
	done
if [ -z "$LIBSCAP" ]
	then
	echo "Not found."
	fi
echo

echo "Looking for NATALI/Netatalk..."
LIBSNATALI=""
NATALI=""
NETATALK=""
NATALI_NETATALK_FOUND=""
for i in /usr/lib/libatalk.a /usr/local/lib/libatalk.a /usr/local/atalk/lib/libatalk.a
	do
	if [ -r $i ]
		then
		echo "Found \"$i\"."
		NETATALK="$i"
		fi
	done
for i in /usr/lib/libnatali.a /usr/local/lib/libnatali.a
	do
	if [ -r $i ]
		then
		echo "Found \"$i\"."
		NATALI="$i"
		fi
	done
if [ -n "$NATALI" -a -n "$NETATALK" ]
	then
	USE_ATALK="yes"
	ATALKTYPE="ali"
	LIBSNATALI="$NATALI $NETATALK"
	else
	if [ -z "$NATALI" ]; then echo "NATALI not found."; fi
	if [ -z "$NETATALK" ]; then echo "Netatalk not found."; fi
	fi
echo

if [ `echo "$LIBSANP $LIBSCAP $NATALI_NETATALK_FOUND" | wc -w` -gt 1 ]
    then
    echo "You have more than one AppleTalk library.  Which do you wish to use?"
    echo
    if [ -n "$LIBSANP" ]; then echo "1) ANP"; echo; fi
    if [ -n "$LIBSCAP" ]; then echo "2) CAP"; echo; fi
    if [ -n "$LIBSNATALI" ]; then echo "3) NATALI"; echo; fi
    answer=""
    while [ -z "$answer" ]
	do
	read answer
	case "$answer" in
	    1 )
		ATALKTYPE="ali"
		LIBSCAP=""
		LIBSNATALI=""
		;;
	    2 )
		ATALKTYPE="cap"
		LIBSANP=""
		LIBSNATALI=""
		;;
	    3 )
		ATALKTYPE="ali"
		LIBSANP=""
		LIBSCAP=""
		;;
	    * )
		answer=""
		;;
	    esac
	done
    fi

#
# See if we can find uncompress or gunzip
#
GUNZIP=`findprog gunzip`
if [ -z "$GUNZIP" ]
    then
    UNCOMPRESS=`findprog uncompress`
    else
    UNCOMPRESS=""
    fi
echo

#========================================================================
# Start of global.mk builder code.
#========================================================================

echo "Building global.mk..."
exec 3>include/global.mk

#========================================================================
echo "    Building header..."
cat >&3 <<===EndOfHere1===
#
# ~ppr/src/include/global.mk
# Generated by Configure on `date`.
#
# You may want to change some of this if it doesn't work on 
# your system.
#

# Directory for program files and such.
# Default is /usr/ppr.
HOMEDIR=$HOMEDIR

# Directory into which to install configuration files.
# Default is /etc/ppr.
CONFDIR=$CONFDIR

# Directory for spool files.
# Default is /var/spool/ppr.
VAR_SPOOL_PPR=$VAR_SPOOL_PPR

# Directory for temporary files.
# Default is /tmp.
TEMPDIR=$TEMPDIR

# Directory for symbolic links to PPR executables
# Default is /usr/bin.
BINDIR=$BINDIR

# Directory were we will find X-Windows programs
# Default is /usr/bin/X11.
XWINBINDIR=$XWINBINDIR

===EndOfHere1===

echo "# If all of the above have their default values then," >&3
echo "# this line may be commented out." >&3
if [ $HOMEDIR = "/usr/ppr" -a $CONFDIR = "/etc/ppr" \
		-a $VAR_SPOOL_PPR = "/var/spool/ppr" \
		-a $TEMPDIR = "/tmp" -a $BINDIR = "/usr/bin" \
		-a $XWINBINDIR = "/usr/bin/X11" ]
	then
	$NECHO -n "#" >&3
	fi
echo 'CHANGE=-DHOMEDIR=\"$(HOMEDIR)\" -DCONFDIR=\"$(CONFDIR)\" -DVAR_SPOOL_PPR=\"$(VAR_SPOOL_PPR)\" -DTEMPDIR=\"$(TEMPDIR)\" -DXWINBINDIR=\"$(XWINBINDIR)\"' >&3
echo >&3

cat >&3 <<===EndOfHere2===
# Do we want A4?
${A4}A4=-DA4

# Shell script which will modify shell and perl
# scripts to use the above values.  There is no
# reason to change this.
INSTALLSCRIPT=../installprogs/installscript.sh

===EndOfHere2===

echo "# Programs for uncompressing files.  This variable is used when" >&3
echo "# building ppr/ppr_infile.c." >&3
if [ -n "$GUNZIP" ]
    then
    echo "UNCOMPRESS_OPTS=-DGUNZIP=\\\"$GUNZIP\\\"" >&3
    else
    if [ -n "$UNCOMPRESS" ]
	then
	echo "UNCOMPRESS_OPTS=-DUNCOMPRESS=\\\"$UNCOMPRESS\\\"" >&3
	else
	echo "UNCOMPRESS_OPTS=" >&3
	fi
    fi
echo >&3

if [ "$USE_GDBM" = "yes" ]
then
cat >&3 <<===EndOfHere3===
# Decide which user database module to use
USER_DBM=gdbm
#USER_DBM=none

# Decide which database library to use for the TT font database
TT_DBM=gdbm
#TT_DBM=none

# Libraries which should be linked in for the user database:
DBLIBS=../libpprdb.a -lgdbm
#DBLIBS=../libpprdb.a

===EndOfHere3===
else
cat >&3 <<===EndOfHere4===
# Decide which user database module to use
#USER_DBM=gdbm
USER_DBM=none

# Decide which database library to use for the TT font database
#TT_DBM=gdbm
TT_DBM=none

# Libraries which should be linked in for the user database:
#DBLIBS=../libpprdb.a -lgdbm
DBLIBS=../libpprdb.a

===EndOfHere4===
fi

#========================================================================
echo "    Selecting a system dependent section..."

if [ $SYSTEM = "SYSVR4" ]
then
echo "      Emmiting section for AT&T System V release 4"
cat >&3 <<'===EndSYSVR4==='
#==========================================================
# AT&T System V release 4 for 80[[34]]86 WGS microcomputers,
# possibly with the AT&T/NCR AppleTalk Network Program.
# This also offers support for client printing using
# AT&T Streams NetBIOS.
#==========================================================
MAKE=make
STRIP=strip
CP=cp
RMF=rm -f
OBJ=o
DOTEXE=
LIBEXT=a
LIBCMD=ar -sq
CC=gcc
CFLAGS=-Wall -O2 -I ../include $(CHANGE) $(A4)
CPP=gcc -E -P -xc-header
SOCKLIBS=-lsocket -lnsl -lresolv
LEX=lex
LIB_LEX=-ll
MAILX=mailx

# If you don''t have NetBIOS, comment these out.
ALLNETBIOS=allnetbios
INSTALLNETBIOS=installnetbios
===EndSYSVR4===
fi

if [ $SYSTEM = "SUNOS5" ]
then
echo "      Emmiting section for SunOS 5.x (Solaris 2.x)"
cat >&3 <<'===EndSUNOS5==='
#=====================================================
# For SunOS 5.x (Solaris 2.x) with CAP 6.0.
#
# Notice that unlike in the section above, the client
# spooling program which depends on streams NetBIOS
# is not built.  The client spooling program does
# compile properly, but I don't have NetBIOS for this
# operating system and so can't test it.
#
# This port too has been in regular use.
#=====================================================
MAKE=make
STRIP=strip
CP=cp
RMF=rm -f
OBJ=o
DOTEXE=
LIBEXT=a
LIBCMD=ar -q
CC=gcc
CFLAGS=-Wall -O2 -I ../include $(CHANGE) $(A4)
CPP=gcc -E -P -xc-header
SOCKLIBS=-lsocket -lnsl -lresolv
LEX=lex
LIB_LEX=-ll
MAILX=mailx
===EndSUNOS5===
fi

if [ $SYSTEM = "NETBSD" ]
then
echo "      Emmiting section for NetBSD"
cat >&3 <<'===EndNETBSD==='
#===================================================
# For NetBSD 1.0 with Gnu Make and CAP 6.0
# Not tested recently.
#===================================================
MAKE=gmake
STRIP=strip
CP=cp
RMF=rm -f
OBJ=o
DOTEXE=
LIBEXT=a
LIBCMD=ar -q
CC=gcc
CFLAGS=-Wall -O2 -I ../include $(CHANGE) $(A4)
CPP=gcc -E -P -xc-header
SOCKLIBS=
LEX=lex
LEX_LIB=-ll
MAILX=mail
===EndNETBSD===
fi

if [ $SYSTEM = "LINUX" ]
then
echo "      Emmiting section for Linux"
cat >&3 <<'===EndLINUX==='
#=======================================================
# For Linux with CAP 6.0 or NATALI and Netatalk.
#
# This has been tested with Slackware 2.1.  PostScript
# errors will not be detected unless you replace the 
# Linux kernel with a later one, such as 1.1.85.
#
# Later kernels, such as 1.2.13 also work.  1.3.x 
# kernels have trouble with TCP/IP connexions to
# Extended Systems Pocket Print Servers but work
# otherwise.  See the options for the "tcpip" interface
# in ppad(8) for a kludge.
#=======================================================
MAKE=make --no-print-directory 
STRIP=strip
CP=cp
RMF=rm -f
OBJ=o
DOTEXE=
LIBEXT=a
LIBCMD=ar -crs
CC=gcc
CFLAGS=-Wall -O2 -fno-strength-reduce -fomit-frame-pointer -m486 -I ../include $(CHANGE) $(A4)
CPP=gcc -E -P -xc-header
SOCKLIBS=
LEX=flex
LEX_LIB=-lfl
MAILX=mail

# These have unusual values in Linux.  The shell script
# "installscript" will insert these values in shell scripts
# when it installs them.
SIGUSR1=10
SIGUSR2=12
===EndLINUX===
fi

if [ $SYSTEM = "SUNOS4" ]
then
echo "      Emmiting section for SunOS 4.1.3_U1"
cat >&3 <<'===EndSUNOS4==='
#=========================================================
# For SunOS 4.1.3_U1 with CAP 6.0 or NATALI and Netatalk.
# This is untested.
#=========================================================
MAKE=make
STRIP=strip
CP=cp
RMF=rm -f
OBJ=o
DOTEXE=
LIBEXT=a
LIBCMD=ar -q
CC=gcc
CFLAGS=-Wall -O2 -fomit-frame-pointer -I ../include $(CHANGE) $(A4)
CPP=gcc -E -P -xc-header
SOCKLIBS=
LEX=lex
LEX_LIB=-ll
MAILX=mail
===EndSUNOS4===
fi

if [ $SYSTEM = "OSF1" ]
then
echo "      Emmiting section for OSF/1 3.2"
cat >&3 <<'===EndOSF1==='
#=====================================================
# For OSF/1 3.2 DEC Alpha, possibly with CAP 6.0.
#=====================================================
MAKE=make
STRIP=strip
CP=cp
RMF=rm -f
OBJ=o
DOTEXE=
LIBEXT=a
LIBCMD=ar -q
===EndOSF1===
if [ "$NATIVE_CC" = "yes" ]
then
echo "CC=`pwd`/misc/fixed_cc_osf" >&3
cat >&3 <<'===EndOSF2==='
CFLAGS=-O2 -I ../include $(CHANGE) $(A4)
CPP=/lib/cpp
===EndOSF2===
else
cat >&3 <<'===EndOSF3==='
CC=gcc
CFLAGS=-Wall -O2 -I ../include $(CHANGE) $(A4)
CPP=gcc -E -P -xc-header
===EndOSF3===
fi
cat >&3 <<'===EndOSF4==='
# Steve Swinnea <swinnea@weiss.che.utexas.edu> says that it is sometimes 
# necessary to link in mkfifo(), pipe(), waitpid(), and possibly sigaction()
# from this libraries.
OSFLIBS=-lsys5
SOCKLIBS=
LEX=lex
LEX_LIB=-ll
MAILX=mailx
===EndOSF4===
fi

if [ $SYSTEM = "IRIX" ]
then
echo "      Emmiting section for IRIX 6.3"
cat >&3 <<'===EndIRIX==='
#=====================================================
# SGI IRIX 6.3
#=====================================================
MAKE=make
STRIP=strip
CP=cp
RMF=rm -f
OBJ=o
DOTEXE=
LIBEXT=a
LIBCMD=ar -q
===EndIRIX===
if [ "$NATIVE_CC" = "yes" ]
then
echo "CC=cc" >&3
cat >&3 <<'===EndIRIX2==='
CFLAGS=-O2 -I ../include $(CHANGE) $(A4)
CPP=/lib/cpp
===EndIRIX2===
else
cat >&3 <<'===EndIRIX3==='
CC=gcc
CFLAGS=-Wall -O2 -I ../include $(CHANGE) $(A4)
CPP=gcc -E -P -xc-header
===EndIRIX3===
fi
cat >&3 <<'===EndIRIX4==='
SOCKLIBS=
LEX=lex
LEX_LIB=-ll
MAILX=mailx
===EndIRIX4===
fi

if [ $SYSTEM = "WIN32" ]
then
echo "      Emmiting section for WIN32"
cat >&3 <<'===EndWIN32==='
#=======================================================
# For Cygnus WIN32.
#=======================================================
MAKE=make --no-print-directory 
STRIP=strip
CP=cp
RMF=rm -f
OBJ=o
DOTEXE=.exe
LIBEXT=a
LIBCMD=ar -crs
CC=gcc
CFLAGS=-Wall -O2 -fomit-frame-pointer -m486 -I ../include $(CHANGE) $(A4)
CPP=gcc -E -P -xc-header
SOCKLIBS=
LEX=flex
LEX_LIB=-lfl
MAILX=mail

SIGUSR1=18
SIGUSR2=19
===EndWIN32===
fi

#====================================================
# Start of AppleTalk settings.
#====================================================
echo "    Selecting AppleTalk support..."
cat >&3 <<===EndATALK1===

===EndATALK1===

if [ -n "$USE_ATALK" ]
then
cat >&3 <<===EndATALK2===
# Since we are using AppleTalk, indicate the type
# and enable building and installing of the programs.
ATALKTYPE=$ATALKTYPE
ALLATALK=allatalk
INSTALLATALK=installatalk
#
# The libraries we must include for AppleTalk
===EndATALK2===

if [ -n "$LIBSALI" ]
    then
    echo "      AT&T ALI"
    echo >&3 "ATALKLIBS=LIBSALI"
    else
    if [ -n "$LIBSCAP" ]
	then
	echo "      CAP60"
	echo "ATALKLIBS=$LIBSCAP \$(SOCKLIBS)" >&3
	else
	if [ -n "$LIBSNATALI" ]
	    then
	    echo "      NATALI/Netatalk"
	    echo "ATALKLIBS=$LIBSNATALI" >&3
	    fi
	fi
    fi

else
echo "      None"

fi
#====================================================
# End of AppleTalk settings.
#====================================================

echo "    Emmiting trailer..."
cat >&3 <<===EndEOF===

# end of file

===EndEOF===

#========================================================================
# End of global.mk builder code.
#========================================================================

# We are done
echo "Done."

echo
echo "Take a look at include/global.mk if you like, then run \"make\"."
echo
exit 0
