: 'Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1988.'

echo "Configuration questions for the ABC system."

: some greps do not return status, fix this first.
echo "grimblepritz" >grimble
if grep blurfldyick grimble >/dev/null 2>&1 ; then
	contains=./contains
else
	if grep grimblepritz grimble >/dev/null 2>&1 ; then
		contains=grep
	else
		contains=./contains
	fi
fi
rm grimble
: the following should work in any shell
case $contains in
./contains)
	echo " "
	echo "Grep doesn't return a status; attempting remedial action."
	cat >contains <<'EOF'
	grep "$1" "$2" >.greptmp && cat .greptmp && test -s .greptmp
EOF
	chmod 755 ./contains;;
esac

: 'Determine if echo -n works'
echo -n "bleep" >bleep
if $contains n bleep >/dev/null 2>&1
then
	n=""
else
	n="-n"
fi
rm -f bleep

: 'Get answers from previous run'
if test -r answers
then
	. answers
else
	rootdef="/tmp/abc"
	abcdef="/usr/new"
	libdef="/usr/new/lib/abc"
	mandef="/usr/man/mann"
fi

: 'Are we preparing for cross compilation?'
echo " "
echo $n "Are you going to cross-compile the ABC system to a remote machine? "
read ans
case $ans in
y*|Y*|t*|T*)
remote=" remote"
rootdir='blurfl'
while test ! -d "$rootdir" ; do
	case $rootdir in
	blurfl)
		echo " "
		echo "Then please type the full pathname of a local directory"
		echo "where you have write permission"
		echo "and which we can use as a local root to install"
		echo "the ABC system for you to copy to the remote machine"
		echo $n " (default $rootdef) "
		;;
	*) 	echo "$rootdir does not appear to exist."
		echo $n "Are you sure you typed the right name? "
		read ans
		case $ans in
		y*|Y*)	echo "Don't forget to make $rootdir before 'make install'."
			break
			;;
		*)	echo " "
			echo $n "Please try again (default $rootdef) "
			;;
		esac
		;;
	esac
	read rootdir
	case $rootdir in
	"") rootdir=$rootdef;;
	esac
done
;;
*)
remote=""
rootdir=""
;;
esac

: 'What unix?'
echo " "
echo $n "Finding out about your brand of unix ... "
cat >brand.c <<EOF
#include <stdio.h>
#include <signal.h>
main() {
#ifdef SIGCHLD
#ifdef SIGVTALRM
	printf("BSD4_2\n");
#else
	printf("BSD\n");
#endif
#else
#ifdef SIGCLD
	printf("ATT\n");
#else
	printf("unknown\n"); /* Version 7 we hope ... */
#endif
#endif
}
EOF
cc brand.c -o brand
unix=`./brand`
rm -f brand*

echo " "
case $unix in
BSD4_2)
	echo "You appear to be running Berkeley's BSD unix, version 4.2 (or later)."
	echo $n "Is this true? "
	read ans
	case $ans in
	n*|N*|f*|F*)	unix="unknown";;
	esac
	;;
BSD)
	echo "You appear to be running Berkeley's BSD unix."
	echo $n "Is this true? "
	read ans
	case $ans in
	n*|N*|f*|F*)	unix="unknown";;
	esac
	;;
ATT)
	echo "You appear to be running ATT's System III or System V unix."
	echo $n "Is this true? "
	read ans
	case $ans in
	n*|N*|f*|F*)	unix="unknown";;
	esac
	;;
esac

case $unix in
unknown)
	echo "You might be running VERSION 7 unix."
	echo $n "Is this true? "
	read ans
	case $ans in
	y*|Y*|t*|T*)
		unix=VERSION7;;
	*)	echo "We cannot find out what type of unix you are running."
		echo "See ./uhdrs/*.h and ./Problems in case of problems."
		unix=""
		;;
	esac
	;;
esac

