#!/bin/sh
#------------------------------------------------------------------------------
# Ulm's Oberon System -- make distribution tape
# Copyright (c) University of Ulm, SAI, D-7900 Ulm, Germany
# afb 4/93
#------------------------------------------------------------------------------

path="$PATH"		# keep original path
PATH=/bin:/usr/bin:/usr/ucb:/etc # be sure to call commands we know about
srcpath=..		# Oberon sources are expected to be found here
abs_srcpath=`pwd`/"$srcpath"
destpath=..		# the oberon distribution directory is to be put here
mmo=/usr/local/bin/mmo
make=/usr/local/bin/make	# GNU make
ci=/usr/local/bin/ci
co=/usr/local/bin/co

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"
		if [ "" != "$default" ]
		then	echo -n " [$default] "
		else	echo -n ": "
		fi
		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
}

#------------------------------------------------------------------------------
# forced_gets prompt shellvar [text]
#------------------------------------------------------------------------------
forced_gets()
{	prompt="$1"
	varname="$2"
	shift; shift
	while	gets "$prompt" "$varname" "" "$@"
		[ "" = "`eval echo '$'$varname`" ]
	do	echo "$prompt must not be empty."
	done
}

#------------------------------------------------------------------------------
# set distributionBy and distributionDate
#------------------------------------------------------------------------------
get_distributor()
{	text1='

Each distribution gets a stamp with the name of the distributing
organization and the time of distribution.
'
	text2='

Organizations which are redistributing Ulm'"'"'s Oberon System in
a modified form are requested to define an abbreviation of
their organizational unit which will be appended to the
version of the distribution. E.g. if you have a version
"0.6" from the original provider, and your abbreviation is
"yac", and your release has the number "0.1", the version becomes
"0.6-yac-0.1". If another company with the abbreviation "sac"
choses to redistribute your version, the version becomes
"0.6-yac-0.1-sac-0.1".

Under normal circumstances, abbreviations should be up to
three lower case letters long. Exceptions get accepted, though.
'
	if [ -r organization ]
	then	ORGANIZATION=`cat organization | sed '/^#/d;' | line`
	fi
	if [ "" = "$ORGANIZATION" ]
	then	forced_gets "Your organization" distributedBy "$text1"
	else	gets "Your organization" distributedBy "$ORGANIZATION" "$text1"
	fi
	organization="$distributedBy"
	echo "$distributedBy" >organization
	if [ -r version_prefix -a -r distributedBy ]
	then	if cmp -s version_prefix distributedBy
		then	: OK
		else	forced_gets "Abbreviation of your organization" \
				abbreviation_of_org "$text2"
			echo "-$abbreviation_of_org" >>version_prefix
		fi
	fi
	gets "Time of distribution" distributionDate "`date`"
}

#------------------------------------------------------------------------------
# sets version and distdir
#------------------------------------------------------------------------------
get_version()
{
	text='
Give a version number which consists of a major and a minor number
which are separated by a dot, e.g. 2.27.
'
	if [ -r version ]
	then	version=`cat version`
	else	version=0.0
	fi
	case $version
	in [0-9]*.[0-9]*)
		major=`expr "$version" : '\([0-9]*\)\..*'`
		minor=`expr "$version" : '.*\.\([0-9]*\)'`
	;; *)	major=0 minor=0
	esac
	minor=`expr $minor + 1`
	def_version="$major.$minor"
	while :
	do	gets "Version" version "$def_version" "$text"
		case $version
		in [0-9]*.[0-9]*)	# OK
		;; *)
			error "bad version number"; continue
		esac
		version_suffix="$version"
		if [ -r version_prefix ]
		then	version=`cat version_prefix`-"$version"
		fi
		distdir="$destpath"/oberon-"$version"
		if [ -d "$distdir" ]
		then	error "$distdir exists already"; continue
		fi
		if mkdir "$distdir"
		then	break
		else	error "Cannot create $distdir"; continue
		fi
	done
	gets "Saving $version_suffix to ./version?" answer yes
	if [ yes = "$answer" ]
	then	[ -r version ] && mv version oldversion
		echo -n "$version_suffix" >version
	fi
}

