#!/bin/sh
#------------------------------------------------------------------------------
# Ulm's Oberon System -- interactive installation procedure
# Copyright (C) University of Ulm, SAI, D-89069 Ulm, Germany
# afb 4/93
#------------------------------------------------------------------------------

	name=`basename $0`

# keep original path
	path="$PATH"
# be sure to call commands we know about
	PATH=/bin:/usr/bin:/usr/ucb:/etc

# configuration parameters
	libdir=			# determined by ilib or checklib
	bindir=			# determined by checkbin
	mandir=			# determined by iman
	catdir=			# determined by iman
	rel=			# determined by checkrel
	m20as=			# determined by checkas
	gas_version=		# determined by checkas
	archivist=		# determined by checkar
	needranlib=		# determined by checkar
	ranlib=			# determined by checkar
	ld=			# determined by checkld
	gmake=			# determined by checkmake
	newer=			# determined by checknewer
	version=		# determined by checkversion
	distributedBy=		# determined by checkversion
	distributionDate=	# determined by checkversion
	owner_name=		# determined by checkuid
	owner_group=		# determined by checkgid
	tbd="1 2 3 4 5 6 7"	# still to be done
	params="libdir bindir mandir catdir rel m20as gas_version"
	params="$params archivist needranlib ranlib ld gmake newer"
	params="$params version distributedBy distributionDate tbd"
	params="$params owner_name owner_group"

# other parameters
	uid=			# determined by checkuid

# temporary files
	tmpdir=/tmp
	tmpconf=$tmpdir/$name.$$.1
	tmptest=$tmpdir/$name.$$.2
	trap "rm -f $tmpconf $tmptest" 0

# catch interrupts for proper termination
	trap "quit" 1 2 3 15

error()
{
	echo; echo "***" "$@"
}

quit()
{
	saveconf
	echo; error QUIT; echo; exit 1
}

#------------------------------------------------------------------------------
# gets prompt shellvar default text
#------------------------------------------------------------------------------
gets()
{
	prompt="$1" shellvar="$2" default="$3" text="$4"
	case $text
	in "")	#
	;; *)	echo "$text"
	esac
	done=FALSE
	until [ $done = TRUE ]
	do	echo -n "$prompt" "[$default] "
		if read gets_text
		then	:
		else	quit; continue
		fi
		case $gets_text
		in !)	PATH="$path" PS1="$0 $ " ${SHELL-/bin/sh}
			echo $0
		;; "?")	echo "$text"
		;; ".")	quit
		;; !*)	gets_text="`echo $gets_text | sed 's/^!//'`"
			PATH="$path" PS1="$0 $ " ${SHELL-/bin/sh} \
			   -c "$gets_text"
			echo $0
		;; "")	eval "$shellvar"='"$default"'; done=TRUE
		;; *)	eval "$shellvar"='"$gets_text"'; done=TRUE
		esac
	done
}

#------------------------------------------------------------------------------
checkuid()
{	uid=`whoami`
	case $uid
	in root)	uid=0
	;; *)		return
	esac

	if [ "" != "$owner_name" ]
	then	return
	fi

	ok=FALSE
	until [ $ok = TRUE ]
	do	echo '
Give login name and group name of the designated owner of the Oberon system.'
		gets "login name" owner_name bin
		gets "group name" owner_group bin
		> $tmptest
		if chown $owner_name.$owner_group >/dev/null 2>&1
		then	ok=TRUE
		else	echo "$owner_name/$owner_group: unknown user"
		fi
	done
}