: 'termio system?'
echo " "
echo $n "Checking for termio system ... "
termio=""
case $unix in
ATT)	if test -r /usr/include/termio.h
	then
		termio="#define"
		echo "termio.h found."
	elif test -r /usr/include/sgtty.h
	then
		termio="#undef"
		echo "sgtty.h found."
	fi
	;;
*)	if test -r /usr/include/sgtty.h
	then
		termio="#undef "
		echo "sgtty.h found."
	elif test -r /usr/include/termio.h
	then
		termio="#define"
		echo "termio.h found."
	fi
	;;
esac
case $termio in
'')	echo " "
	echo "Neither termio.h nor sgtty.h found."
	echo "See ./unix/u1trm.c, ./unix/u1keys.c and ./keys/abckeys.c"
	echo "in case of problems."
	termio="#undef "
esac

: 'Include <strings.h> or <string.h>?'
echo " "
echo $n "Finding include file for string operations ... "
if test -r /usr/include/strings.h
then
	strs="<strings.h>"
	echo "strings.h found."
else
	if test -r /usr/include/string.h
	then
		strs="<string.h>"
		echo "string.h found."
	else
		strs="no_strs"
		echo " "
		echo "Neither strings.h nor string.h found."
		echo "We will add some external definitions to ./uhdrs/os.h."
	fi
fi

: 'Some ATT System V have <unistd.h> for <sys/file.h>'
: 'Some systems have both and may need either one, so we must check'
echo " "
echo $n "Finding include file for access modes ... "
if $contains F_OK /usr/include/sys/file.h >/dev/null 2>&1
then
	modes="<sys/file.h>"
	echo "proper sys/file.h found."
else
	if $contains F_OK /usr/include/unistd.h >/dev/null 2>&1
	then
		modes="<unistd.h>"
		echo "proper unistd.h found."
	else
		modes="no_modes"
		echo " "
		echo "No access mode definitions found in sys/file.h or unistd.h."
		echo "We will substitute our own definitions in ./uhdrs/os.h."
	fi
fi

: 'Some Berkeley systems (and the PC:-) have ftime(), including <sys/timeb.h>'
echo " "
echo $n "Finding include file for ftime(3) ... "
if test -r /usr/include/sys/timeb.h
then
	ftime="#define"
	echo "sys/timeb.h found."
else
	ftime="#undef "
	echo " "
	echo "No sys/timeb.h found."
	echo "We will assume ftime(3) is not available in ./uhdrs/os.h."
	echo "The ABC function \"now\" will only return entire seconds."
fi

: 'Check for void type'
echo " "
echo $n "Checking to see if your compiler accepts the void type ... "
cat >void.c << EOF
void main();
EOF
if cc -c void.c >/dev/null 2>&1
then
	void="(void)"
	echo "yes, it does."
else
	void="      "
	echo "no, it doesn't."
	echo "We will use an empty VOID in casts."
fi
rm -f void.c void.o

: 'Determine type for signal handlers'
case $void in
"(void)")
echo " "
echo $n "Finding type for signal handlers ... "
echo "#include <signal.h>" >sigtype.c
echo "main() { void (*oldsig)(); oldsig= signal(SIGINT, SIG_IGN); }" >>sigtype.c
cc -c sigtype.c >warnings 2>&1
if test -s warnings
then
	sigtype=int
	echo "assuming int."
else
	sigtype=void
	echo "found void."
fi
rm -f sigtype.c sigtype.o warnings
;;
*)	sigtype=int;;
esac

: 'Floating point arithmetic ok?'
echo " "
case $remote in
" remote")
echo "Does your C compiler need a special flag for"
echo $n "loading (probably software) floating point routines? "
read ans
case $ans in
y*|Y*)	echo $n "What flag is needed (default -f) "
	read fflag
	case $fflag in
	'')	fflag="-f";;
	esac
	echo "We will try $fflag."
	;;
*)	echo "Assuming no flag is needed."
	fflag=
	;;