#------------------------------------------------------------------------------
# link_src pattern source destdir
# we need to eval the case statement: it doesn't work without eval
# for patterns which includes "|"
#------------------------------------------------------------------------------
link_src()
{	p="$1"
	s="$2"
	d="$3"
	script='
	for file in '"$s"'/*
	do	case `basename $file`
		in '$p') if ln $file '$d'
			 then echo -n .
			 else echo FAILED; echo cannot link $file
			      return 1
			 fi
		esac
	done'
	eval "$script"
	echo OK
}

dirtab='
#------------------------------------------------------------------------------
# if directories are to be changed here then check the statements below
# in the main part because they depend on some of these locations
#------------------------------------------------------------------------------
# directory		depends of		pattern
#------------------------------------------------------------------------------
src
src/lib			lib			[MR]*|.mmo_proto
src/lib/base		lib/base		*.o[dm]|make_*|makefile
src/lib/conf		lib/conf		*.o[dm]|make_*|makefile|conf.sh
src/lib/fp		lib/fp			*.o[dm]|make_*|makefile|MC68881.s
src/lib/fpbase		lib/fpbase		*.o[dm]|make_*|makefile
src/lib/if		lib/if			*.o[dm]|make_*|makefile
src/lib/sun		lib/sun			*.o[dm]|make_*|makefile
src/lib/tasks		lib/tasks		*.o[dm]|make_*|makefile
src/lib/unix		lib/unix		*.o[dm]|make_*|makefile
src/lib/math		lib/math		*.o[dm]|make_*|makefile
#------------------------------------------------------------------------------
src/NeWS		NeWS			Makefile
src/NeWS/lib		NeWS/lib		*.o[dm]|makefile|mk_*
src/NeWS/clients	NeWS/clients		*.o[dm]|makefile
#------------------------------------------------------------------------------
src/rts			rts			*.s|Makefile
src/mmo			mmo			*.o[dm]|makefile|mmo_proto
src/newer		newer			*.o[dm]|makefile
src/oberon		oberon			*.d|*.m2|makefile
src/oberon_ref		oberon_ref		*.o[dm]|makefile
src/oc			oc			*.o[dm]|makefile
src/odb			odb			*.[ch]|*.awk|COPYING|makefile
#------------------------------------------------------------------------------
doc			doc			makefile
doc/articles		doc/articles		[a-z]*
doc/bin			doc/bin			[a-z]*
doc/man			doc/man			makefile
doc/man/man1		doc/man/man1		makefile|*.1
doc/man/man3		doc/man/man3		makefile|*.3
doc/man/man5		doc/man/man5		makefile|*.5
doc/misc		doc/misc		macros|header|trailer
doc/tut			doc/tut
doc/tut/streams		doc/tut/streams		[a-z]*
#------------------------------------------------------------------------------
install			install			[a-z]*|READ_ME|COPY*|INSTALL|F*.o[dm]
install			doc/articles		LANGDIFF
#------------------------------------------------------------------------------
# binary stuff
#------------------------------------------------------------------------------
bin			/usr/local/bin		oc|odb|mmo|newer
lib			oberon			oberon
lib			rts			*.o
lib			library			REF|SYM|libo.a
lib/NeWS		library/NeWS		REF|SYM|libNeWS.a
gnu			/usr/local/bin		gas-1.36|make-3.70
'

#------------------------------------------------------------------------------
# main
#------------------------------------------------------------------------------
init_check=`echo "$dirtab" |
sed '/^#/d;/^$/d' |
while read destdir sourcedir pattern
do	case "$sourcedir"
	in /*) : OK
	;; *)  sourcedir="$srcpath/$sourcedir"
	esac
	if [ ! -d "$sourcedir" ]
	then	echo "$sourcedir"
	fi
done`
if [ "" != "$init_check" ]
then	error "Some directories doesn't exist. Check dirtab in mk_tape."
	echo "$init_check" | sed 's/^/   /'
	exit 1
fi

get_distributor
get_version

echo "$dirtab" |
sed '/^#/d;/^$/d' |
while read destdir sourcedir pattern
do	if [ ! -d "$distdir"/"$destdir" ]
	then	if mkdir "$distdir"/"$destdir"
		then	:
		else	echo; error "Cannot create $distdir/$destdir"
			exit 1
		fi
	fi
	if [ "" != "$pattern" ]
	then	case "$sourcedir"
		in /*) : OK
		;; *)  sourcedir="$srcpath/$sourcedir"
		esac
		echo -n "*** $sourcedir --> $destdir ($pattern)"
		link_src "$pattern" "$sourcedir" "$distdir"/"$destdir" ||
		{	echo FAILED
			exit 1
		}
	fi
done

echo "*** Patching src/lib/conf and install"
(	cd "$distdir"/src/lib/conf &&
	rcspath=$abs_srcpath/lib/conf/RCS
	$co -l $rcspath/SysConf.od,v $rcspath/SysConf.om,v
	./conf.sh \
		"version=$version" \
		"distributedBy=$distributedBy" \
		"distributionDate=$distributionDate"
	$ci -u -m"upgraded by install to version $version" \
		$rcspath/SysConf.od,v $rcspath/SysConf.om,v
)
(	cd "$distdir"/install
	echo "$version" >version_prefix
	echo "$distributedBy" >distributedBy
	echo "$distributionDate" >distributionDate
	echo "$version" >distributionVersion
	rm -f organization oldversion version
)
echo "*** Creating symbolic links to REF, SYM and libo.a in the lib directories"
(	cd "$distdir"/src/lib
	for dir in *
	do	if [ -d "$dir" ]
		then	path=../../../lib
			cd "$dir" &&
			{	ln -s $path/REF $path/SYM $path/libo.a .
				cd ..
			}
		fi
	done
)
(	cd "$distdir"/src/NeWS/lib
	path=../../../lib/NeWS
	ln -s $path/REF $path/SYM $path/libNeWS.a .
)
echo "*** Updating makefile for distribution"
(	cd "$distdir"/src/lib
	$make depend
)
(	cd "$distdir"/src
	for dir in mmo newer oc
	do	cd $dir
		$mmo -u makefile -L
		cd ..
	done
)
echo "*** Finishing root directory of distribution"
ln "$distdir"/install/install "$distdir"/install_oberon
ln "$distdir"/install/READ_ME "$distdir"
ln "$distdir"/install/INSTALL "$distdir"
ln "$distdir"/install/LANGDIFF "$distdir"
ln "$distdir"/install/COPY* "$distdir"
ln "$distdir"/install/F*.o[dm] "$distdir"
echo "*** Creating tar-file"
(	dir_name=`basename $distdir`
	cd "$destpath" &&
	tar cf "$dir_name".tar "$dir_name"
)
