#!/bin/sh 
#
# Update code, compile it, install it, and build rpms of installed code.
# Copyright (C) 1998, 1999 Stephen Rust <steve@tp.org>

NAME="gbuild"
AUTHOR="Stephen Rust"
COPYRIGHT="Copyright (C) 1998, 1999"
EMAIL="<steve@tp.org>"
GBVERSION="Version 0.6"
GBCOMMAND="$0"
awk="gawk"
cut="cut"
sed="sed"
cvs="cvs"
rpm="rpm"
make="gmake"
prefix="/usr"

set -e

# Variable Declarations (override these in the file specified in $rcfile)
pwd=`pwd`
rcfile=$HOME/.gbuildrc
if [ -w $prefix/share/gbuild ]
then
	ioprefix="$prefix/share/gbuild"
else
	ioprefix="$HOME/.gbuild"
fi
logprefix="$ioprefix/log"
tarprefix="$ioprefix/tar"
specprefix="$ioprefix/spec"
rpmprefix="$ioprefix/rpm"
srpmprefix="$ioprefix/srpm"
tmp="/tmp"
verbose=0
xverbose=0
compile=0
update=0
rpm=0
srpm=0
spec=0
tar=0
norc=""
checkout="checkout"
VERSION=""
RELEASE=""
GLOBAL=""
EXCLUDES=""
INCLUDES=""
REPLACE=""
PARTS=""

# Function Definitions

# Display version and exit
version ()
{
	echo "$NAME  $GBVERSION $COPYRIGHT $AUTHOR $EMAIL"
	echo "$NAME: running as $GBCOMMAND"
	exit
}

# short form help
short_usage () 
{
	echo "Usage: gbuild [-u [module-names]] [-c [module-names]]"
	echo "              [-co <checkout-cmd>]"
	echo "              [-opt [configure options]] [-d <directory-name>]"
	echo "              [-sd <i/o file directory>] [-cd <compile directory>]"
	echo "              [--tar --spec --rpm --srpm]"
	echo "              [--includes <pkg specific options>]"
	echo "              [--excludes <pkg specific options>]"
	echo "              [--replace  <pkg specific options>]"
	echo "              [--norc --init]"
	echo "              [--tmp <tmp-directory>]"
	echo "              [-h --help -v -V]"
	echo "              [-version --version]"
}

# long form help
long_usage()
{
	echo "    -h      Short command line help screen"
	echo "    --help  This long help screen with help descriptions"
	echo "    -version, --version  Print the version"
	echo "    -v, -V  Verbose operating mode and Extra Verbose mode respectively"
	echo "    -d, -sd Specify working directory or I/O files directory respectively"
	echo "    -cd     Specify directory to actually compile code in"
	echo "    -co     Specify the command to update code (usually checkout or update)"
	echo "    -opt    Specify options to autogen/configure"
	echo 
	echo "    -u      Perform CVS Update of code"
	echo "    -c      Perform Compile of code"
	echo
	echo "    --tar   Build tar file of code"
	echo "    --spec  Auto-create a spec file for RPM usage"
	echo "    --rpm   Only build binary RPM, not SRPM"
	echo "    --srpm  Build both RPM and SRPM"
	echo
	echo "    --includes Specify options to apply to specific packages"
	echo "    --excludes Specify options to remove from specific packages"
        echo "    --replace  Specify match and replace strings for specific packages"
	echo
	echo "    --norc  Bypass rc file"
	echo "    --init  Initialize the system directories"
	echo "    --tmp   Sets the tmp working directory"
	echo
	echo "See /usr/share/gbuild/README file for a more in-depth description of these options."
}

# Command help and exit
# do short help
short_help()
{
	short_usage
	echo
	echo "Use the --help option for descriptions of these options"
	exit
}

# do long help
long_help()
{
	short_usage
	echo
	long_usage
	exit
}

# initialize the $ioprefix directory structure
init_gbuild()
{
	if [ ! -e "$ioprefix" ]
	then
		mkdir $ioprefix
	fi

	if [ ! -e "$ioprefix/log" ]
	then
		mkdir $ioprefix/log
	fi

	if [ ! -e "$ioprefix/tar" ]
	then
		mkdir $ioprefix/tar
	fi

	if [ ! -e "$ioprefix/spec" ]
	then
		mkdir $ioprefix/spec
	fi

	if [ ! -e "$ioprefix/rpm" ]
	then
		mkdir $ioprefix/rpm
	fi
	
	if [ ! -e "$ioprefix/srpm" ]
	then
		mkdir $ioprefix/srpm
	fi
}