esac
;;
*)
echo $n "Checking your floating point ... "
cat >float.c <<EOF
#include <stdio.h>
double dadd(a, b) double a, b; {
	return a+b;
}
main() {
	double a, b, c;
	double dadd();
	a = 3.14;
	b = 2.7 * a;
	c = dadd(a, b);
	exit(0);
}
EOF
cc -O float.c -o float >/dev/null 2>&1
echo " "
if sh 2>/dev/null <<EOF
	if ./float
	then exit 0
	else exit 1
	fi
EOF
then
	echo "Your floating point seems to be alright."
	fflag=""
else
	echo "Your C compiler seems to need a special flag for"
	echo "loading (probably software) floating point routines."
	echo $n "What flag is needed (default -f) "
	read fflag
	case $fflag in
	'')	fflag="-f";;
	esac
	cc -O $fflag float.c -o float >/dev/null 2>&1
	until sh 2>/dev/null <<EOF
		if ./float
		then echo "Flag $fflag seems to be allright."
		     exit 0
		else exit 1
		fi
EOF
	do
		echo "This flag doesn't seem to work."
		echo "Try again ([RETURN] to give up) "
		read fflag
		case $fflag in
		'')	echo "Giving up on floating point flag"
			break;;
		esac
		cc -O $fflag float.c -o float >/dev/null 2>&1
	done
fi
rm -f float* core
;;
esac

: 'termcap library for ABC editor'
echo " "
echo $n "Searching for your termcap-like library routines ... "
if test -r /lib/libtermcap.a -o -r /usr/lib/libtermcap.a
then
	termlib="-ltermcap"
	echo "termcap library found."
elif test -r /usr/local/lib/libtermcap.a
then
	termlib="/usr/local/lib/libtermcap.a"
	echo "local termcap library found."
elif test -r /lib/libcurses.a -o -r /usr/lib/libcurses.a
then
	termlib="-lcurses"
	echo "terminfo library found."
elif test -r /usr/local/lib/libcurses.a
then
	termlib="/usr/local/lib/libcurses.a"
	echo "local terminfo library found."
elif test -r /lib/libtermlib.a -o -r /usr/lib/libtermlib.a
then
	termlib="-ltermlib"
	echo "termlib library found."
elif test -r /usr/local/lib/libtermlib.a
then
	termlib="/usr/local/lib/libtermlib.a"
	echo "local termlib library found."
else
	echo " "
	echo "Your system appears NOT to have any termcap-like library."
	echo $n "Is this true? "
	read ans
	case $ans in
	n*|N*|f*|F*)	termlib="ask_it"
		;;
	*)	termlib="libtermcap.a"
		cat <<\EOF
We will setup ./Makefile to use the public domain version of the
termcap routines from ./tc. Your users must set the $TERMCAP and
$TERM environment variables appropriately in their profile; or you
can install shell scripts that arrange these.
However, it would be better if your system administrator installs
termcap in the proper places. You should run Setup again after she
has done so.
See ./tc/README for details.
EOF
		;;
	esac
