#!/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
# afb 9/96: Solaris 2.x
#------------------------------------------------------------------------------

	path="$PATH"		# keep original path
	PATH=/bin:/usr/bin:/usr/ccs/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
	version="3.0"

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 "$prompt" "[$default] \c"
		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()
{
	rel=`/usr/bin/uname -r`
	text='
Give the release number of your SunOS system.'
	ok=FALSE
	until [ $ok = TRUE ]
	do	gets release rel $rel "$text"
		case $rel
		in 5.*)		ok=TRUE
		;; *)	echo 'Your release is not supported!'; ok=FALSE
			echo 'This distribution supports SunOS 5.x only.'
		esac
	done
}

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

#------------------------------------------------------------------------------
# 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 /usr/local/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 get 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/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
		else	error "cannot copy to $libdir"
			continue
		fi
	done
}

#------------------------------------------------------------------------------
# install m2c-command
#------------------------------------------------------------------------------
im2c()
{
	checklib
	checkbin
	text1="
<<< CAUTION >>>
m2c exists already in $bindir.
Is the old version to be moved to $bindir/m2c.old?"
	text2="
<<< CAUTION >>>
A C-compiler was not found on your system which would be necessary
to recompile m2c. If you don't have a C-compiler on your system,
the precompiled version will be installed which needs the
environment variable MODLIB to find your local Modula-2 library
directory."
	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
	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
	if [ -x /opt/SUNWspro/bin/cc ]
	then	cc=/opt/SUNWspro/bin/cc
	else	cc=`whereis gcc | awk '{ print $2 }'`
		if [ ! -x $cc ]
		then	cc=
			gets "Your C-compiler: " cc "" "$text2"
		fi
	fi

	if [ -x $cc ]
	then	case $cc in
		*gcc*)	cc="$cc -traditional"
		esac
		(cd src/m2c &&
		 make "CC=$cc" "BINDIR=$bindir" "LIBDIR=$libdir" \
		      "PR=$prcmd" "VERSION=$version" >PROT 2>&1 ||
		 { echo 'Cannot rebuild m2c. Errors are in src/m2c/PROT.';
		   exit 1
		 }
		) && chall $bindir/m2c
	else	cp bin/m2c $bindir && chall $bindir/m2c
	fi
}

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

#------------------------------------------------------------------------------
# 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 for Solaris 2.x / SPARCv8 ***

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:  manuals (in ./man directory)
'

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

checkuid

call_checkrel=
for ipart in $iparts
do	case $ipart
	in 1)	call_checkrel=ON
	;; 2)	call_checkrel=ON
	;; 3)
	;; 4)
	;; *)	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)	iman
	esac
done

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