# set the PARTS variable
build_parts()
{
	if [ "$PARTS" = "" ]
	then
		if [ -e "$HOME/.gbuild/PARTS" ]
		then
			partsfile="$HOME/.gbuild/PARTS"
			
		elif [ -e "$prefix/share/gbuild/PARTS" ]
		then
			partsfile="$prefix/share/gbuild/PARTS"
	
		else
			echo "$NAME: $HOME/.gbuild/PARTS doesn't exist"
			echo "$NAME: $prefix/share/gbuild/PARTS file doesn't exist."
			echo "$NAME: Please do one of the following:"
			echo "$NAME: (1) make sure the PARTS file exists in $ioprefix/PARTS"
			echo "$NAME: (2) define PARTS in $rcfile"
			echo "$NAME: (3) or specify modules on the cmdline"
			exit 1
		fi
	
		if [ $verbose -ne 0 ]
		then
			echo "$NAME: Reading packages from $partsfile"
		fi
		PARTS=`cat $partsfile`
	fi
}

# Does the compiling for a module
build_module()
{
	echo "$NAME: $cur compiling"

	if [ -e "autogen.sh" ]
	then
		cmd="./autogen.sh"

	elif [ -e "configure" ]
	then
		cmd="./configure"

	else
		echo "No autogen.sh or configure found.  Is this a GNU package?"
		exit 1
	fi

		# for this hack:
		# Use --excludes instead on cmdline or in .gbuildrc

		# Short&ugly hack: ORBit doesn't work with --disable-static
		# but we may want to build the rest of GNOME with this
		# parameter, so we drop it when compiling ORBit.
		#if [ "x$i" = "xORBit" ] ; then
	  	#newoptlist=`echo $optlist | $sed -e 's,--disable-static,,'`
		#else
	  	#newoptlist=$optlist
		#fi

	sh $cmd $optlist 1>>$logprefix/AUTOGEN.$cur.LOG 2>>$logprefix/AUTOGEN.$cur.LOG

	$make 1>>$logprefix/AUTOGEN.$cur.LOG 2>>$logprefix/AUTOGEN.$cur.LOG

	$make install 1>>$logprefix/AUTOGEN.$cur.LOG 2>>$logprefix/AUTOGEN.$cur.LOG

	ldconfig
}

# Builds a tar file
build_tar()
{
	echo "$NAME: $cur creating tar file"
	
	cd $cur

	if [ ! -e "config.status" ]
	then
		echo "$NAME: $cur no config.status found, skipping tar"
	else
		CURVERS=`grep "\@VERSION\@" config.status | $cut -f 3 -d'%'`

		$make dist 1>>$logprefix/AUTOGEN.$cur.LOG 2>>$logprefix/AUTOGEN.$cur.LOG
	
		if [ $verbose -ne 0 ]
		then
			echo "$NAME: $cur tar file $i-$CURVERS.tar.gz built"
		fi

		mv $cur-$CURVERS.tar.gz $tarprefix/$cur-$CURVERS.tar.gz

		if [ $verbose -ne 0 ]
		then
			echo "$NAME: $cur-$CURVERS.tar.gz stored in $tarprefix"
		fi
	fi
}

# builds spec file
build_spec()
{
	echo "$NAME: $cur creating spec file"

	autospec -f $cur $cdprefix/$cur.spec

	mv $cdprefix/$cur.spec $specprefix/$cur.spec

	if [ $verbose -ne 0 ]
	then
		echo "$NAME: $cur.spec stored in $specprefix"
	fi
}