fi
while test "$termlib" = "ask_it" -o "$termlib" = "try_again"; do
	case $termlib in
	ask_it)
		echo " "
		echo "Please specify where the termcap-like routines are kept"
		echo $n " (either full pathname or -lxxx option) "
		;;
	try_again)
		echo $n "Please try again (full pathname or -lyyy option) "
		;;
	esac
	read termlib
	case $termlib in
	-l*)
		echo "Hope $termlib will do the job."
		;;
	/*)
		if test -r $termlib
		then
			echo "Ok, we will try $termlib."
		else
			echo "$termlib does not appear to exist."
			termlib="try_again"
		fi
		;;
	*)
		echo "$termlib is not a valid library specification."
		termlib="try_again"
		;;
	esac
done

: 'find C preprocessor for ./scripts/mkdep'
echo " "
echo 'Checking if we can use "cc -M" for generating dependency information ... '
cc -M -Iuhdrs mkconfig.c >testcpp.out 2>&1
if $contains 'mkconfig.o: *uhdrs/osconf.h' testcpp.out >/dev/null 2>&1 ; then
	echo "Yes, we can."
	cpp='REDUNDANT'
	mkdep="\$(CC) -M \$(DEFS)"
else
	echo "No, we can't; we will try to use ./scripts/mkdep for \"make depend\"."
	mkdep="../scripts/mkdep \$(DEFS)"
	echo "Checking to see how your C preprocessor is invoked ... "
	cpp=''
cat <<'EOT' >testcpp.c
#define ABC abc
#define XYZ xyz
ABC+XYZ
EOT
	echo 'Maybe "cc -E" will work ...'
	cc -E testcpp.c >testcpp.out 2>&1
	if $contains 'abc+xyz' testcpp.out >/dev/null 2>&1 ; then
		echo "Yes, it does."
		cpp='cc -E'
	else
		echo 'No ... maybe "cc -P" will work ...'
		cc -P testcpp.c >testcpp.out 2>&1
		if $contains 'abc+xyz' testcpp.out >/dev/null 2>&1 ; then
			echo "Yes, that does."
			cpp='cc -P'
		else
			echo 'Wrong again ... maybe "/lib/cpp" will work ...'
			/lib/cpp testcpp.c >testcpp.out 2>&1
			if $contains 'abc+xyz' testcpp.out >/dev/null 2>&1
			then 	echo "Hooray, it works!  I was beginning to wonder."
				cpp='/lib/cpp'
			else
	echo $n "No. I can't find a C preprocessor.  Name one: "
	read cpp
	until sh 2>/dev/null <<EOF
$cpp testcpp.c >testcpp.out 2>&1
if $contains 'abc+xyz' testcpp.out >/dev/null 2>&1
then exit 0
else exit 1
fi
EOF
	do
		echo "Sorry, I can't get that to work."
		echo "Try again ([RETURN] to give up) "
		read cpp
		case $cpp in
		'')	: giving up
			break;;
		esac
	done
			fi
		fi
	fi
fi
rm -f testcpp.*

case $cpp in
REDUNDANT) : no mkdep needed
	;;
'')	cat <<EOF
Giving up on C preprocessor.
You have to adapt the shell script ./scripts/mkdep yourself;
see the comment at its head.
EOF
	;;
*)
	cat <<EOF
We will change the name of the C preprocessor to "$cpp"
in ./scripts/mkdep (used by 'make depend').
But you still may have problems with the format of the lines that the
preprocessor creates for '#include' statements; see the comment at the
start of ./scripts/mkdep.
If you can't get 'make depend' to work see ./Problems for a workaround.
EOF
	;;
esac

: 'Shall we make ABC public?'
public="no"
echo " "
echo $n "Do you want to make ABC publically available? "
read ans
case $ans in
y*|Y*)
public="yes"
abcdir='blurfl'
while test ! -d "$rootdir$abcdir" ; do
	case $abcdir in
	blurfl)
		echo " "
		echo "Please type the full pathname of a$remote directory"
		echo "in which you have write permission, and where you"
		echo "want the binaries 'abc' and 'abckeys' installed"
		echo $n " (default $abcdef) "
		;;
	*) 	echo "$rootdir$abcdir does not appear to exist."
		echo $n "Are you sure you typed the right name? "
		read ans
		case $ans in
		y*|Y*)	echo "Don't forget to make $rootdir$abcdir before 'make install'."
			break
			;;
		*)	echo " "
			echo $n "Please try again (default $abcdef) "
			;;
		esac
		;;
	esac
	read abcdir
	case $abcdir in
	"") abcdir=$abcdef;;
	esac
done
libdir='blurfl'
while test ! -d "$rootdir$libdir" ; do
	case $libdir in
	blurfl)
		echo " "
		echo "Please type the full pathname of the$remote directory"
		echo "where you want to install the auxiliary datafiles"
		echo "needed by the ABC system"
		echo $n " (default $libdef) "
		;;
	*) 	echo "$rootdir$libdir does not appear to exist."
		echo $n "Are you sure you typed the right name? "
		read ans
		case $ans in
		y*|Y*)	echo "Don't forget to make $rootdir$libdir before 'make install'."
			break
			;;
		*)	echo " "
			echo $n "Please try again (default $libdef) "
			;;
		esac
		;;
	esac
	read libdir
	case $libdir in
	"") libdir=$libdef;;
	esac
done
mandir='blurfl'
while test ! -d "$rootdir$mandir" ; do
	case $mandir in
	blurfl)
		echo " "
		echo "Please type the full pathname of the$remote directory"
		echo "where you want to install the 'abc.1' manual page"
		echo $n " (default $mandef) "
		;;
	*) 	echo "$rootdir$mandir does not appear to exist."
		echo $n "Are you sure you typed the right name? "
		read ans
		case $ans in
		y*|Y*)	echo "Don't forget to make $rootdir$mandir before 'make install'."
			break
			;;
		*)	echo " "
			echo $n "Please try again (default $mandef) "
			;;
		esac
		;;
	esac
	read mandir
	case $mandir in
	"") mandir=$mandef;;
	esac
done
;;
*)
abcdir="$abcdef"
libdir="$libdef"
mandir="$mandef"
;;
esac

cat >answers <<EOF
rootdef="$rootdir"
abcdef="$abcdir"
libdef="$libdir"
mandef="$mandir"
EOF

echo " "
echo "End of configuration questions for the ABC system."

rm -f ./contains

editor="ed -"

: 'Getting generic copies of Makefile, uhdrs/os.h, and scripts/mkdep.'
: 'Since some tars leave the owner of the tape as the owner of the files'
: 'this will make sure the edit scripts will not fail.'

echo " "
echo "Creating Makefile from Makefile.unix."

cat >Makefile <<EOF
# This file was generated and edited by the Setup command.
# To make lasting changes edit the files Makefile.unix and Setup,
# and run Setup again.
# 
EOF
chmod 644 Makefile
cat Makefile.unix >>Makefile

echo "Creating uhdrs/os.h from uhdrs/os.h.gen."

cat >uhdrs/os.h <<EOF
/* This file was generated and edited by the Setup command. */
/* To make lasting changes edit the files uhdrs/os.h.gen and Setup, */
/* and run Setup again. */

