#!/bin/sh
#
# Copyright (C) 1998 Florian La Roche <florian@knorke.saar.de>
# See the GNU Copyright...
#

ErrorExit()
{
   echo $*
   exit 1
}

Usage()
{
  cat <<EOF
Make - Handle compilation and installation of source packages.
Copyright (C) 1996-1998 Florian La Roche <florian@knorke.saar.de>

All source packages must be tar'd and gzip'd/bzip2'd. Normally, they shouldn't
be modified from the general public release. Instead, all changes for Linux
are stored in an extra context diff. The context diff tries to install
everything with all features included and in a good place.

In the following description, "package" refers to a tar'd and gzip'd source
code, which is in the file "package.tar.gz" and the corresponding context
diff is in "package.dif".

Make extract package	Untar "package.tar.gz" and patch it with "package.dif".
Make compile package	Change into the source directory, call make for
			"Makefile.Linux" with the target 'compile'.
Make install package	Change into the source directory and call make for
			"Makefile.Linux" with the target 'install'.
Make diff package	Make new context diffs for that source package.
Make remove package	Remove the extracted source directory.

If you have a new source package, called "diff-2.6.tar.gz", extract it with
"tar xzf diff-2.6.tar.gz". Then change into the source directory with
"cd diff-2.6". Before you change any file in the source directory, you have
to save the old version with "ci -l filename". Just enter "." to the
asked question. This will create a file "filename,v", which contains the old
source code. If you make context diffs later on, your source directory is just
scanned for "*,v" and context diffs are generated for those files.
If you have to create a new file, first create an empty file with "touch file"
and then save it with "ci -l file".
You have to create the file "Makefile.Linux", which is for GNU make, with the
targets 'compile' and 'install'.
'compile' just compiles everything in the source directory.
'install' should be invoked as root, so that everything can be installed.
EOF
  return
}

Extract()
{
	echo -n "Decompress and extract $1, "
	if test -f $1.tar.gz ; then
		tar xzf $1.tar.gz	|| ErrorExit "Tar failed on $1."
	elif test -f $1.tar.bz2 ; then
		tar xIf $1.tar.bz2	|| ErrorExit "Tar failed on $1."
	else
		ErrorExit "Cannot find $1.tar.gz/bz2."
	fi
	cd $1				|| ErrorExit "cd failed on $1."
	echo -n "set up rcs, "
	for i in `awk '/^--- / { print $2 }' ../$1.dif | sort -u` ; do
		mkdir -p `dirname $i`
		touch $i
		ci -q -t-Original -i $i	|| ErrorExit "ci failed on $i."
		co -q -l -ko $i		|| ErrorExit "co failed on $i."
	done
	echo "patch sources."
	patch -s -p0 <../$1.dif	|| ErrorExit "Patch failed on $1."
	cd ..
}

Diff()
{
	echo "Make rcs diffs for $1"
	cd $1 || ErrorExit "Cd failed on $1."
	find . -name '*,v' -o -name ".*,v" | \
		sed -e 's/,v$//' -e 's/^\.\///' | sort | \
		xargs -r -ihugo rcsdiff -ko -q -u -L hugo hugo > ../$1.dif
	cd ..
}

Make()
{
	echo "Make $1 in subdir $2."
	make $1 -f Makefile.Linux -C $2 || ErrorExit "Make $1 failed on $input."
}

Remove()
{
	test -f $1.tar.gz -a -f $1.dif || ErrorExit "Nothing to remove."
	echo "Remove directory $1."
	rm -fr $1
}

if [ $# = 2 ]; then
   if [ extract = "$1" -o e = "$1" ]; then
	Extract $2
   elif [ "$1" = compile -o "$1" = c -o "$1" = install -o "$1" = i ]; then
	Make $1 $2
   elif [ "$1" = "diff" -o "$1" = d ]; then
	Diff $2
   elif [ "$1" = "remove" ]; then
	Remove $2
   else
	Usage
   fi
else
   Usage
fi
exit 0