build_rpm()
{
	if [ "$rpmlist" = "" ]
	then
		if [ "$compilelist" != "" ]
		then
			rpmlist="$compilelist"

		elif [ "$updatelist" != "" ]
		then
			rpmlist="$updatelist"
		else
			build_parts
			rpmlist="$PARTS"
		fi
		rpmlist=`echo $rpmlist | sed "s/\r\n//g"`
	fi

	cd $cdprefix

	echo "$NAME: $rpmlist building rpm"
	for i in $rpmlist
	do
		cd $i

		specfile=""
		if [ -e "$ioprefix/spec/$i.spec" ]
		then
			specfile="$ioprefix/spec/$i.spec"
			
		elif [ -e "$i/$i.spec" ]
		then
			specfile="$i/$i.spec"

		else
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: $ioprefix/spec/$i.spec not found"
				echo "$NAME: $cdprefix/$i/$i.spec not found"
			fi
			echo "$NAME: no spec found for $i: Specify the --spec option to create a spec file"
		fi
		
		if [ "$specfile" != "" ]
		then
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: $i using spec file $specfile"
			fi

			CURRPM="$i"
			CURPREFIX=`grep "\@prefix\@" config.status | $cut -f 3 -d'%'`
			CURVERSION=`grep "\@VERSION\@" config.status | $cut -f 3 -d'%'`

			$make install prefix=$tmp/$i-root$CURPREFIX 1>>$logprefix/AUTOGEN.$i.LOG 2>>$logprefix/AUTOGEN.$i.LOG

			do_rpm_build
		fi

		cd ..
	done
}

# builds an rpm
# passes in $CURRPM, $specfile
do_rpm_build()
{
	CURPREFIX=`grep "\@prefix\@" config.status | $cut -f 3 -d'%'`
	CUREXECPREFIX=`grep "\@exec_prefix\@" config.status | $cut -f 3 -d'%'`
	CURVERSION=`grep "\@VERSION\@" config.status | $cut -f 3 -d'%'`
	TMPSPEC="$tmp/spec.$$.tmp"
	TMPRC="$tmp/rpmrc.$$.tmp"
	TMPMACRO="$tmp/rpmmacro.$$.tmp"

	# setup temporary macros files
	echo "	%_builddir		$cdprefix
		%_buildshell		/bin/sh
		%_dbpath		%{_var}/lib/rpm
		%_defaultdocdir		%{_usr}/doc
		%_instchangelog		5
		%_rpmdir		$rpmprefix
		%_rpmfilename		%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm
		%_signature		none
		%_sourcedir		$cdprefix
		%_specdir		$specprefix
		%_srcrpmdir		$srpmprefix
		%_tmppath		$tmp
		%_topdir		$cdprefix" > $TMPMACRO

	# setup temporary rpmrc
	echo "macrofiles: /usr/lib/rpm/macros:/usr/lib/rpm/%{_target}/macros:/etc/rpm/macros:/etc/rpm/%{_target}/macros:~/.rpmmacros:$TMPMACRO" > $TMPRC

	# setup the new spec file
	cat $specfile | $awk -v prefix=$CURPREFIX -v execprefix=$CURPREFIX -v version=$CURVERSION -v buildroot=$tmp/$CURRPM-root '
		BEGIN {
    			release = "";
    			allow = 1;
		}

		/^%/ {
    			if ($1=="%prep"||$1=="%build"||$1=="%install"||$1=="%clean") {
        			allow = 0;
    			} else if ($1 == "%define") {
        			if (prefix != "" && $2 == "prefix") {
            				printf "%%define prefix %s\n", prefix;
        			} else if (execprefix != "" && $2 == "exec_prefix") {
            				printf "%%define exec_prefix %s\n", execprefix;
        			} else if (version != "" && ($2=="ver" || $2=="version")) {
            				printf "%%define %s %s\n", $2, version;
        			} else if (release != "" && ($2=="rel" || $2=="release")) {
            				printf "%%define %s %s\n", $2, release;
        			} else {
            				if (allow) { print }
        			}
    			# the %setup directory first clears the build directory
    			# disaster if you develop in build directory
    			} else if ($1 != "%setup" && $1 != "%patch") {
        			allow = 1; print;
    			} else {
        			if (allow) { print }
    			}
		}

		/^[^%]/ {
  			if ($1 ~ /Source.*:/ || $1 ~ /Patch.*:/) {
      				#nothing
  			} else if ($1 ~ /[Bb][Uu][Ii][Ll][Dd][Rr][Oo][Oo][Tt]:/) {
      				printf "BuildRoot: %s\n", buildroot;
  			} else if (allow) { print }
		}
		($0 == "") { if (allow) { print } }
	' > $TMPSPEC

	# build the rpm
	if [ $srpm -ne 0 ]
	then
		rpm --rcfile $TMPRC -vv -ba $TMPSPEC 1>>$logprefix/AUTOGEN.$cur.LOG 2>>$logprefix/AUTOGEN.$cur.LOG
	else
		rpm --rcfile $TMPRC -vv -bb $TMPSPEC 1>>$logprefix/AUTOGEN.$cur.LOG 2>>$logprefix/AUTOGEN.$cur.LOG
	fi
}