EOF
chmod 644 uhdrs/os.h
cat uhdrs/os.h.gen >>uhdrs/os.h

echo "Creating scripts/mkdep from scripts/mkdep.gen."

cat >scripts/mkdep <<EOF
: This file was generated from scripts/mkdep.gen by Setup
:
EOF
chmod 755 scripts/mkdep
cat scripts/mkdep.gen >>scripts/mkdep

echo " "
echo $n "Editing appropriate definitions in these files ... "

case $unix in
BSD4_2)
	make="make \$(MFLAGS)"
	strchr="index"
	perror="#define"
	mkdir="#define"
	getwd="#define"
	rename="#define"
	select="#define"
	readdir="#define"
	;;
BSD)	make="make \$(MFLAGS)"
	strchr="index"
	perror="#define"
	mkdir="#undef "
	getwd="#undef "
	rename="#undef "
	select="#undef "
	readdir="#undef "
	;;
ATT)	make="make -\$(MAKEFLAGS)"
	strchr="strchr"
	perror="#define"
	mkdir="#define"
	getwd="#define"
	rename="#undef "
	select="#undef "
	readdir="#undef "
	;;
VERSION7|*)
	make="make"
	strchr="index"
	perror="#undef "
	mkdir="#undef "
	getwd="#undef "
	rename="#undef "
	select="#undef "
	readdir="#undef "
	;;
esac

$editor Makefile <<EOF
/^MAKE=/s?MAKE=.*?MAKE= $make?
/^FLOAT=/s?FLOAT=.*?FLOAT= $fflag?
/^MKDEP=/s?MKDEP=.*?MKDEP= $mkdep?
/^TERMLIB=/s?TERMLIB=.*?TERMLIB= $termlib?
w
q
EOF