#------------------------------------------------------------------------------
# dirname path
#    not all people have dirname installed (belongs to the System V options)
#------------------------------------------------------------------------------
dirname()
{	expr \
		${1-.}'/' : '\(/\)[^/]*//*$' \
		\| ${1-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \
		\| .
}

#------------------------------------------------------------------------------
# set permission and owner of given files
#
# use find instead of test -x because test -x is always true
# if you are super-user and the file exists (bug of test)
#------------------------------------------------------------------------------
chall()
{	for file
	do	if [ -d $file ]
		then	chmod 755 $file # -rwxr-xr-x
		elif [ `find $file -perm -100 -print | wc -l` -gt 0 ]
		then	chmod 711 $file	# -rwx--x--x
		else	chmod 644 $file # -rw-r--r--
		fi
		if [ "$uid" = 0 ]
		then	chgrp $owner_group $file
			chown $owner_name $file
		fi
	done
}

#------------------------------------------------------------------------------
# check for current directory and exit if it isn't the root-directory
# of the tar-tape
#------------------------------------------------------------------------------
checkloc()
{	if [ ! -d install ] || [ ! -d doc ] || [ ! -d lib ] || [ ! -f bin/oc ]
	then	error $0 not started in the root directory of the distribution
		exit 1
	fi
}

#------------------------------------------------------------------------------
# getconf
#------------------------------------------------------------------------------
getconf()
{
	text='
There is a configuration file which is probably left by an earlier
run of install. Are these settings to be considered?
'
	if [ -r install/conf ]
	then	gets "Reading configuration OK?" answer yes "$text"
		if [ yes != "$answer" ]
		then	return
		fi
		echo -n "Reading the configuration file..."
		. install/conf
		echo "OK"
	fi
}

#------------------------------------------------------------------------------
# saveconf
#------------------------------------------------------------------------------
saveconf()
{	
	text='
The configuration has been changed or extended since your last install-run.
Is it OK to update the configuration file in install/conf?
'
	{	echo "# Configuration parameters of install"
		for param in $params
		do	eval val=\$$param
			echo "$param='$val'"
		done
	} >$tmpconf
	if [ ! -f install/conf ]
	then	cp $tmpconf install/conf
	else	if cmp -s $tmpconf install/conf
		then	: OK -- no difference
		else	gets "Update configuration?" answer yes "$text"
			case $answer
			in yes)	cp $tmpconf install/conf
			esac
		fi
	fi
}

#------------------------------------------------------------------------------
# installed part
#------------------------------------------------------------------------------
installed()
{	part="$1"
	case "$tbd"
	in *"$part"*)	return 1	# it is still to be done
	;; *)		return 0	# it is already installed
	esac
}

#------------------------------------------------------------------------------
# install_ok part
#------------------------------------------------------------------------------
install_ok()
{	part="$1"
	parts=""
	for p in $tbd
	do	if [ "$p" = "$part" ]
		then	: leave it out
		elif [ "$parts" = "" ]
		then	parts="$p"
		else	parts="$parts $p"
		fi
	done
	tbd="$parts"
}

#------------------------------------------------------------------------------
# check for SunOS release
#------------------------------------------------------------------------------
checkrel()
{
	if [ "" != "$rel" ]
	then	return
	fi
	if [ -x /usr/5bin/uname ]
	then	rel=`/usr/5bin/uname -r`
	else	rel=4.0.3
	fi
	if grep 'SunOS Release' /etc/motd >/dev/null
	then	rel=`grep 'SunOS Release' /etc/motd |
		sed 's/^SunOS Release //; s/[_ ].*//'`
	fi
	text='
Give the release number of your SunOS system.'
	oldrelease='
<<< CAUTION >>>

Your release is not supported by the Oberon package because the storage
management utilizes mmap(2). If you cannot upgrade to SunOS 4.xx, the
system dependent module SysStorage must be rewritten. Regrettably, you
will not even be able to use oc(1), the calling interface to the Oberon
compiler, because it is written in Oberon.

Enter "yes" to continue the installation (which will at least initially
not work) or "no" to exit the installation process. Alternatively, you
my enter "again" to type a different release number.
'
	ok=FALSE
	until [ $ok = TRUE ]
	do	gets release rel $rel "$text"
		case $rel
		in 3.*)		gets "Continue?" answer no "$oldrelease"
				if [ no = "$answer" ]
				then	quit
				elif [ again = "$answer" ]
				then	ok=FALSE
				else	ok=TRUE
				fi
		;; 4.0)		ok=TRUE
		;; 4.0.3)	ok=TRUE
		;; 4.1)		ok=TRUE
		;; 4.1.1)	ok=TRUE
		;; 4.1.1_U1)	ok=TRUE
		;; *)	echo 'Your release is not supported!'; ok=FALSE
			echo 'Try to give a release which comes close to yours,'
			echo 'eg 3.5, 4.0 or 4.1'
		esac
	done
}