# Builds options to pass to the recursive calls of gbuild
build_opt()
{
	GBUILD_OPT=$GLOBAL

	SAVIFS="$IFS"
	IFS=`echo -e "\n\r\t"`
		
	for i in $REPLACE
	do
		echo "i = $i"
		pkg=`echo $i | $awk -F':' '{ print $1 }' | $sed 's/^ *//g' | $sed 's/ *$//g'`
		pre=`echo $i | $awk -F':' '{ print $2 }' | $sed 's/^ *//g' | $sed 's/ *$//g'`
		pst=`echo $i | $awk -F':' '{ print $3 }' | $sed 's/^ *//g' | $sed 's/ *$//g'`

		if [ "$CURRENT" = "$pkg" ]
		then
			GBUILD_OPT=`echo $GBUILD_OPT | $sed "s,$pre,$pst,g"`
		fi
	done

	for i in $INCLUDES
	do
		pkg=`echo $i | $awk -F':' '{ print $1 }' | $sed 's/^ *//g' | $sed 's/ *$//g'`
		chg=`echo $i | $awk -F':' '{ print $2 }' | $sed 's/^ *//g' | $sed 's/ *$//g'`

		if [ "$CURRENT" = "$pkg" ]
		then
			GBUILD_OPT="$GBUILD_OPT $chg"
		fi
	done

	for i in $EXCLUDES
	do
		pkg=`echo $i | $awk -F':' '{ print $1 }' | $sed "s/^ *//g" | $sed "s/ *$//g"`
		chg=`echo $i | $awk -F':' '{ print $2 }' | $sed "s/^ *//g" | $sed "s/ *$//g"`

		if [ "$CURRENT" = "$pkg" ]
		then
			GBUILD_OPT=`echo $GBUILD_OPT | $sed "s,$chg,,g"`
		fi
	done

	if [ "$directory" != "" ]
	then
		GBUILD_OPT="$GBUILD_OPT -d $directory"
	fi
	
	if [ "$ioprefix" != "" ]
	then
		GBUILD_OPT="$GBUILD_OPT -sd $ioprefix"
	fi
	
	if [ "$cdprefix" != "" ]
	then
		GBUILD_OPT="$GBUILD_OPT -cd $cdprefix"
	fi	

	if [ $verbose -ne 0 ]
	then
		GBUILD_OPT="$GBUILD_OPT -v"
	fi
	
	if [ $xverbose -ne 0 ]
	then
		GBUILD_OPT="$GBUILD_OPT -V"
	fi

	if [ $tar -ne 0 ]
	then
		GBUILD_OPT="$GBUILD_OPT --tar"
	fi

	if [ $spec -ne 0 ]
	then
		GBUILD_OPT="$GBUILD_OPT --spec"
	fi

	if [ $rpm -ne 0 ]
	then
		GBUILD_OPT="$GBUILD_OPT --rpm"
	fi
	
	if [ $srpm -ne 0 ]
	then
		GBUILD_OPT="$GBUILD_OPT --srpm"
	fi

	IFS="$SAVIFS"
}

# Main code starts here

if [ ${#*} -lt 1 ]
then 
	echo "$NAME: No options specified.  Use -h or --help for command help."
	exit
fi

# Check to display version
for arg in $*
do
	case $arg in
		-version)  version;;
		--version) version;;
		*) ;;
	esac
done

# Check if help is requested
for arg in $*
do
	case $arg in
		-h)	short_help;;
		--help) long_help;;
		*) ;;
	esac
done

# Check for verbose
for arg in $*
do
	case $arg in
		-v) verbose=1;;
		-V) xverbose=1;;
		*)  ;;
	esac
done

# Check to use rc file or not
for arg in $*
do
	case $arg in
		--norc)
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: bypassing rules in $rcfile"
			fi
			norc="yes";;
		*) ;;
	esac
done

arguments=$*

# Load settings from rcfile
if [ -e "$rcfile" -a "$norc" = "" ]
then
	if [ $verbose -ne 0 ]
	then
		echo "$NAME: loading settings from $rcfile"
	fi
	source $rcfile
fi

# Add Global settings from rc, to arguments, if any
if [ "$GLOBAL" != "" ]
then
	arguments="$GLOBAL $arguments"