case $public in
yes)
	$editor Makefile <<EOF
	/^DESTABC=/s?DESTABC=.*?DESTABC= $abcdir?
	/^DESTLIB=/s?DESTLIB=.*?DESTLIB= $libdir?
	/^DESTMAN=/s?DESTMAN=.*?DESTMAN= $mandir?
	w
	q
EOF
	;;
esac

$editor uhdrs/os.h <<EOF
/^#include <strings.h>/s?<strings.h>?$strs?
/^#include <sys\/file.h>/s?<sys/file.h>?$modes?
/^#define VOID/s?(void)?$void?
/^#define SIGTYPE/s?int?$sigtype?
/^#undef  TERMIO/s?#undef ?$termio?
/^#define HAS_FTIME/s?#define?$ftime?
/^#define HAS_PERROR/s?#define?$perror?
/^#define HAS_MKDIR/s?#define?$mkdir?
/^#define HAS_GETWD/s?#define?$getwd?
/^#define HAS_RENAME/s?#define?$rename?
/^#define HAS_SELECT/s?#define?$select?
/^#define HAS_READDIR/s?#define?$readdir?
w
q
EOF

case $strs in
no_strs)
	case $strchr in
	"index")	index=index; rindex=rindex;;
	"strchr")	index=strchr; rindex=strrchr;;
	esac
	$editor uhdrs/os.h <<EOF
/#include no_strs/c

extern char *strcat();
extern int strcmp();
extern int strncmp();
extern char *strcpy();
extern char *strncpy();
extern int strlen();
extern char *$index();
extern char *$rindex();

.
w
q
EOF
;;
esac

case $modes in
no_modes)
	$editor uhdrs/os.h <<EOF
/#include no_modes/c

#define R_OK 4
#define W_OK 2
#define F_OK 0
.
w
q
EOF
;;
esac

case $strchr in
strchr)	$editor uhdrs/os.h <<EOF
/#define strchr/,/#define strrchr/d
w
q
EOF
;;
esac

case $unix in
ATT)	$editor uhdrs/os.h <<EOF
/#define HAS_GETWD/a
#define getwd(path) getcwd(path, SIZE_PATH)
.
w
q
EOF
;;
esac

case $termlib in
libtermcap.a)
	$editor Makefile <<EOF
	/TERMLIB=/s?.*?TERMLIB=?
	/#OWNTLIB=/s?#??
	/#KOWNTLIB=/s?#??
	/#OWNTBASE=/s?#??
	w
	q
EOF
	;;
esac

case $cpp in
REDUNDANT) : no mkdep needed
	;;
'')	: do it yourself message given above
	;;
*)
	$editor scripts/mkdep <<EOF
	/^CPP=/s?.*?CPP="$cpp"?
	w
	q
EOF
	;;
esac

echo $n " "
echo "done."

echo " "
echo "This completes the setup of the ABC system."

case $remote in
' remote')
	cat <<EOF
You should first compile mkconfig (from ./mkconfig.c and ./uhdrs/os.h)
with 'make mkconfig' here, run it on the remote machine and copy the
result to ./uhdrs/config.h.
After that you can proceed here with 'make makefiles'.
EOF
	;;
*)	echo "You can now try 'make makefiles'."
	;;
esac

case $remote in
" remote")
	if test ! -d "$rootdir"
	then
		echo " "
		echo "You should 'mkdir $rootdir' before 'make install'."
	fi
	;;
esac

case $public in
yes)
	if test ! -d "$rootdir$abcdir"
	then
		echo " "
		echo "You should 'mkdir $rootdir$abcdir' before 'make install'."
	fi
	if test ! -d "$rootdir$libdir"
	then
		echo "You should 'mkdir $rootdir$libdir' before 'make install'."
	fi
	if test ! -d "$rootdir$mandir"
	then
		echo "You should 'mkdir $rootdir$mandir' before 'make install'."
	fi
	;;
esac

exit 0