#------------------------------------------------------------------------------
# checknewer
#    compile newer -- installation is done later by immo
#------------------------------------------------------------------------------
checknewer()
{	if [ "" != "$newer" ]
	then	return
	fi

	echo "Recompiling newer. This takes less than one minute."
	assembler=`pwd`/gnu/gas-1.36
	(	cd src/newer &&
		OBAS="$assembler" OBLD="$ld" \
			OBAR="$archivist" OBNEEDRANLIB="$needranlib" \
			OBRANLIB="$ranlib" \
			$gmake -r OC=../../bin/oc OBLIB=../../lib
	) >src/newer/PROT 2>&1 ||
	{	echo "Recompilation of newer failed. Errors are in src/newer/PROT."
		return 1
	}
	newer=`pwd`/src/newer/newer
}

#------------------------------------------------------------------------------
# where to find the MC68020-assembler
#------------------------------------------------------------------------------
checkas()
{
	if [ "" != "$m20as" ]
	then	return
	fi

	text1='
The SunOS assembler does *not* work for the Oberon compiler because it
contains some serious bugs.  Please give the complete pathname of an
alternative assembler, preferably GNU assembler (gas) of version 1.36.
You may choose "gas". In this case, gas-1.36 which is part of this
distribution in binary form will be installed in the library directory.'
	text2="
<<< CAUTION >>>
This is not gas version 1.36 which is the only assembler which
is guaranteed to work with this Oberon system. You have four
options:
(1) Enter "'"'"yes"'"'" and use the given assembler at your own risk.
(2) Give "'"'"no"'"'" to specify another assembler.
(3) gas-1.36 is part of this distribution in binary form. Give
    "'"'"gas"'"'" to use the precompiled gas-1.36 from this distribution.
    In this case, gas gets installed in the library directory.
(4) Stop the installation and look for gas-1.36. You can download gas-1.36
    from the same directory where you have found this stuff.
    You'll find some additional instructions about gas-1.36 in the
    gas subdirectory.
"
	gaspath=`PATH="$path" type gas | sed 's/^.*is //'`
	case $gaspath
	in *"not found"*)
		if [ -x /usr/local/bin/gas ]
		then	gaspath=/usr/local/bin/gas
		else	gaspath=gas
		fi
	esac
	while :
	do	gets "MC68020-assembler" m20as "$gaspath" "$text1"
		if [ gas = "$m20as" ]
		then	m20as="$libdir/gas-1.36"
			break
		fi
		if [ ! -f "$m20as" ]
		then	echo "$m20as: no such file"; continue
		fi
		gas_version=`$m20as -v -o /dev/null </dev/null 2>&1 |
			sed 's/GNU assembler version //'`
		case $gas_version
		in 1.36)	# OK
		;; [0-9]*)	error "Warning: this is GAS version $gas_version"
				gets "$m20as OK" answer no "$text2"
				case $answer
				in yes) break
				;; gas) m20as=$libdir/gas-1.36
					gas_version=1.36
					break
				;; *) continue
				esac
		;; *)		gas_version=
				error "Warning: this is not a GNU assembler"
				gets "$m20as OK" answer no "$text2"
				case $answer
				in yes) break
				;; gas) m20as=$libdir/gas-1.36
					gas_version=1.36
					break
				;; *) continue
				esac
		esac
		break
	done
}

