#!/bin/sh
#------------------------------------------------------------------------------
# Modula-2 system -- interactive installation procedure
# (c) University of Ulm, Sektion Informatik, D-7900 Ulm
# afb 4/88
# afb 6/90: binary installation
#------------------------------------------------------------------------------

	path="$PATH"		# keep original path
	PATH=/bin:/usr/bin:/usr/ucb:/etc # be sure to call commands we know about
	libdir=			# determined by ilib or checklib
	bindir=			# determined by checkbin
	uid=			# determined by checkuid
	rel=			# determined by checkrel
	m20as=			# determined by checkas
	gas_version=		# determined by checkas
	version="2.2.1"

trap "quit" 1 2 3 15

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

quit()
{
	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=`id | sed 's/uid=\([0-9][0-9]*\).*/\1/'`
	# be carefully, id is be optional (System V set)
	case $uid
	in [0-9]*)	case $uid
			in 0)	# ok - we are super-user
			;; *)	error Warning: You are not super-user.; return
			esac
	;; *)		case `whoami`
			in root)
				uid=0
			;; *)	error Warning: You are not super-user.; return
				uid=`whoami`
			esac
	esac

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

#------------------------------------------------------------------------------
# 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 bin ] || [ ! -d man ] || [ ! -d lib ] || [ ! -f bin/m2c ]
	then	error $0 not started in ./modula
		exit 1
	fi
}

#------------------------------------------------------------------------------
# check for SunOS release
#------------------------------------------------------------------------------
checkrel()
{
	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.'
	ok=FALSE
	until [ $ok = TRUE ]
	do	gets release rel $rel "$text"
		case $rel
		in 3.[0-9]*)	ok=TRUE
		;; 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
}

#------------------------------------------------------------------------------
# where to find the MC68020-assembler
#------------------------------------------------------------------------------
checkas()
{
	text1='
<<< CAUTION >>>
The SunOS 4.1 assembler does *not* work for the Modula-2 compiler
(some new bugs were introduced from 4.0.3 to 4.1).
Please give the complete pathname of an alternative assembler,
preferably GNU assembler (gas).'
	text2="
<<< CAUTION >>>
This is not gas version 1.36 which is the only assembler which
is guaranteed to work with this Modula-2 system. You have three
options:
(1) Enter "'"'"yes"'"'" and use the given assembler at your own risk.
(2) Give "'"'"no"'"'" to specify another assembler.
(3) 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."
	case $rel
	in 3.*)	m20as=/bin/as
	;; 4.*)	m20as=/usr/bin/as
	esac
	if [ $rel = 4.1 -o $rel = 4.1.1 -o $rel = 4.1.1_U1 ]
	then	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=`type as | sed 's/^.*is //'`
			fi
		esac
		while :
		do	gets "MC68020-assembler" m20as "$gaspath" "$text1"
			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
					;; *) continue
					esac
			;; *)		gas_version=
			esac
			break
		done
	fi
}

#------------------------------------------------------------------------------
# where to install the Modula-2 library directory?
#------------------------------------------------------------------------------
checklib()
{
	case $libdir
	in "")
	;; *)	return
	esac

text1="
Please give the complete path name of the library directory
you have installed in an earlier $0-run."
text2='
<<< CAUTION >>>
The compiler cannot be invoked without correctly installed library directory.
Give "no" if you decide to give another directory. On "yes", m2c will be
installed for the given library directory. In each case the default library
directory can be overrided by setting the environment parameter MODLIB.'
	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 /home/`hostname`/lib/modula "$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
		ok=TRUE
		if [ -d $libdir ]
		then	for file in m0 m1 m2 m3 msym m2rt0.o libm2.a m2e
			do	if [ ! -f $libdir/$file ]
				then	error Warning: $libdir/$file not found
					ok=FALSE
					break
				fi
			done
		else	error Warning: $libdir: no such directory
			ok=FALSE
		fi
		if [ $ok = FALSE ]
		then	gets "$libdir OK?" answer no "$text2"
			if [ "$answer" = yes ]
			then	ok=TRUE
			fi
		fi
	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/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
}

#------------------------------------------------------------------------------
# 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
}

