#!/bin/sh
#------------------------------------------------------------------------------
# Modula-2 system -- interactive installation procedure
# (C) 1984-1998 University of Ulm, SAI, 89069 Ulm, Germany
# afb 4/88
# afb 6/90: binary installation
# afb 9/96: Solaris 2.x
# dwo 2/98: Solaris 2.6
# changed l367-384, l102 add l386, l394 echo "....
# afb 6/98: update makefile of mmm to switch off -L flag
#------------------------------------------------------------------------------

	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.0b8"

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
}

#------------------------------------------------------------------------------
# try to locate a command in $path (the original PATH)
# sets located_cmd to the path if found
#------------------------------------------------------------------------------
locate()
{	if [ ! -f /usr/ucb/whereis ]
	then	return 1
	fi
	located_cmd=`PATH="$PATH:$path" whereis -b "$1" | awk '{ print $2 }'` # changed $path to $PATH:$path  
	if [ "" != "$located_cmd" ]
	then	return 0
	else	return 1
	fi
}

#------------------------------------------------------------------------------
# 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`
	case $rel
	in 5.*)		: OK
	;; *)	error "Your release is not supported!"
		echo "This distribution supports Solaris 2.x only"
		exit 1
	esac
	arch=`uname -m`
	case $arch
	in sun4*)	: OK
	;; *)	error "This distribution supports SPARCv8 only"
		exit 1
	esac
	for cmd in ar make
	do	if [ ! -f /usr/ccs/bin/$cmd ]
		then	error "This distribution requires SUNWbtool."
			echo "Please install this Solaris package first."
		fi
	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 overridden 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 /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
		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 -p $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 -p "$libdir" && chall "$libdir"
		then	echo; echo "*** $libdir: created"
		elif [ ! -d "$libdir" ]
		then	error "$libdir: cannot create"; continue
		fi
		# Note that we cannot use make here because
		# our makefiles require GNU-make which
		# is not part of Solaris...
		(  cd src/lib/std &&
		   ./updateSysConf "$libdir" "$version" &&
		   echo &&
		   echo "Updating library module SysConf..." &&
		   MODLIB=../../../lib ../../../bin/m2c -c SysConf.m2 &&
		   ar r libm2.a SysConf.o &&
		   rm -f SysConf.o &&
		   echo "Library successfully updated" ||
		   echo "Unable to update SysConf!"
		   echo
		)
		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."
	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 [ -f /opt/SUNWspro/bin/cc ]
	then	cc=/opt/SUNWspro/bin/cc
	else	cc=`whereis gcc | awk '{ print $2 }'`
#------------------------------------------------------------------------------
# original code 
#------------------------------------------------------------------------------
#	if [ ! -f $cc ]
#		then	cc=
#			gets "Your C-compiler: " cc "" "$text2"
#		fi
#------------------------------------------------------------------------------
# new code, avoids script terminating when variable $cc empty (Solaris 2.6),
# happens when gcc in a directory like /usr/local/bin or /opt/local/bin,
# because whereis search path limited, "-z" does not work too
#------------------------------------------------------------------------------
		if [ $cc="" ]
               	then    cc=
                       	gets "Your C-compiler (full PATH): " cc "" "$text2"
        	fi
	fi

	if [ -f $cc ]
	then	 echo "... building binary" # added flow control 
                 (cd src/m2c &&
		 make "CC=$cc" "BINDIR=$bindir" "LIBDIR=$libdir" \
		      "VERSION=$version" install >PROT 2>&1 ||
		 { echo 'Cannot rebuild m2c. Errors are in src/m2c/PROT.';
		   exit 1
		 }
		) && chall $bindir/m2c
	else    echo "... copying from binary distribution" # added flow control 
		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
	(  cd src/mmm &&
	   echo && echo "Updating Makefile-Generator..." &&
	   ../../bin/mmm -u makefile -L &&
	   MODLIB=../../lib make M2C=../../bin/m2c &&
	   mv ../../bin/mmm ../../bin/mmm.orig &&
	   mv mmm ../../bin &&
	   echo "Makefile-Generator successfully updated!" ||
	   echo "Unable to update Makefile-Generator!"
	   echo
	)
	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()
{
	checklib
	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

	for manual in man/man1/*.1
	do	target=$mandir/man1/`basename $manual`
		man/bin/patch_macros \
			-l "$libdir" -m man/misc/macros -r "$version" \
			$manual >$target
		chall $target
	done
	for manual in man/man3/*.3
	do	target=$mandir/man3/`basename $manual`
		man/bin/patch_macros \
			-l "$libdir" -m man/misc/macros -r "$version" \
			$manual >$target
		chall $target
	done

	/usr/bin/catman -w -M $mandir

	if locate groff
	then	groff="$located_cmd"
	else	if [ ! -f /usr/bin/nroff ]
		then	error "Warning: No text formatting package found (nroff etc.)"
			return
		fi
		groff=
	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"
					continue
				fi
			fi
		esac
	done

	cat <<!

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

!
	for manual in "$mandir"/man1/* "$mandir"/man3/*
	do	case $manual
		in */man1/*)	dest=$catmandir/cat1/`basename $manual`
		;; */man3/*)	dest=$catmandir/cat3/`basename $manual`
		esac
		if [ "" != "$groff" ]
		then	"$groff" -t -e -Tlatin1 -man $manual >$dest 2>/dev/null
		else
			tbl $manual 2>/dev/null | nroff -Ttn300 -n1 -man | col >$dest
		fi
		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
