case $0 in
*/first)
	FDIR=`expr $0 : '\(.*\)/first'`
	;;
first)	FDIR=.
	;;
esac

if echo 'a\c' | grep 'c' >/dev/null ; then
	: We have BSD-like echo command.
	E_FLAG='-n'
	E_SFX=''
else
	: We have SystemV-like echo command.
	E_FLAG=''
	E_SFX='\c'
fi
export E_FLAG E_SFX

echo check write-ability of /tmp and /usr/tmp ...
if ( >/usr/tmp/aaax.$$ )
then
	rm /usr/tmp/aaax.$$
else
	echo /usr/tmp must exist and be writable.
	exit 2
fi
if ( >/tmp/aaax.$$ )
then
	rm /tmp/aaax.$$
else
	echo /tmp must exist and be writable.
	exit 2
fi
echo "/tmp and /usr/tmp ok"

if [ -f macros ]
then
	. macros
fi

: "Now get system name and directories"
. $FDIR/get_sys

: "Take action according to the system used"
BM=1
OLDACM=$ACM
OLDSYS=$SYS
case $SYSNAME in
vax_bsd4_1a)    ACM=vax4 ; SYS=BSD4_1 ; BYTE_ORDER=0123 ; MACH=vax4 ;;
vax_bsd4_2)	ACM=vax4 ; SYS=BSD4_2 ; BYTE_ORDER=0123 ; MACH=vax4 ;;
vax_sysV_2)	ACM=vax4 ; SYS=SYS_5 ; BYTE_ORDER=0123 ; MACH=vax4 ;;
i386)		ACM=i386 ; SYS=SYS_5 ; BYTE_ORDER=0123 ; MACH=i386 ;;
sun3)		ACM=sun3 ; SYS=BSD4_2; BYTE_ORDER=3210 ; MACH=m68020 ;;
sun2)		ACM=sun2 ; SYS=BSD4_2; BYTE_ORDER=3210 ; MACH=m68k4 ;;
m68_unisoft|m68k2)	ACM=m68k2 ; SYS=V7; BYTE_ORDER=3210 ; MACH=m68k2 ;;
m68_sysV_0|mantra)	ACM=mantra ; SYS=SYS_5; BYTE_ORDER=3210 ; MACH=m68k4 ;;
m68020)		ACM=m68020 ; SYS=SYS_5; BYTE_ORDER=3210 ; MACH=m68020 ;;
sparc)		ACM=sparc ; SYS=BSD4_2; BYTE_ORDER=3210 ; MACH=sparc ;;
sparc_solaris)	ACM=sparc_solaris ; SYS=SYS_5; BYTE_ORDER=3210 ; MACH=solaris ;;
i86)		ACM=i86 ; SYS=SYS_5; BYTE_ORDER=0123 ; MACH=i86 ;;
xenix3)		ACM=xenix3 ; SYS=SYS_5; BYTE_ORDER=0123 ; MACH=i86 ;;
minix)		ACM=minix ; SYS=V7; BYTE_ORDER=0123 ; MACH=i86 ;;
pmds)		ACM=pmds ; SYS=V7; BYTE_ORDER=3210 ; MACH=m68k2 ;;
pmds4)		ACM=pmds4 ; SYS=V7; BYTE_ORDER=3210 ; MACH=m68k4 ;;
minixST)	ACM=minixST ; SYS=V7; BYTE_ORDER=3210 ; MACH=m68k2 ;;
m68k4)		ACM=m68k4 ; SYS=V7; BYTE_ORDER=3210 ; MACH=m68k4 ;;
*)		ACM=XXX ; SYS=XXX ; BYTE_ORDER=XXX ; MACH=XXX ;;
esac

: do not change the order in MACH_LIST. check limit_enquire first.
MACH_LIST="i86 xenix3 minix i386 6500 6800 6805 6809 i80 em22 em24 em44 m68k2 pmds minixST m68k4 pmds4 sun2 mantra m68020 sun3 sparc sparc_solaris ns pdp s2650 vax4 z80 z8000 arm"

while :
do
	for i in $MACH_LIST
	do
		if [ $i = $ACM ]
		then break
		fi
	done
	if [ $i = $ACM ]
	then break
	fi
	echo "This installation script has no knowledge about $SYSNAME.
You will have to specify the default machine that you want ACK to
compile for. Choices:"
	l=
	x=
	for i in $MACH_LIST
	do
		l="$l $i"
		x=x$x
		case $x in
		xxxxxxxxxx)	echo $l
				x=
				l=
				;;
		esac
	done
	ACM=$OLDACM
	echo $l
	echo $E_FLAG "Your choice: [$OLDACM] $E_SFX"
	. $FDIR/get_answer
	case $ANS in
	'')	ANS="$ACM";;
	esac
	ACM="$ANS"
