#!/bin/bash
# Copyright 2001-2002 William Stearns <wstearns@pobox.com>
# Released under the GPL.
#### This template file is designed to be copied and modified to become an
#### external module for buildkernel.  Our example is for the Goober printer
#### driver.  All instances of the string goober, at a minimum, need
#### modification.  Comments to the developer start with ####

#### None of the following structure is mandatory.  You can strip this
#### all out and put in your own code, even in a different language.
#### Just note that your program will be called 8 times, once each with
#### the following keywords as the sole command line parameter.
#### POSTLOADVARS, POSTGETSOURCE, POSTOPENSOURCE, POSTCONFIGURE
#### POSTKERNELBUILD, POSTMODULEBUILD, POSTINSTALL, POSTCLEAN
####	See below for a description of each.
####
####	Your script should be called bkrun-goober and placed in
#### /var/lib/buildkernel/ .
####	Don't let the length scare you; most of it is comments to you
#### and a somewhat long example of a build.  Just come up with the
#### commands needed to build your code and place them here.

#### Uncomment this to see each line as it's executed.
#set -x

if [ -z "$GOOBERVERSION" ]; then
#### If the user wants to compile your code, he/she needs to set the version
#### of the code they want in /etc/bkrc .  Test for it here, don't do anything
#### if the user didn't set it.  Not everyone has the goober 4000, remember.
	echo GOOBERVERSION unset, skipping Goober.
	exit
fi

exec 2>&1
#### Please set to the project name
PROJECT="goober"
#### Where this script and goober-options can be found.  Leave as is.
BKLIBDIR="${BKLIBDIR:-/var/lib/buildkernel}/"
#### Leave as is.
THISSCRIPT="$BKLIBDIR/bkrun-$PROJECT"   #We can't use $0 as this script is sourced; $0=/usr/bin/buildkernel

####If the user may need to set additional options, they may be set in
####the following file.  Optional.
if [ -f $BKLIBDIR/$PROJECT-options ]; then
	echo $PROJECT loading options
	. $BKLIBDIR/$PROJECT-options
fi
if [ -n "$TEMPGOOBERVERSION" ]; then
	GOOBERVERSION=$TEMPGOOBERVERSION
fi
echo $PROJECT \(Version $GOOBERVERSION\) $1 called from Buildkernel

####The directory in which the build will take place.
GOOBERSOURCEDIR=${GOOBERSOURCEDIR:-"/usr/src/goober-$GOOBERVERSION"}
####The tar file where the source can be found.
GOOBERTAR=${GOOBERTAR:-"$BKSOURCEDIR/goober-$GOOBERVERSION.tar.gz"}
####A test file which will exist if the source has been opened, won't
####if not.
GOOBERTESTFILE=${GOOBERTESTFILE:-"$GOOBERSOURCEDIR/COPYING"}

case $1 in
POSTLOADVARS)
####	When $1=POSTLOADVARS, buildkernel has loaded all config file and
####	command line variables.  No kernel source has been opened.
####
####	Might be a good time to check your own version info.
#### Following used if we have to figure out what the newest beta or stable version is.
	if [ -n "`cat $BKLIBDIR/${PROJECT}-options 2>/dev/null | grep '^TEMPGOOBERVERSION='`" ]; then
		echo Stripping out old $PROJECT version.
		cat $BKLIBDIR/${PROJECT}-options | grep -v '^TEMPGOOBERVERSION=' >$BKLIBDIR/${PROJECT}-options.temp
		cat $BKLIBDIR/${PROJECT}-options.temp >$BKLIBDIR/${PROJECT}-options
		rm -f $BKLIBDIR/${PROJECT}-options.temp
	fi
	;;
POSTGETSOURCE)
####	The kernel source has been retrieved.  You might want to do the
#### same.
####
#### While the example's a little convoluted, in short, do whatever it
#### takes to get the tar down if it's not already here.
	cd $BKSOURCEDIR
#### Used if we have to figure out what the newest beta or stable version is.
	if [ "$GOOBERVERSION" = "NEWESTBETA" ]; then
		echo -n 'Getting the Goober NEWESTBETA snapshot '
		rm -f $BKSOURCEDIR/goober.html