fi

# Now parse the rest of the arguments
for arg in $arguments 
do
	case $arg in

		--norc) ;;
		-v)	;;
		-V)	;;

		-u) 	
			toggle="-u"
			update=1;;
		-co)
			toggle="-co";;
		-c)	
			toggle="-c"
			compile=1;;
		-d)	
			toggle="-d";;
		-sd) 
			toggle="-sd";;
		-cd)
			toggle="-cd";;
		-opt)  
			toggle="-opt";;
		--includes)
			toggle="--includes";;
		--excludes)
			toggle="--excludes";;
		--replace)
			toggle="--replace";;
		--tar)  
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: --tar option"
			fi
			tar=1;;
		--spec) 
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: --spec (autospec) option"
			fi
			spec=1;;
		--rpm)	
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: --rpm option"
			fi
			rpm=1;;
		--srpm) 	
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: --srpm option"
			fi
			srpm=1
			rpm=1;;
		--tmp)
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: --tmp option"
			fi
			toggle="--tmp";;
		--init) 
			init_gbuild;;
		*)	
			if [ "$toggle" = "-u" ]
			then		
				if [ "$updatelist" = "" ]
				then
					updatelist="$arg"
				else
					updatelist="$updatelist $arg"
				fi
				if [ $verbose -ne 0 ]
				then
					echo "$NAME: updatelist set to $updatelist"
				fi

			elif [ "$toggle" = "-co" ]
			then
				if [ "$checkout" = "" ]
				then
					checkout="$arg"
				else
					checkout="$checkout $arg"
				fi
				if [ $verbose -ne 0 ]
				then
					echo "$NAME: checkout set to $checkout"
				fi

			elif [ "$toggle" = "-c" ]
			then
				if [ "$compilelist" = "" ]
				then
					compilelist="$arg"
				else
					compilelist="$compilelist $arg"
				fi
				if [ $verbose -ne 0 ]
				then
					echo "$NAME: using compilelist $compilelist"
				fi

			elif [ "$toggle" = "-d" ]
			then
				directory="$arg"
				if [ $verbose -ne 0 ]
				then
					if [ "$directory" != "" ]
					then
						echo "$NAME: directory re-set to $directory"
					else
						echo "$NAME: directory set to $directory"
					fi
				fi
					
			elif [ "$toggle" = "-sd" ]
			then
				ioprefix="$arg"
				if [ $verbose -ne 0 ]
				then
					if [ "$ioprefix" != "" ]
					then
						echo "$NAME: ioprefix re-set $ioprefix"
					else
						echo "$NAME; ioprefix set to $ioprefix"
					fi
				fi

			elif [ "$toggle" = "-cd" ]
			then
				cdprefix="$arg"
				if [ $verbose -ne 0 ]
				then
					if [ "$cdprefix" != "" ]
					then
						echo "$NAME: cdprefix re-set $cdprefix"
					else
						echo "$NAME: cdprefix set to $cdprefix"
					fi
				fi

			elif [ "$toggle" = "-opt" ]
			then
				if [ "$optlist" = "" ]
				then
					optlist="$arg"
				else
					optlist="$optlist $arg"
				fi
				if [ $verbose -ne 0 ]
				then
					echo "$NAME: using optlist $optlist"
				fi

			elif [ "$toggle" = "--includes" ]
			then
				if [ "$INCLUDES" = "" ]
				then
					INCLUDES="$arg"
				else
					INCLUDES="$INCLUDES $arg"
				fi
				if [ $verbose -ne 0 ]
				then
					echo "$NAME: using includelist $INCLUDES"
				fi

			elif [ "$toggle" = "--excludes" ]
			then
				if [ "$EXCLUDES" = "" ]
				then
					EXCLUDES="$arg"
				else
					EXCLUDES="$EXCLUDES $arg"
				fi
				if [ $verbose -ne 0 ]
				then
					echo "$NAME: using excludelist $INCLUDES"
				fi

			elif [ "$toggle" = "--replace" ]
			then
				if [ "$REPLACE" = "" ]
				then
					REPLACE="$arg"
				else
					REPLACE="$REPLACE $arg"
				fi
				if [ $verbose -ne 0 ]
				then
					echo "$NAME: using replacelist $REPLACE"
				fi

			elif [ "$toggle" = "--tmp" ]
			then
				if [ $verbose -ne 0 ]
				then
					echo "$NAME: setting tmp working directory to $arg"
				fi
				tmp="$arg"
			else	
				echo "$NAME: bad option $arg"
			fi;;	
	esac