#------------------------------------------------------------------------------
# where to install the Oberon library directory?
#------------------------------------------------------------------------------
checklib()
{
	if [ "" != "$libdir" -a -d "$libdir" ]
	then	return
	fi

	text1='
The Oberon compiler needs a library directory.
Typically /usr/lib/oberon or /usr/local/lib/oberon.'
	text2='
<<< CAUTION >>>
The given directory exists. Give "no" if you want to give another directory.
If you answer "yes", the lib-files will be copied into this directory
regardless about the previous contents of this directory.
You can escape to the shell via "!" and rename or cleanup the directory.'
	text3='
<<< CAUTION >>>
An absolute pathname is necessary. Prepending the result of pwd is not
always the right choice, especially if symbolic links gets involved.
If you answer "yes" the proposal is taken. Otherwise you have the
possibility to re-enter an absolute path by giving "no".'
	ok=FALSE
	until [ $ok = TRUE ]
	do	gets "library directory" libdir /usr/local/lib/oberon "$text1"
		case $libdir	# make sure we have an absolute path
		in /*)	# ok
		;; *)	libdir=`pwd`/$libdir
			gets "$libdir OK ?" answer no "$text3"
			case $answer
			in yes) # ok
			;; *)	continue
			esac
		esac
		if [ -f "$libdir" ]
		then	error "$libdir: exists (regular file)"
			continue
		fi
		if [ -d "$libdir" ]
		then	gets "$libdir OK ?" answer no "$text2"
			case $answer
			in yes) # ok
			;; *)	continue
			esac
		fi
		if [ ! -d "$libdir" ] && mkdir "$libdir" && chall "$libdir"
		then	echo; echo "*** $libdir: created"
		elif [ ! -d "$libdir" ]
		then	error "$libdir: cannot create"; continue
		fi
		break
	done
}

#------------------------------------------------------------------------------
# where to install binaries?
#------------------------------------------------------------------------------
checkbin()
{	if [ "" != "$bindir" ]
	then	return
	fi

	text='
Where to install binary files? Possible would be /usr/bin or
a directory with local binaries like /usr/local/bin.'
	ok=FALSE
	until [ $ok = TRUE ]
	do	gets "bin-directory" bindir /usr/local/bin "$text"
		if [ ! -d $bindir ]
		then	error "$bindir: no such directory"
		elif [ ! -w $bindir ]
		then	error "cannot create files in $bindir"
		else	case $bindir	# be sure to have an absolute path
			in "/"*)	# ok
			;; *)		bindir=`pwd`/$bindir
			esac
			ok=TRUE
		fi
	done
}

#------------------------------------------------------------------------------
# checkld
#------------------------------------------------------------------------------
checkld()
{
	if [ "" != "$ld" ]
	then	return
	fi
text='
The Oberon compiler needs a linkage editor. In general, the ld(1) as
shipped by Sun works but you are free to give a compatible
alternative here, e.g. GNU-ld.
'
	ldpath=`PATH="$path" type ld | sed 's/^.*is //'`
	case $ldpath
	in *"not found"*)
		if [ -x /usr/bin/ld ]
		then	ldpath=/usr/bin/ld
		else	ldpath=/bin/ld
		fi
	esac
	while :
	do	gets "Path of ld" ld "$ldpath" "$text"
		if [ ! -f "$ld" ]
		then	echo "No such file: $ld"; continue
		fi
		case "$ld"
		in /*)	: OK
		;; *)	echo "Must be an absolute path: $ld"; continue
		esac
		break
	done
}

checkar()
{
	if [ "" != "$archivist" ]
	then	return
	fi
text1='
The Oberon System needs an archiving program. Luckily, you have installed
the System V features of SunOS which provide a version of ar(1) which
does not need ranlib. Nevertheless, you may override the default,
especially if not all your Suns have access to /usr/5bin/ar.
'
text2='
The Oberon System needs an archiving program. Regrettably, you have not
installed the System V option on your system which provides a version
of ar(1) which does not need ranlib. The default is now to live with
/usr/bin/ar and /usr/bin/ranlib. Nevertheless, you may override the
default, especially if you have an alternative floating around which
avoids ranlib.
'
text3='
Does the specified archiving program need ranlib or not?
'
text4='
OK -- your archiving program needs ranlib. Which ranlib should
do the job? Usually, ranlib as provided by Sun should do it.
'
	if [ -x /usr/5bin/ar ]
	then	arpath=/usr/5bin/ar text="$text1"
	else	arpath=`PATH="$path" type ar | sed 's/^.*is //'`
		case $arpath
		in *"not found"*)
			if [ -x /usr/bin/ar ]
			then	arpath=/usr/bin/ar
			else	arpath=/bin/ar
			fi
		esac
		text="$text2"
	fi
	while :
	do	gets "Path of ar" archivist "$arpath" "$text"
		if [ ! -f "$archivist" ]
		then	echo "No such file: $archivist"; continue
		fi
		case "$archivist"
		in /*)	: OK
		;; *)	echo "Must be an absolute path: $archivist"; continue
		esac
		break
	done
	if [ "/usr/5bin/ar" = "$archivist" ]
	then	needranlib=FALSE
	elif [ "/usr/bin/ar" = "$archivist" ]
	then	needranlib=TRUE
	else	gets "ranlib needed?" answer "yes" "$text3"
		case $answer
		in yes)	needranlib=TRUE
		;; *)	needranlib=FALSE
		esac
	fi
	if [ "$needranlib" = FALSE ]
	then	ranlib=/usr/bin/ranlib; return
	fi

	ranlibpath=`PATH="$path" type ranlib | sed 's/^.*is //'`
	case $ranlibpath
	in *"not found"*)
		if [ -x /usr/bin/ranlib ]
		then	ranlibpath=/usr/bin/ranlib
		else	ranlibpath=/bin/ranlib
		fi
	esac
	while :
	do	gets "Path of ranlib" ranlib "$ranlibpath" "$text4"
		if [ ! -f "$ranlib" ]
		then	echo "No such file: $ranlib"; continue
		fi
		case "$ranlib"
		in /*)	: OK
		;; *)	echo "Must be an absolute path: $ranlib"; continue
		esac
		break
	done
}

#------------------------------------------------------------------------------
# checkversion
#    sets version, DistributedBy and DistributionDate
#------------------------------------------------------------------------------
checkversion()
{
	for param in distributedBy distributionDate distributionVersion
	do	if [ ! -f install/$param ]
		then	error "Cannot find install/$param"; quit
		fi
		eval $param='`cat install/$param`'
	done
	version="$distributionVersion"
}

#------------------------------------------------------------------------------
# checkmake
#    sets gmake
#------------------------------------------------------------------------------
checkmake()
{
	if [ "" != "$gmake" ]
	then	return
	fi
text1='
The make utility as provided by Sun is not able to work for makefiles
generated by mmo, the makefile-generator for Oberon. Instead, we
need GNU-make, preferably of version 3.58 or 3.70. If you have
intermediate versions (e.g. 3.60) of GNU-make, you may use them at
your own risk but we have had some bad experiences with them due to new bugs.

The rest of the installation process depends on the choice of a
working GNU-make. Because of possible troubles, a precompiled
GNU-make of version 3.70 is shipped with this distribution.

Do you still prefer your own GNU-make or do you have already make-3.70
installed?
'
text2='
Where to install the shipped GNU-make and under which name?
Please give a complete pathname consisting of the directory and
the name of your choice.
'
text3='
Where to find your GNU-make? Please give the complete pathname of it.
'
	gets "Taking your own GNU-make?" answer no "$text1"
	case $answer
	in no|n)
		if [ -d /usr/local/bin ]
		then	path=/usr/local/bin
		else	path=/usr/bin
		fi
		while :
		do	gets "Pathname" gmakepath $path/gmake "$text2"
			dir=`dirname $gmakepath`
			if [ "" = "$dir" ]
			then	echo "Directory is missing: $gmakepath"
				continue
			fi
			if [ ! -d "$dir" ]
			then	echo "No such directory: $dir"
				continue
			fi
			if [ -d "$gmakepath" ]
			then	echo "Is a directory: $gmakepath"
				continue
			fi
			if [ -f "$gmakepath" ]
			then	echo "Exists already: $gmakepath"
				continue
			fi
			if cp gnu/make-3.70 "$gmakepath"
			then	gmake="$gmakepath"
			else	echo "Copy failed."; continue
			fi
			break
		done
	;; y|yes)
		while :
		do	gets "Path" gmakepath "??" "$text3"
			case $gmakepath
			in /*)	: OK
			;; *) 	echo Please give an absolute path name
				continue
			esac
			if [ ! -f "$gmakepath" ]
			then	echo "No such file: $gmakepath"
				continue
			fi
			gmake="$gmakepath"
			break
		done
	;; *)	error "That's neither yes nor no...?"; quit
	esac
}

#------------------------------------------------------------------------------
# check for existence of given directory and create it, if necessary
#------------------------------------------------------------------------------
checkdir()
{	dir="$1"

	test -d $dir ||
	{	if mkdir $dir
		then	chall $dir
			echo; echo "*** $dir created"
		else	error cannot create $dir; return 1
		fi
	}
	return 0
}

#------------------------------------------------------------------------------
# update_sysconf
#    examine all necessary parameters and incorporate them into SysConf
#------------------------------------------------------------------------------
update_sysconf()
{
	checkrel
	checklib
	checkas
	checkld
	checkar
	checkmake
	checknewer
	checkversion

	unset OBAS
	unset OBAR
	unset OBLD
	unset OBRANLIB
	unset OBRANLIB

	assembler=`pwd`/gnu/gas-1.36

	echo; echo "Two modules of the library need to be recompiled."
	echo "This takes 2-3 minutes."; echo

	(	cd src/lib/conf &&
		OBLIB="$libdir" sh conf.sh -m SysConf \
			assembler="$m20as" \
			linkeditor="$ld" \
			archivist="$archivist" \
			needRanlib="$needranlib" \
			ranlib="$ranlib" \
			version="$version" \
			distributedBy="$distributedBy" \
			distributionDate="$distributionDate" &&
		cd .. &&
		OBAS="$assembler" OBLD="$ld" \
			OBAR="$archivist" OBNEEDRANLIB="$needranlib" \
			OBRANLIB="$ranlib" \
			$gmake -r OC=../../../bin/oc OBLIB=../../../lib \
				NEWER="$newer"
	) 2>&1 | tee src/lib/PROT | sed 's/^/ *** /'
	echo; echo Done.
}

#------------------------------------------------------------------------------
# install Oberon library directory
#------------------------------------------------------------------------------
ilib()
{
	if installed 1
	then	: OK
	else	echo "part 1 of the installation must be done first"
		return 1
	fi

	checklib
	checkas

	if	( cd lib; tar cf - . ) | ( cd "$libdir"; tar xf - ) &&
		chall $libdir/* $libdir/*/* &&
		find $libdir -name '*.a' -exec ranlib -t {} \;
	then	: OK
	else	error "cannot copy to $libdir"; return 1
	fi
	case $m20as
	in $libdir/*)	cp gnu/gas-1.36 $m20as
	esac
}

#------------------------------------------------------------------------------
# install oc-command
#------------------------------------------------------------------------------
ioc()
{
	if installed 1
	then	: OK
	else	echo "part 1 of the installation must be done first"
		return 1
	fi

	checkversion
	checklib
	checkas
	checkar
	checkld
	checkmake
	checknewer
	checkbin
	text1="
<<< CAUTION >>>
oc exists already in $bindir.
Is the old version to be moved to $bindir/oc.old?"
	echo; echo 'oc needs to be recompiled. This takes 2 minutes.'; echo
	if [ -f $bindir/oc ]
	then	gets "Saving $bindir/oc?" answer "yes" "$text1"
		if [ "$answer" = "yes" ]
		then	mv $bindir/oc $bindir/oc.old
		else	rm $bindir/oc
		fi
	fi
	assembler=`pwd`/gnu/gas-1.36
	(	cd src/oc &&
		rm -f test_oc &&
		OBAS="$assembler" OBLD="$ld" \
			OBAR="$archivist" OBNEEDRANLIB="$needranlib" \
			OBRANLIB="$ranlib" \
			$gmake -r \
				OC=../../bin/oc OBLIB=../../lib \
				NEWER="$newer" \
				InstallBin="$bindir" \
				install &&
		chall $bindir/oc
	) >src/oc/PROT 2>&1 ||
	{	echo "Recompilation of oc failed. Errors are in"
		echo "src/oc/PROT."
		quit
	}
}

#------------------------------------------------------------------------------
# install mmo-command
#------------------------------------------------------------------------------
immo()
{
	if installed 1
	then	: OK
	else	echo "part 1 of the installation must be done first"
		return 1
	fi

	checkversion
	checklib
	checkmake
	checkbin
	checknewer

	text1="
<<< CAUTION >>>
mmo exists already in $bindir.
Is the old version to be moved to $bindir/mmo.old?"
	echo; echo 'mmo needs to be recompiled. This takes 2 minutes.'; echo
	if [ -f $bindir/mmo ]
	then	gets "Saving $bindir/mmo?" answer "yes" "$text1"
		if [ "$answer" = "yes" ]
		then	mv $bindir/mmo $bindir/mmo.old
		else	rm $bindir/mmo
		fi
	fi
	assembler=`pwd`/gnu/gas-1.36
	(	cd src/mmo &&
		rm -f mmo &&
		OBAS="$assembler" OBLD="$ld" \
			OBAR="$archivist" OBNEEDRANLIB="$needranlib" \
			OBRANLIB="$ranlib" \
			$gmake -r \
				OC=../../bin/oc OBLIB=../../lib \
				NEWER="$newer" \
				InstallBin="$bindir" \
				InstallLib="$libdir" \
				install &&
		chall $bindir/mmo
	) >src/mmo/PROT 2>&1 ||
	{	echo "Recompilation of mmo failed. Errors are in"
		echo "src/mmo/PROT."
		quit
	}
	cp $newer $bindir && chall $bindir/newer
}

#------------------------------------------------------------------------------
# recompile and install the Oberon debugger (odb)
#------------------------------------------------------------------------------
iodb()
{
	trap "rm -f /tmp/i$$; quit" 1 2 3 15

	checkbin
	checklib
	checkrel
	text="
<<< CAUTION >>>
odb is already in $bindir.
Give \"yes\" if it is to be replaced by the new one."
	if [ -f $bindir/odb ]
	then	gets "Update $bindir/odb?" answer "no" "$text"
		case $answer
		in no)	return
		esac
	fi

	flags=
	case $rel
	in 4.*)	flags="$flags -DSUNOS4"
	esac
	flags="$flags -DM68=1 -DM68010=0 -DM68020=1"
	flags="$flags -DVERSION="'\"'"$version"'\"'
	flags="$flags -DOBLIBDIR="'\"'"$libdir"'\"'

	cat <<!

The Oberon debugger (odb) needs to be recompiled.
This takes 3-4 minutes.

!
	(cd src/odb;
	 if make CFLAGS="$flags" >PROT 2>&1
	 then	cp odb $bindir && chall $bindir/odb
	 else	error "Cannot rebuild odb. Errors are in ./src/odb/PROT."
	 fi
	)
}

#------------------------------------------------------------------------------
# installation of Oberon manuals
#------------------------------------------------------------------------------
iman()
{
	checklib
	checkmake

	if [ "" != "$mandir" ]
	then	defman="$mandir"
	else	case $MANPATH
		in /usr/man:?*)	defman=`expr $MANPATH : '/usr/man:\([^:][^:]*\).*'`
		;; ?*:?*)	defman=`expr $MANPATH : '\([^:][^:]*\):.*'`
		;; "")		defman=/usr/man
		;; *)		defman=$MANPATH
		esac
	fi
	text1='
Where to install unformatted manuals? Possible would be '"$defman"'.
The subdirectories man1, man3 and man5 will be created as necessary.'
	ok=FALSE
	until [ $ok = TRUE ]
	do	gets "man-directory" mandir "$defman" "$text1"
		if checkdir $mandir && checkdir $mandir/man1 &&
		   checkdir $mandir/man3 && checkdir $mandir/man5
		then	if [ -w $mandir/man1 ] && [ -w $mandir/man3 ] &&
			   [ -w $mandir/man5 ]
			then	ok=TRUE
			else	error "no write-permission in $mandir/man[135]"
			fi
		fi
	done

	case $mandir
	in /*)	: OK
	;; *)	mandir=`pwd`/$mandir
	esac

	echo
	echo "The installation of the manuals takes up to 13-15 minutes."

	(	cd doc/man &&
		$gmake -r \
			InstallMan="$mandir" InstallLib="$libdir" \
			Release="$version"
	) >doc/man/PROT 2>&1 ||
	{	error "Installation of manuals failed. Errors are in doc/man/PROT."
		return 1
	}
	chall $mandir/*/*
}