done
while :
do
case $SYS in
V7|BSD4_1|BSD4_2|SYS_5)
	break
	;;
*)
	SYS=$OLDSYS
	echo 'What kind of Unix is the target system running?
Choices:
	V7	for Unix V7, BSD 2.*
	BSD4_1	for Berkeley 4.1
	BSD4_2	for Berkeley 4.2, 4.3, SunOs 3, SunOs 4
	SYS_5	for Xenix, System III, System V, SunOs 5'
	echo $E_FLAG "Your choice (V7|BSD4_1|BSD4_2|SYS_5): [$OLDSYS] $E_SFX"
	. $FDIR/get_answer
	case $ANS in
	'')	ANS="$SYS";;
	esac
	SYS="$ANS"
	;;
esac
done
rm -f local.h
sed -e /ACKM/s/'".*"'/'"'$ACM'"'/ -e /BIGMACH/s/'[01]'/$BM/ -e /SYSTEM/s/'^#[ 	]*define[ 	]*[a-zA-Z_][a-zA-Z0-9_]*'/"#	define $SYS"/ < $FDIR/local.h.src >local.h
case $BYTE_ORDER in
XXX)	;;
*)	echo '/* Optional definition of BYTE_ORDER: */' >> local.h
	echo "#define BYTE_ORDER 0x$BYTE_ORDER" >> local.h
	;;
esac

echo "Your default machine to compile for is $ACM"

echo "Installation of the complete ACK takes a long time. Limiting the
number of languages, runtime libraries, back-ends, and assemblers to be
installed may save a lot of time. If you want to install everything,
answer no to the next question. Otherwise, answer yes, and you will be
prompted for details"
while :
do
	echo $E_FLAG "Do you want to limit the installation in any way? (y/n) [$LIMIT] $E_SFX"
	. $FDIR/get_answer
	case $ANS in
	'')	ANS="$LIMIT";;
	esac
	case X$ANS in
	Xj*|Xy|X)
		LIMIT=y
		. $FDIR/limit_enquire
		break
		;;
	Xn*)	. $FDIR/get_sysvax
		LIMIT=n
		DO_MACHINE_INDEP=y
		DO_FAST=n
		if [ $TARGET_HOME = $UTIL_HOME ]
		then
			case $ACM in
			sun3|m68020|i386|vax*)
				DO_FAST=y
				;;
			esac
		fi
		break
		;;
	*)	echo "I do not understand your answer ($ANS). Try again."
		;;
	esac
done

. $FDIR/get_makepars

echo "TARGET_HOME=\"$TARGET_HOME\"; export TARGET_HOME" > macros
echo "UTIL_HOME=\"$UTIL_HOME\"; export UTIL_HOME" >> macros
echo "SRC_HOME=\"$SRC_HOME\"; export SRC_HOME" >> macros
echo "SYSNAME=\"$SYSNAME\"; export SYSNAME" >> macros
echo "ACM=\"$ACM\"; export ACM" >> macros
echo "CURRENT=\"$CURRENT\"; export CURRENT" >> macros
echo "SYS=\"$SYS\"; export SYS" >> macros
echo "LIMIT=\"$LIMIT\"; export LIMIT" >> macros
echo "CONFIG=\"$CONFIG\"; export CONFIG" >> macros
echo "DISABLE_LANG=\"$DISABLE_LANG\"; export DISABLE_LANG" >> macros
echo "DISABLE_SUP=\"$DISABLE_SUP\"; export DISABLE_SUP" >> macros
echo "DO_MACHINE_INDEP=\"$DO_MACHINE_INDEP\"; export DO_MACHINE_INDEP" >> macros
echo "MACH_LIST=\"$MACH_LIST\"; export MACH_LIST" >> macros
echo "SYSVAX=\"$SYSVAX\"; export SYSVAX" >> macros
echo "MACH=\"$MACH\"; export MACH" >> macros
echo "WS=\"$WS\"; export WS" >> macros
echo "PS=\"$PS\"; export PS" >> macros
echo "DO_FAST=\"$DO_FAST\"; export DO_FAST" >> macros

cat macros $FDIR/install_tail > INSTALL
chmod +x INSTALL

echo "
A shell-script called 'INSTALL' has been created. Running it
installs ACK. Note that this may take a (very) long time, so run it
in the background, with its output redirected, f.i.:
	sh INSTALL > INSTALL.out 2>&1 &
"
exit 0