#### bkgetfile is a function that can pull down files via http or ftp.
#### Parameters: protocol, machine name, user, password, directory, filename
		bkgetfile http www.goober.org '' '' software/goober goober.html
#### One example of parsing the html to find a version number.
		TEMPGOOBERVERSION="`cat goober.html | grep goober-snapshot | sed -e 's/.*goober\-//' -e 's/\.tar\.gz.*//'`"
		echo which is version $TEMPGOOBERVERSION .
		rm -f $BKSOURCEDIR/goober-$TEMPGOOBERVERSION.tar.gz
#### Pull down the actual tar.
		bkgetfile http www.goober.org '' '' software/goober goober-$TEMPGOOBERVERSION.tar.gz
		echo >>$BKLIBDIR/${PROJECT}-options
		echo TEMPGOOBERVERSION=$TEMPGOOBERVERSION >>$BKLIBDIR/${PROJECT}-options
	elif [ ! -f $BKSOURCEDIR/goober-$GOOBERVERSION.tar.gz ]; then
		echo Getting Goober version $GOOBERVERSION .
		rm -f $BKSOURCEDIR/goober-$GOOBERVERSION.tar.gz
		bkgetfile http www.goober.org '' '' software/goober goober-$GOOBERVERSION.tar.gz
	else
		echo $BKSOURCEDIR/goober-$GOOBERVERSION.tar.gz is already here.
	fi
	;;
POSTOPENSOURCE)
####	Kernel source has been opened, patches have been applied, and
#### .config has been restored.  If you need to do any patching,
#### now's a good time.
####
#### Do whatever it takes to open up the tar if the test file's not
#### there.
	if [ ! -f "$GOOBERTESTFILE" ]; then	#Open up source
		if [ -f "$GOOBERTAR" ]; then
			mkdir --parents $GOOBERSOURCEDIR
			cd $GOOBERSOURCEDIR ; cd ..
			case $GOOBERTAR in
			*.tar.gz)	tar -xzvf $GOOBERTAR												;;
			*.tar.bz2)	cat $GOOBERTAR | bunzip2 - | tar -xvf -							;;
			*.tar)		tar -xvf $GOOBERTAR												;;
			*)			echo Don\'t know how to open $GOOBERTAR \! ; bkbeep ; sleep 30		;;
			esac
			if [ ! -f "$GOOBERTESTFILE" ]; then
				echo Still no $GOOBERTESTFILE after attempting to open \! ; bkbeep ; sleep 30
			fi
		else
			echo Goober source is not yet opened and we don\'t have $GOOBERTAR \! ; bkbeep ; sleep 30
		fi
	fi
	;;
POSTCONFIGURE)
####	The user has configured the kernel.  Good time to make any last
#### minute changes to the kernel config.
####
	:
	;;
POSTKERNELBUILD)
####	Kernel has been built.  Compile your code now?
####
	:
	;;
POSTMODULEBUILD)
####	Kernel modules have been built.  Alternate time to compile your
#### code.
####
	:
	;;
POSTINSTALL)
####	Kernel and modules put in place.  Yours too?
####
#### In this example, I wait until all the kernel building is done and
#### build our code.
	if [ -n "$GOOBERSOURCEDIR" ]; then
		cd $GOOBERSOURCEDIR >/dev/null
		if [ `pwd` != "$GOOBERSOURCEDIR" ]; then
			echo can\'t change directory, exiting
		else
			make realclean
			./configure
			make
			make install
			depmod -a
		fi
	fi
	;;
POSTCLEAN)
####	"make clean" has been run if user requested it.  Time to do any
#### last minute cleanups.
####
	:
	;;
''|ALL)
#### You shouldn't need to change the following.  The user has the
#### option of calling your module directly at some later date with
#### the sole parameter of "ALL".  This recursive call will perform
#### all of the above steps in order.
	. $THISSCRIPT POSTLOADVARS
	. $THISSCRIPT POSTGETSOURCE
	. $THISSCRIPT POSTOPENSOURCE
	. $THISSCRIPT POSTCONFIGURE
	. $THISSCRIPT POSTKERNELBUILD
	. $THISSCRIPT POSTMODULEBUILD
	. $THISSCRIPT POSTINSTALL
	. $THISSCRIPT POSTCLEAN
	;;
*)	echo unrecognized phase $1 ;;
esac