#------------------------------------------------------------------------------
# install Modula-2 library directory
#------------------------------------------------------------------------------
ilib()
{
	text1='
The Modula-2 compiler needs a library directory.
Typically /usr/lib/modula or /u/lib/modula.'
	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 /home/`hostname`/lib/modula "$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
		if cp lib/* "$libdir" && chall $libdir/*
		then	ok=TRUE
			for archive in $libdir/*.a
			do	ranlib $archive
			done
		else	error "cannot copy to $libdir"
			continue
		fi
	done
#	case $libdir
#	in /usr/lib/modula|/u/lib/modula)	# ok
#	;; *)	echo 'Insert "MODLIB='$libdir'; export MODLIB" into /etc/profile.'
#		echo 'To continue the installation type <ctrl>-d.'
#		PS1="$0 $ " PATH="$path" ${SHELL-/bin/sh}
#		echo; echo 'OK. Installation continues.'
#	esac
}

#------------------------------------------------------------------------------
# install m2c-command
#------------------------------------------------------------------------------
im2c()
{
	checklib
	checkas
	checkbin
	text1="
<<< CAUTION >>>
m2c exists already in $bindir.
Is the old version to be moved to $bindir/m2c.old?"
	if [ -x /bin/pr ]
	then	prcmd=/bin/pr
	else	prcmd=/usr/bin/pr
	fi
	echo; echo 'm2c needs to be recompiled. This takes 2 minutes.'; echo
	defs="-DFP=1" # FP-support isnt optional
	if [ "1.36" = "$gas_version" ]
	then	defs="$defs -DGAS136"
	fi
	if [ -f $bindir/m2c ]
	then	gets "Saving $bindir/m2c?" answer "yes" "$text1"
		if [ "$answer" = "yes" ]
		then	mv $bindir/m2c $bindir/m2c.old
		else	rm $bindir/m2c
		fi
	fi
	(cd src/m2c &&
	 make "BINDIR=$bindir" "LIBDIR=$libdir" \
	      "M20AS=$m20as" "PR=$prcmd" "DEFS=$defs" >PROT 2>&1 ||
	 { echo 'Cannot rebuild m2c. Errors are in src/m2c/PROT.'; exit 1; }
	) && chall $bindir/m2c
#	echo; echo 'Insert "MC68881=YES; export MC68881" into /etc/profile.'
#	echo 'To continue the installation type <ctrl>-d.'
#	PS1="$0 $ " ${SHELL-/bin/sh}
#	echo; echo 'OK. Installation continues.'
}

#------------------------------------------------------------------------------
# install binaries (except of m2c and mdb)
#------------------------------------------------------------------------------
ibin()
{
	checkbin
	text="
<<< CAUTION >>>
One of the Modula-2 utilities is already in $bindir.
Give \"yes\" if it is to be replaced by the new one."
	answer=no
	for file in m2b mmm mprof mtags
	do	if [ -f bin/$file ]
		then	if [ -f $bindir/$file ]
			then	gets "Update $bindir/$file?" answer \
					"$answer" "$text"
				case $answer
				in yes)	ok=TRUE
				;; *)	ok=FALSE answer=no
				esac
			else	ok=TRUE
			fi
			if [ $ok = TRUE ]
			then	cp bin/$file $bindir && chall $bindir/$file
			fi
		else	error Warning: $file not on distribution tape
		fi
	done
}

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

	checkbin
	text="
<<< CAUTION >>>
mdb is already in $bindir.
Give \"yes\" if it is to be replaced by the new one."
	if [ -f $bindir/mdb ]
	then	gets "Update $bindir/mdb?" 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"'\"'

	cat <<!

The Modula-2 debugger (mdb) needs to be recompiled.
This takes 3-4 minutes.

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

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

	cp man/man1/* $mandir/man1
	for manual in man/man1/*
	do	chall $mandir/man1/`basename $manual`
	done
	cp man/man3/* $mandir/man3
	for manual in man/man3/*
	do	chall $mandir/man3/`basename $manual`
	done

	if [ ! -f /usr/bin/nroff ]
	then	error "Warning: No text formatting package found (nroff etc.)"
		return
	fi

	text2='
Where to install formatted manuals? Possible would be '"$mandir"'.
Again, the subdirectories cat1 and cat3 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
			then	if [ -w $catmandir/cat1 ] && [ -w $catmandir/cat3 ]
				then	ok=TRUE
				else	echo "no write-permission in $catmandir/cat1 or $catmandir/cat3"
				fi
			fi
		esac
	done

	cat <<!

The manuals will be formatted now. This takes some minutes.

!
	for manual in man/man1/* man/man3/*
	do	case $manual
		in man/man1/*)	dest=$catmandir/cat1/`basename $manual`
		;; man/man3/*)	dest=$catmandir/cat3/`basename $manual`
		esac
		tbl $manual 2>/dev/null | nroff -Ttn300 -n1 -man | col >$dest
		chall $dest
	done
}

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

checkloc

text='
*** Modula-2 installation procedure ***

Please select a set of the installation parts following

   1:  compiler and library (in ./lib directory)
   2:  m2c (driver of the compiler like cc)
   3:  Modula-2 utilities (in ./bin directory)
   4:  mdb (Modula-2 debugger)
   5:  manuals (in ./man directory)
'

gets "set of 1-5" iparts "1 2 3 4 5" "$text"

checkuid

call_checkrel=
for ipart in $iparts
do	case $ipart
	in 1)	call_checkrel=ON
	;; 2)	call_checkrel=ON
	;; 3)
	;; 4)	call_checkrel=ON
	;; 5)
	;; *)	echo Unknown installation part number: $ipart; exit 1
	esac
done
case $call_checkrel
in ON)	checkrel
esac

for ipart in $iparts
do	case $ipart
	in 1)	ilib
	;; 2)	im2c
	;; 3)	ibin
	;; 4)	imdb
	;; 5)	iman
	esac
done

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