#------------------------------------------------------------------------------
# format manual entries
#------------------------------------------------------------------------------
fman()
{
	if installed 6
	then	: OK
	else	echo "part 6 of the installation must be done first"
		return 1
	fi

	text2='
Where to install formatted manuals? Possible would be '"$mandir"'.
Again, the subdirectories cat1, cat3 and cat5 will be created as necessary.
Give ``-'"''"' if manuals are not to be formatted.'
	ok=FALSE
	until [ $ok = TRUE ]
	do	gets "man-directory" catmandir "$mandir" "$text2"
		case $catmandir
		in -)	return
		;; *)	if checkdir $catmandir && checkdir $catmandir/cat1 &&
			   checkdir $catmandir/cat3 && checkdir $catmandir/cat5
			then	if [ -w $catmandir/cat1 ] &&
				   [ -w $catmandir/cat3 ] &&
				   [ -w $catmandir/cat5 ]
				then	ok=TRUE
				else	echo "no write-permission in $catmandir/cat[135]"
				fi
			fi
		esac
	done

	groffpath=`PATH="$path" type groff | sed 's/^.*is //'`
	case $groffpath
	in *"not found"*)
		groffpath=
	esac
	nroffpath=`PATH="$path" type nroff | sed 's/^.*is //'`
	case $nroffpath
	in *"not found"*)
		nroffpath=
	esac

	text3='