done

if [ "$directory" = "" ]
then
	if [ $verbose -ne 0 ]
	then
		echo "$NAME: default directory . is working directory"
	fi
	directory="$pwd"
fi

if [ "$cdprefix" = "" ]
then
	if [ $verbose -ne 0 ]
	then
		echo "$NAME: compile directory default, using $directory"
	fi
	cdprefix="$directory"
fi

if [ ! -e "$ioprefix/log" ]
then
	if [ "$ioprefix" = "$HOME/.gbuild" ]
	then
		echo "$NAME: $HOME/.gbuild/log directory doesn't exist: run gbuild --init"
		exit 1
	else
		echo "$NAME: $ioprefix/log doesn't exist, please make sure you entered it correctly"
		exit 1
	fi
else
	if [ $verbose -ne 0 ]
	then
		echo "$NAME: using $ioprefix/log directory as logfiles directory"
	fi
fi

# cvs update the code
if [ $update -ne 0 ] 
then
	if [ "$updatelist" = "" ]
	then
		build_parts
		updatelist=$PARTS
		updatelist=`echo $updatelist | $sed "s/\r\n//g"`
	fi

	count=0
	for i in $updatelist
	do
		count=`expr $count + 1`
	done

	# Do the update if
	#  - We're given only one item to update
	#  - We were passed the --norc option
	#  - This insures we do the variable substition necessary,
	#    by calling ourselves again even for only 1 package
	if [ $count -lt 2 -a "$norc" = "yes" ]
	then
		cd $directory
		echo "$NAME: updating $updatelist"
		$cvs -z3 $checkout $updatelist
		cd $pwd
		echo "$NAME: $updatelist update complete"
	else
		echo "$NAME: updating module(s) $updatelist"
		for CURRENT in $updatelist
		do
			build_opt
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: making recursive gbuild call"
			fi
			$GBCOMMAND --norc $GBUILD_OPT -u $CURRENT
		done
		echo "$NAME: update complete"
	fi
fi

if [ $compile -ne 0 ] 
then
	if [ "$compilelist" = "" ]
	then
		build_parts
		compilelist=$PARTS
		compilelist=`echo $compilelist | $sed "s/\r\n//g"`
	fi

	count=0
	for i in $compilelist
	do
		count=`expr $count + 1`
	done

	if [ $count -lt 2 -a "$norc" = "yes" ]
	then
		cur=$compilelist
		echo "$NAME: building $cur"
		echo "Start `date`" >> $logprefix/COMPILE.LOG
		echo > $logprefix/AUTOGEN.$cur.LOG

		pwd=`pwd`
		cd $directory

		if [ ! "$cdprefix" = "$directory" ]
		then
			cp -au $cur $cdprefix
			cd $cdprefix
		fi

		cd $cur	&& touch .LOCK \
		&& rm -f config.status && rm -f config.cache \
		&& DEPS=`find . -name .deps` \
		&& if [ "$DEPS" != "" ];then rm -r $DEPS; fi \
		&& build_module \
		&& rm -f .LOCK \
		&& cd .. \
		&& echo "      installed `date` $cur" >>$logprefix/COMPILE.LOG \
		&& if [ $spec -ne 0 ]; then build_spec; fi \
		&& if [ $tar -ne 0 ]; then build_tar; fi \
		&& if [ $rpm -ne 0 -o $srpm -ne 0 ]; then build_rpm; fi

		if [ -e "$cdprefix/$cur/.LOCK" ]
		then
			rm -f $cdprefix/$cur/.LOCK
			cd $cdprefix
			echo "$NAME: $cur failed compile"
			echo "      failed    `date` $cur" >> $logprefix/COMPILE.LOG
		fi
		
		echo "$NAME: $cur done"
		cd $pwd

		echo "Finish `date`" >>$logprefix/COMPILE.LOG
	else
		echo "$NAME: compiling module(s) $compilelist"
		for CURRENT in $compilelist
		do
			build_opt
			if [ $verbose -ne 0 ]
			then
				echo "$NAME: making recursive gbuild call"
			fi
			$GBCOMMAND --norc $GBUILD_OPT -c $CURRENT
		done
		echo "$NAME: compile complete"
	fi
fi

exit