The manuals should be preferably formatted by the groff package
and luckily there is one on your system. Press [RETURN] to take
the one been found or give an alternative.
'
	text4='
While the manuals should be preferably formatted by the groff package
it seems that you have only nroff installed. If you have groff
anywhere floating around you can give its path otherwise we have
to live with nroff.
'
	text5='
No formatting package was found on your system (neither groff nor
nroff). You may either specify a formatter (must be nroff or
groff and including this name) or give up by pressing [RETURN].
'
	if [ "" != "$groffpath" -o "" != "$nroffpath" ]
	then	if [ "" != "$groffpath" ]
		then	text="$text3" suggestion="$groffpath"
		else	text="$text4" suggestion="$nroffpath"
		fi
	else	text="$text5" suggestion=
	fi
	while :
	do	gets "Path of groff or nroff" formatter "$suggestion" "$text"
		if [ "" = "$formatter" ]
		then	echo OK -- giving up
			return
		fi
		case $formatter
		in /*)	: OK
		;; *)	echo "Need an absolute pathname"; continue
		esac
		case $formatter
		in *groff*)	choice=groff
		;; *nroff*)	choice=nroff
		;; *)		echo "That's neither groff nor nroff!"
				continue
		esac
		if [ ! -f "$formatter" ]
		then	echo "No such file: $formatter"; continue
		fi
		if [ "$choice" = nroff ]
		then	for tool in tbl eqn col
			do	path=`dirname $formatter`/$tool
				if [ ! -f "$path" ]
				then	echo "No such file: $path"; continue 2
				fi
				eval "\$tool=$path"
			done
		fi
		break
	done

	cat <<!

The manuals will be formatted now. This takes 15-20 minutes.

!
	for manual in doc/man/man[135]/*.*
	do	chapter=`expr $manual : 'doc/man/man\(.\)/.*'`
		manual=`basename $manual`
		src="$mandir"/man$chapter/$manual
		dest="$catmandir"/cat$chapter/$manual
		case $choice
		in nroff)
			$eqn $src | $tbl 2>/dev/null |
			$formatter -Ttn300 -n1 -man | $col >$dest
		;; groff)
			$formatter -e -t -man -Tascii $src >$dest
		esac
		chall $dest
	done
}

#------------------------------------------------------------------------------
# main
#------------------------------------------------------------------------------

checkloc
getconf

text1='
*** Oberon installation procedure ***

Please select a set of the installation parts following

   1:  preparation of oc and the library           x
   2:  oc (driver of the compiler like cc)         x
   3:  compiler and library (in ./lib directory)   x
   4:  mmo (Oberon makefile generator)             x
   5:  odb (Oberon debugger)                       x
   6:  manuals (in ./doc/man directory)            x
   7:  formatting manuals                          x
'

script='echo "$text1" |
while read line
do	echo -n "$line" | tr x " "
	case "$line"
	in *[1-7]:*)
		part=`expr "$line" : '"'"'.*\([1-7]\):.*'"'"'`
		if installed $part
		then	echo "   *** installed ***"
		else	echo
		fi
	;; *) echo
	esac
done'
text2=`eval "$script"`

gets "set of 1-7" iparts "$tbd" "$text2"

checkuid

for ipart in $iparts
do	case $ipart
	in 1)	update_sysconf
	;; 2)	ioc
	;; 3)	ilib
	;; 4)	immo
	;; 5)	iodb
	;; 6)	iman
	;; 7)	fman
	esac &&
	install_ok $ipart
done

saveconf

echo; echo "*** Installation procedure finished."; echo
