Sophie

Sophie

distrib > Mandriva > 2006.0 > x86_64 > by-pkgid > 77d4f3b28bbd05e3cd801bca9225262f > files > 1

slbd-0.25-1mdk.src.rpm

#!/bin/sh

#  slbd.sh ([Stefan's|Stupid] Little Build Daemon)
#  This shell script rebuilds distributions of src.rpm files
#
#  Copyright (c) by Stefan van der Eijk <stefan@eijk.nu>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# v0.25 2004/07/21
#	- add KEEP_BUILDDIR variable, to save the contents of the builddir
#	- add rpmlint checks
#
# v0.24 2004/07/11
#	- Misc fedora changes
#
# v0.23 2004/06/18
#	- added filter for "Internal compiler error"
#
# v0.22 2004/05/21
#	- put segfault buildoutputs in a seperate dir
#	- remove CHROOT=0 and CHROOT variable, chroots are always used
#	- introduce INSTALLER variable
#
# v0.21 2004/04/30
#	- introduces RPM_OPTIONS variable, to cope with fedora's rpmbuild
#
# v0.20 2004/04/18
#	- make paths to executables variable
#	- changed the name of the original ${RPM_BIN} variable to ${COMPARE}
#	- make path to slbd base variable ${SLBD}
#
# v0.19 2004/04/12
#	- sort out packages with dependency issues
#	- add missing dependency to buildoutput
#
# v.018 2004/01/05
#	- invoke urpmi with "--keep" option when updating a built environment
#
# v.017 2004/01/02
#	- only check for Segmentation fault on problem packages
#
# v0.16 2003/12/23
#	- fixed hardcoded /chroot/cooker --> ${CHROOT_PATH} (vdanen)
#	- use unified diff for Filelist, Provides and Requires comparison
#
# v0.15 2003/12/15
#	- put the Segfault check before the other BuildOutput checks
#
# v0.14 2003/11/26
#	- changed the direcotry structure
#
# v0.13 2003/11/22
#	- added rpm_qa directories to contain package listing at build time
#
# v0.12 2003/11/18
#	- added filter for "Segmentaion fault"
#
# v0.11 2003/11/13
#	- rebuild src.rpm to obtain correct dependency information
#
# v0.10 2003/10/31
#	- ad KEEP_RPMS
#
# v0.09 2003/10/18
#	- invoke urpmi with --no-uninstall
#
# v0.08 2003/09/07
#	- make automake1.7 hack simpler
#	- added "File not found" check
#
# v0.07 2003/09/02
#	- quite some small fixes
#	- automake1.7 hack
#
# v0.06 2003/07/06
#	- added support for:
#	  - configuration file
#	  - multiple instances on one machine (different chroots)
#	- merged min.sh
#
# v0.05 2003/05/26
#	- implemented chroot support
#	- rewrite of find_new() and find_old()
#
# v0.04 2003/05/02
#	- different urpmi locations per arch
#	- delete only ${HOSTTYPE} packages, not noarch
#
# v0.03 2003/04/27
#	- add number of packages installed to the log
#	- log when min.sh is run
#
# v0.03 2003/04/22
#	- log what packages rebuilt
#	- try to recover initial state by running min.sh
#
# v0.02 2003/04/22
#	- check if number of packages is the same before & after build
#	otherwise remove /build/run/`hostname -f` file
#	- invoke urpmi with -q option
#
# v0.01 2003/04/06
#	- first release under the GPL
#

export PATH=$PATH:/usr/X11R6/bin:/usr/sbin:/sbin

cleanup () {
# Cleanup build environment
# Input: src name
# - ${1} = src name
# Output: none
# Configuration file: ~/.slbd
# - CHROOT_PATH

. ~/.slbd ${src}

echo "Cleanup"

NHOME=${CHROOT_PATH}${HOME}
rm -rf ${CHROOT_PATH}/tmp/* ${CHROOT_PATH}/var/tmp/*
rm -rf	${HOME}/RPM/BUILD/* \
	${HOME}/RPM/RPMS/{${P_ARCH},noarch}/* \
	${HOME}/RPM/SOURCES/* \
	${HOME}/RPM/SPECS/* \
	${HOME}/tmp/*
find	${HOME}/RPM/ -type f -exec rm -rf {} \;

chmod 777 ${NHOME}/RPM/BUILD/* -R 
rm -rf	${NHOME}/RPM/BUILD/* \
	${NHOME}/RPM/RPMS/{${P_ARCH},noarch}/* \
	${NHOME}/RPM/SOURCES/* \
	${NHOME}/RPM/SPECS/* \
	${NHOME}/tmp/*
find	${NHOME}/RPM/ -type f -exec rm -rf {} \;
}

find_new () {
# Find a new src.rpm to build.
# Input: src name
# - ${1} = src name
# Output: name of src.rpm
# Configuration file: ~/.slbd
# - BUILD_OUTPUT
# - RPM_SRC

src=${1}

. ~/.slbd ${src}

find ${BUILD_OUTPUT}/BO/ -type f \
| perl -p -e "s#.*/##g" 2> /dev/null \
| sort -u \
> ~/tmp/${src}_LOG1

find ${BUILD_OUTPUT}/ -type f \
| grep -v RPMS \
| perl -p -e "s#.*/##g" 2> /dev/null \
| sort -u \
> ~/tmp/${src}_LOG2

find ${RPM_SRC}/ -type f \
| grep src.rpm$ \
| perl -p -e "s#.*/##g" 2> /dev/null \
| perl -p -e "s#^\..*\n##g" 2> /dev/null \
| perl -p -e "s#\.src\.rpm##g" 2> /dev/null \
| sort -u \
> ~/tmp/${src}_LOG3

diff ~/tmp/${src}_LOG1 ~/tmp/${src}_LOG2 \
> ~/tmp/${src}_diff

diff ~/tmp/${src}_LOG1 ~/tmp/${src}_LOG3 \
>> ~/tmp/${src}_diff

diff ~/tmp/${src}_LOG2 ~/tmp/${src}_LOG3 \
>> ~/tmp/${src}_diff

cat ~/tmp/${src}_diff \
| grep "[>|<]" \
| perl -p -e "s#\S\s+(\S+)#\1#g" 2> /dev/null \
| sort -u \
| head -n 1

rm -rf ~/tmp/${src}_LOG? ~/tmp/${src}_diff 2> /dev/null
}

find_old () {
# Find an old src.rpm to build.
# Input: src name
# - ${1} = src name
# Output: name of src.rpm
# Configuration file: ~/.slbd
# - BUILD_OUTPUT
# - MTIME

src=${1}

. ~/.slbd ${src}

find \
${BUILD_OUTPUT}/BO/ \
${BUILD_OUTPUT}/BO/dependency/ \
-maxdepth 1 \
-mtime +1 \
-type f \
| perl -p -e "s#.*/(\S+)#\1#g" 2> /dev/null \
>> ~/tmp/${src}_BUILD

find \
${BUILD_OUTPUT}/BO/cannot_be_installed/ \
${BUILD_OUTPUT}/BO/debug/ \
${BUILD_OUTPUT}/BO/file_not_found/ \
${BUILD_OUTPUT}/BO/OK/ \
${BUILD_OUTPUT}/BO/problem/ \
${BUILD_OUTPUT}/BO/unpackaged_files/ \
-mtime ${MTIME} \
-type f \
| perl -p -e "s#.*/(\S+)#\1#g" 2> /dev/null \
>> ~/tmp/${src}_BUILD

sort -u ~/tmp/${src}_BUILD | uniq | head -n 1

rm -rf ~/tmp/${src}_BUILD
}

build () {
# Count the number of installed packages in the build environment.
# Input:
# - ${1} = package name
# - ${2} = src name
# Output: number of installed packages
# Configuration file: ~/.slbd
# - BUILD_OUTPUT
# - CHROOT_PATH
# - COMPARE
# - INSTALLER
# - KEEP_BUILDDIR
# - MEDIA
# - RPM_SRC
# - STRACE

pkg=${1}
src=${2}

. ~/.slbd ${src}

find ${BUILD_OUTPUT}/ -name ${pkg} \
| grep -v RPMS \
| xargs rm -rf

# Test if ${RPM_SRC}/${pkg}.src.rpm exists, if the file doesn't exist, 
# no need to rebuild.
if [ -e ${RPM_SRC}/${pkg}.src.rpm ];
then

####
# Install BuildRequires
####

	hostname -f > ${BUILD_OUTPUT}/BO/${pkg}
	rpm -qa --root ${CHROOT_PATH} | sort > ~/tmp/begin.txt
	begin=`cat ~/tmp/begin.txt | wc -l`

	echo `date -R` ${src} ${pkg} ${begin} \
		| perl -p -e "s#\.src\.rpm##g" \
		2> /dev/null \
		>> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt

# Dependency information is stored in the headers of a rpm package, and are generated
# when the package is built. To get the correct BuildRequires for the src.rpm to be
# rebuilt, a new src.rpm needs to be built, on the correct platform and with the correct
# (build) options specified.
	echo "Building new src.rpm"
	SPEC=`rpm -qpl ${RPM_SRC}/${pkg}.src.rpm | grep "\.spec$"`
	rpm -i ${RPM_SRC}/${pkg}.src.rpm
	SRPMS=`${RPM_REBUILD_BIN} -bs --nodeps ${HOME}/RPM/SPECS/${SPEC} ${RPM_OPTIONS} | cut -d " " -f2`

	echo "Installing BuildRequires for:" ${pkg};

	if	[ "${INSTALLER}" = "apt" ]
	then
		${SUDO_BIN} ${APT_BIN} \
			build-dep \
			${SRPMS} \
			--assume-yes \
			-o RPM::RootDir=${CHROOT_PATH} \
			1>> ${BUILD_OUTPUT}/urpmi/${pkg} \
			2>> ${BUILD_OUTPUT}/urpmi_error/${pkg}

	elif	[ "${INSTALLER}" = "urpmi" ]
	then
		${SUDO_BIN} ${URPMI_BIN} -q -p \
			${SRPMS} \
			--auto \
			--media ${MEDIA} \
			--no-uninstall \
			--no-verify-rpm \
			--root ${CHROOT_PATH} \
			--split-length 0 \
			1>> ${BUILD_OUTPUT}/urpmi/${pkg} \
			2>> ${BUILD_OUTPUT}/urpmi_error/${pkg}
	elif	[ "${INSTALLER}" = "yum" ]
	then
		echo "not implemented yet"
		${SUDO_BIN} ${YUM_BIN} \
			-c ${YUM_CONFIG} \
			--installroot=${CHROOT_PATH}
	fi

#
# Optional: Find the Redundant BuildRequires
#
	if	[ "${REDUNDANT}" = 1 ] && \
		[ "${INSTALLER}" = "urpmi" ]
	then
		echo "Computing Redundant BuildRequires for:" ${pkg};
		mkdir ~/tmp/${pkg}

		for a in basesystem rpm-build `rpm -qpR ${SRPMS} \
			| cut -d " " -f1 \
			| grep -v rpmlib \
			| sort -u`
		do
			for b in `urpmq -pr ${a} --media ${MEDIA} 2> /dev/null \
				| perl -p -e "s#\|#\n#g" 2> /dev/null \
				| grep -v "no package named ${a}" \
				| cut -d ":" -f2 \
				| sort -u`
			do
				for c in `urpmq -dpr ${b} --media ${MEDIA} 2> /dev/null \
					| perl -p -e "s#\|#\n#g" 2> /dev/null \
					| sort -u`
				do
					if [ "${b}" != "${c}" ]
					then
						echo ${c}
					fi
				done
			done | sort -u > ~/tmp/${pkg}/${a}
		done

# Find the obselete BuildRequires: 

		for a in `rpm -qpR ${SRPMS} \
			| cut -d " " -f1 \
			| grep -v rpmlib \
			| sort -u`
		do
			u=0
			for b in `urpmq -pr ${a} --media ${MEDIA} 2> /dev/null \
				| perl -p -e "s#\|#\n#g" 2> /dev/null \
				| grep -v "no package named ${a}" \
				| cut -d ":" -f2 \
				| sort -u`
			do
				for c in `cat ~/tmp/${pkg}/* | sort -u`
				do
					if [ "${b}" = "${c}" ]
					then
						u=1
					fi
				done
			done
			if [ "${u}" = 1 ]
			then
				echo "@@@ Redundant BuildRequires:" ${a} "@@@" \
				`fgrep ${b} ~/tmp/${pkg}/* \
				| cut -d ":" -f1 \
				| perl -p -e "s#.*/##g" 2> /dev/null \
				| perl -p -e "s#\n# #g" 2> /dev/null`
			fi
		done \
		> ${BUILD_OUTPUT}/redundant/${pkg} 2>&1
		rm -rf ~/tmp/${pkg}

		if [ `cat	${BUILD_OUTPUT}/redundant/${pkg}	| wc -l` = 0 ]
		then
			rm -rf	${BUILD_OUTPUT}/redundant/${pkg}
		fi
	fi

####
# Flip over the .rpmmacros files
####
	NHOME=${CHROOT_PATH}${HOME}
	rm -f ${NHOME}/.rpmmacros
	opwd=`pwd`
	cd ${NHOME}
	ln -s ${RPM_MACROS} .rpmmacros
	cd ${opwd}

####
# Start building
####

	echo "Building:" ${pkg};

# Rebuild the src.rpm, write output to file.
	if [ "${STRACE}" = 0 ]
	then
		rpm -qa --root ${CHROOT_PATH} | sort > ${BUILD_OUTPUT}/rpm_qa/${pkg}

		cp ${RPM_SRC}/${pkg}.src.rpm \
		${CHROOT_PATH}/var/tmp/

		${SUDO_BIN} ${CHROOT_BIN} ${CHROOT_PATH} \
		su -l ${LOGNAME} \
		-c "${RPM_REBUILD_BIN} --rebuild \
	       	/var/tmp/${pkg}.src.rpm \
		${RPM_OPTIONS}" \
		&> ${BUILD_OUTPUT}/BO/${pkg}

		mv ${NHOME}/RPM/RPMS/${P_ARCH}/* \
		${HOME}/RPM/RPMS/${P_ARCH}/ 2> /dev/null
		mv ${NHOME}/RPM/RPMS/noarch/* \
		${HOME}/RPM/RPMS/noarch/ 2> /dev/null

		rm -rf ${CHROOT_PATH}/var/tmp/${pkg}.src.rpm

#	elif ["${STRACE}" = 1 ]
#	then
#		if[ "${P_ARCH}" = alpha ]
#		then
#			strace -etrace=execve,open,stat -f -F \
#			-q -o ~/tmp/strace.out \
#			rpm --rebuild \
#			${RPM_SRC}/${pkg}.src.rpm \
#			${RPM_OPTIONS}
#		elif [ "${P_ARCH}" = i586 ]
#		then
#			strace -etrace=execve,open,stat64 -f -F \
#			-q -o ~/tmp/strace.out \
#			rpm --rebuild \
#			${RPM_SRC}/${pkg}.src.rpm \
#			${RPM_OPTIONS}
#		fi &> ${BUILD_OUTPUT}/BO/${pkg}

# Find implicit BuildRequires in src.rpm file.
#		echo "Finding implicit BuildRequires for:" ${pkg};
#		cat ~/tmp/strace.out \
#		| grep -v "/usr/share/aclocal/" \
#		| ${SLBD}/bin/BR.pl \
#		| sort | uniq > ~/tmp/A.txt

# Find explicit BuildRequires in src.rpm file.
#		echo "Finding explicit BuildRequires for:" ${pkg};
#		for a in `cat ~/tmp/A.txt`
#		do
#			for b in `urpmq -dp ${a} --media ${MEDIA} 2> /dev/null \
#				| perl -p -e "s#\|#\n#g" 2> /dev/null \
#				| sort`
#			do
#			if [ "${a}" != "${b}" ]
#			then
#				echo ${b}
#			fi
#			done
#		done | sort | uniq > ~/tmp/B.txt

#		for c in `cat ~/tmp/A.txt`
#		do
#			u=0
#			for d in `cat ~/tmp/B.txt`
#			do
#				if [ "${c}" = "${d}" ]
#				then
#					u=1
#				fi
#			done
#			if [ "${u}" = 0 ]
#			then
#				echo ${c}
#			fi
#		done | sort | uniq > ~/tmp/C.txt

#		urpmq -dp basesystem rpm-build --media ${MEDIA} 2> /dev/null \
#			| sort -u > ~/tmp/D.txt

#		for i in `cat ~/tmp/C.txt`
#		do
#			u=0
#			for j in `cat ~/tmp/D.txt`
#			do
#				if [ "${i}" = "${j}" ]
#				then
#					u=1
#				fi
#			done
#			if [ "${u}" = 0 ]
#			then
#				echo ${i}
#			fi
#		done >> ${BUILD_OUTPUT}/BR/${pkg}

#		rpm -qpR /${RPM_SRC}/${pkg}.src.rpm \
#			| cut -d " " -f1 \
#			| sort \
#		       	| grep -v rpmlib > ~/tmp/E.txt
#		diff ~/tmp/E.txt ${BUILD_OUTPUT}/BR/${pkg} \
#		> ${BUILD_OUTPUT}/BR_diff/${pkg}

# Cleanup temp file
#		rm -rf ~/tmp/?.txt
 	fi

####
# Analyze the buildoutput, and move it to the right directory.
####

# Buildoutput end with "+ exit 0", build is successful.
	if [ "$(tail --lines 1 ${BUILD_OUTPUT}/BO/${pkg})" = "+ exit 0" ];
	then
# Find the "Wrote:" statements in the buildoutput and remove previous packages.
		for n in `fgrep Wrote: ${BUILD_OUTPUT}/BO/${pkg} | cut -b 8- `
			do

			o=`echo ${n}	| perl -p -e "s#.*/##g" 2> /dev/null \
					| perl -p -e "s#\.(alpha|amd64|ia64|ppc|sparc|sparc64|x86_64)\.rpm#\.i586\.rpm#g" 2> /dev/null`
			if [ -e ${COMPARE}/${o} ]
			then
# Provides
				rpm -qp --provides ${COMPARE}/${o} \
					| sort -u > ~/tmp/${o}-provides.old
				rpm -qp --provides ${n} \
					| perl -p -e "s#\(64bit\)##g"				2> /dev/null \
					| sort -u > ~/tmp/${o}-provides.new
				if [ `diff ~/tmp/${o}-provides.old ~/tmp/${o}-provides.new | wc -l` != 0 ]
				then
					echo "Provides" ${o} >> ${BUILD_OUTPUT}/provides/${pkg}
					diff -uN ~/tmp/${o}-provides.old ~/tmp/${o}-provides.new \
						>> ${BUILD_OUTPUT}/provides/${pkg}
				fi
				rm -rf ~/tmp/${o}-provides.*

# Requires
				rpm -qp --requires ${COMPARE}/${o} \
					| sort -u > ~/tmp/${o}-requires.old
				rpm -qp --requires ${n} \
					| perl -p -e "s#libc\.so\.6\.1#libc\.so\.6#g"		2> /dev/null \
					| perl -p -e "s#libcrypt\.so\.1\.1#libcrypt\.so\.1#g"	2> /dev/null \
					| perl -p -e "s#libdl\.so\.2\.1#libdl\.so\.2#g"		2> /dev/null \
					| perl -p -e "s#libm\.so\.6\.1#libm\.so\.6#g"		2> /dev/null \
					| perl -p -e "s#libnsl\.so\.1\.1#libnsl\.so\.1#g"	2> /dev/null \
					| perl -p -e "s#libresolv\.so\.2\.1#libresolv\.so\.2#g"	2> /dev/null \
					| perl -p -e "s#libutil\.so\.1\.1#libutil\.so\.1#g"	2> /dev/null \
					| perl -p -e "s#\(64bit\)##g"				2> /dev/null \
					| sort -u > ~/tmp/${o}-requires.new
				if [ `diff ~/tmp/${o}-requires.old ~/tmp/${o}-requires.new | wc -l` != 0 ]
				then
					echo "Requires" ${o} >> ${BUILD_OUTPUT}/requires/${pkg}
					diff -uN ~/tmp/${o}-requires.old ~/tmp/${o}-requires.new \
						>> ${BUILD_OUTPUT}/requires/${pkg}
				fi
				rm -rf ~/tmp/${o}-requires.*

# Filelist
				rpm -qpl ${COMPARE}/${o} \
					| sort -u > ~/tmp/${o}-filelist.old
				rpm -qpl ${n} \
					| sort -u > ~/tmp/${o}-filelist.new
				if [ `diff ~/tmp/${o}-filelist.old ~/tmp/${o}-filelist.new | wc -l` != 0 ]
				then
					echo "Filelist difference in" ${o} >> ${BUILD_OUTPUT}/filelist/${pkg}
					diff -uN ~/tmp/${o}-filelist.old ~/tmp/${o}-filelist.new \
						>> ${BUILD_OUTPUT}/filelist/${pkg}
				fi
				rm -rf ~/tmp/${o}-filelist.*

# Rpmlint
				rpmlint ${COMPARE}/${o} \
					| sort -u > ~/tmp/${o}-rpmlint.old
				rpmlint ${n} \
					| grep -v "no-packager-tag" \
					| grep -v "invalid-buildhost" \
					| grep -v "no-signature" \
					| sort -u > ~/tmp/${o}-rpmlint.new
				if [ `diff ~/tmp/${o}-rpmlint.old ~/tmp/${o}-rpmlint.new | wc -l` != 0 ]
				then
					echo "Rpmlint" ${o} >> ${BUILD_OUTPUT}/rpmlint/${pkg}
					diff -uN ~/tmp/${o}-rpmlint.old ~/tmp/${o}-rpmlint.new \
					>> ${BUILD_OUTPUT}/rpmlint_diff/${pkg}
				fi
				rm -rf ~/tmp/${o}-rpmlint.*
			fi
			rpmlint ${n} \
			| grep -v "no-packager-tag" \
			| grep -v "invalid-buildhost" \
			| grep -v "no-signature" \
		       	>> ${BUILD_OUTPUT}/rpmlint/${pkg}
		done
		if [ `cat	${BUILD_OUTPUT}/rpmlint/${pkg}	| wc -l` = 0 ]
		then
			rm -rf	${BUILD_OUTPUT}/rpmlint/${pkg}
		fi
		mv ${BUILD_OUTPUT}/BO/${pkg} ${BUILD_OUTPUT}/BO/OK/

# "error: cannot open" file does not exist --> remove the buildoutput.
	elif	[ "$(tail -n1 ${BUILD_OUTPUT}/BO/${pkg} \
		| cut -b 0-18)" = "error: cannot open" ];
	then
		rm -rf ${BUILD_OUTPUT}/BO/${pkg}

# "Architecture excluded" --> arch_excl
	elif	[ "$(tail -n2 ${BUILD_OUTPUT}/BO/${pkg} | head -n1 \
		| cut -b 8-19)" = Architecture ];
	then
		mv ${BUILD_OUTPUT}/BO/${pkg} \
			${BUILD_OUTPUT}/BO/arch_excl/

# "source package expected, binary found" --> broken
	elif	[ "$(tail -n2 ${BUILD_OUTPUT}/BO/${pkg} | head -n1 \
		| cut -b 0-37)" = "source package expected, binary found" ];
	then
		mv ${BUILD_OUTPUT}/BO/${pkg} \
			${BUILD_OUTPUT}/BO/broken/

# "cannot be installed" --> cannot_be_installed
	elif	[ "$(head -n2 ${BUILD_OUTPUT}/BO/${pkg} | tail -n1)" \
		= "error: ${RPM_SRC}/${pkg}.src.rpm cannot be installed" ] || \
		[ "$(head -n2 ${BUILD_OUTPUT}/BO/${pkg} | tail -n1)" \
			= "error: /var/tmp/${pkg}.src.rpm cannot be installed" ]
	then
		mv ${BUILD_OUTPUT}/BO/${pkg} \
			${BUILD_OUTPUT}/BO/cannot_be_installed/

# "Package already exists: %package debug" --> debug
	elif	[ `fgrep -c "error: Package already exists: %package debug" \
		${BUILD_OUTPUT}/BO/${pkg}` != "0" ]
	then
		mv ${BUILD_OUTPUT}/BO/${pkg} \
			${BUILD_OUTPUT}/BO/debug/

# "File not found" --> file_not_found
	elif	[ "$(tail -n1 ${BUILD_OUTPUT}/BO/${pkg} \
		| cut -b 5-18)" = "File not found" ];
	then
		mv ${BUILD_OUTPUT}/BO/${pkg} \
			${BUILD_OUTPUT}/BO/file_not_found/

		if	[ "${KEEP_BUILDDIR}" = "1" ]
		then
			DATE=`date +%Y%m%H%M%S`
			cd ${CHROOT_PATH}${HOME}/RPM/BUILD
			tar cf ~/save/${pkg}.${DATE}.tar *
			bzip2 ~/save/${pkg}.${DATE}.tar
			cd ${opwd}
		fi

# Missing Dependency
	elif	[ `grep -c "is needed by" ${BUILD_OUTPUT}/BO/${pkg}` != "0" ]
	then
		grep "due to unsatisfied" ${BUILD_OUTPUT}/urpmi/${pkg} \
			>> ${BUILD_OUTPUT}/BO/${pkg}
		mv ${BUILD_OUTPUT}/BO/${pkg} \
			${BUILD_OUTPUT}/BO/dependency/

# "Bad exit status" --> problem
	elif	[ "$(tail -n1 ${BUILD_OUTPUT}/BO/${pkg} \
		| cut -b 5-19)" = "Bad exit status" ];
	then

#	Also see if an "Internal compiler error" took place.
#	If so, make a copy of the Buildoutput
		if	[ `fgrep -c "Internal compiler error" ${BUILD_OUTPUT}/BO/${pkg}` != "0" ]
		then
			cp ${BUILD_OUTPUT}/BO/${pkg} ${HOME}/internal_compiler/${pkg}.`date +%y-%h-%d_%H:%M:%S`.txt
			echo `date -R` "Internal compiler error" ${pkg} >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
		fi

#	Also see if a "Segmentation fault" took place.
#	If so, make a copy of the Buildoutput
		if	[ `fgrep -c "Segmentation fault" ${BUILD_OUTPUT}/BO/${pkg}` != "0" ]
		then
			cp ${BUILD_OUTPUT}/BO/${pkg} ${HOME}/segfault/${pkg}.`date +%y-%h-%d_%H:%M:%S`.txt
			echo `date -R` "Segmentation fault" ${pkg} >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
		fi

		if	[ "${KEEP_BUILDDIR}" = "1" ]
		then
			DATE=`date +%Y%m%H%M%S`
			cd ${CHROOT_PATH}${HOME}/RPM/BUILD
			tar cf ~/save/${pkg}.${DATE}.tar *
			bzip2 ~/save/${pkg}.${DATE}.tar
			cd ${opwd}
		fi

		mv ${BUILD_OUTPUT}/BO/${pkg} \
			${BUILD_OUTPUT}/BO/problem/

# "Installed (but unpackaged) file(s) found:" --> unpackaged_files
	elif	[ `fgrep -c "Installed (but unpackaged) file(s) found:" \
		${BUILD_OUTPUT}/BO/${pkg}` != "0" ]
		then
		mv ${BUILD_OUTPUT}/BO/${pkg} \
			${BUILD_OUTPUT}/BO/unpackaged_files/

		if	[ "${KEEP_BUILDDIR}" = "1" ]
		then
			DATE=`date +%Y%m%H%M%S`
			cd ${CHROOT_PATH}${HOME}/RPM/BUILD
			tar cf ~/save/${pkg}.${DATE}.tar *
			bzip2 ~/save/${pkg}.${DATE}.tar
			cd ${opwd}
		fi
	fi

# Move resulting binary packages.
	if [ "${KEEP_RPMS}" = 1 ]
	then
		mv -f ~/RPM/RPMS/${P_ARCH}/*	${BUILD_OUTPUT}/RPMS/ 2> /dev/null
		mv -f ~/RPM/RPMS/noarch/*	${BUILD_OUTPUT}/RPMS/ 2> /dev/null
	fi

	echo "Removing BuildRequires for:" ${pkg};

	unset R
	for r in `diff ~/tmp/begin.txt ${BUILD_OUTPUT}/rpm_qa/${pkg} \
		| grep "^>" \
		| cut -d " " -f2`
	do
		R="${R} ${r}"
	done

	if [ `echo ${R} | wc -c` != "1" ]
	then
		echo "Removing" ${R} \
		>> ${BUILD_OUTPUT}/urpmi/${pkg}

		${SUDO_BIN} ${RPM_BIN} -e -v ${R} \
		--allmatches \
		--root ${CHROOT_PATH} \
		1>> ${BUILD_OUTPUT}/urpmi/${pkg} \
		2>> ${BUILD_OUTPUT}/urpmi_error/${pkg}
	fi

	a=0
	end=`rpm -qa --root ${CHROOT_PATH} | wc -l`
	while	[ "${end}" -gt "${begin}" ] && \
		[ "${a}" -le 3 ]
	do
		echo `date -R` "min" ${end} \
		| perl -p -e "s#\.src\.rpm##g" 2> /dev/null \
		>> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
		min ${src}
		a=$(($a+1))
		end=`rpm -qa --root ${CHROOT_PATH} | wc -l`
	done

	if [ "$begin" != "${end}" ]
	then
		echo `date -R` begin: ${begin} != end: ${end} \
		>> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
		rm -rf ${SLBD}/run/${LOGNAME}@`hostname -f`
	fi

	if [ `cat	${BUILD_OUTPUT}/urpmi/${pkg}	| wc -l` -le 1 ]
	then
		rm -rf	${BUILD_OUTPUT}/urpmi/${pkg}
	fi
	if [ `cat	${BUILD_OUTPUT}/urpmi_error/${pkg}	| wc -l` = 0 ]
	then
		rm -rf	${BUILD_OUTPUT}/urpmi_error/${pkg}
	fi
fi
cleanup ${src} > /dev/null 2>&1
}

min () {
# Cleanup build environment
# Input: src name
# - ${1} = src name
# Output: none
# Configuration file: ~/.slbd
# - CHROOT_PATH
# - PKGS

HN=`hostname -f`
src=${1}

. ~/.slbd ${src}
. ~/.slbd ${CHROOT_PATH}

for i in `echo ${PKGS} | perl -p -e "s#,# #g" 2> /dev/null`
do
	for j in `rpm -q \
	--queryformat %{NAME}-%{VERSION} ${i} \
	--root ${CHROOT_PATH} \
       	2> /dev/null \
	| grep -v "package ${i} is not installed"`
	do
		urpmq -dr ${j} \
		--media ${MEDIA} \
		--root ${CHROOT_PATH} \
		2> /dev/null \
		| perl -p -e "s#\|#\n#g" 2> /dev/null
	done
done | sort -u > ~/tmp/1.txt

for i in `rpm -qa \
	--root ${CHROOT_PATH} \
	2> /dev/null`;
do
	rpm -q ${i} \
	--queryformat %{NAME}-%{VERSION}-%{RELEASE} \
	--root ${CHROOT_PATH} \
	2> /dev/null
	echo
done | sort -u > ~/tmp/2.txt

for i in `diff ~/tmp/1.txt ~/tmp/2.txt | grep ">" | cut -d " " -f2`
do
	${SUDO_BIN} ${RPM_BIN} \
       	--rebuilddb \
       	--root ${CHROOT_PATH}

	${SUDO_BIN} ${RPM_BIN} -e automake1.7 \
	--allmatches \
	--nodeps \
	--root ${CHROOT_PATH} \
	2> /dev/null

	${SUDO_BIN} ${RPM_BIN} -e ${i} \
	--allmatches \
	--nodeps \
	--root ${CHROOT_PATH} \
	2> /dev/null
done

rm -rf ~/tmp/1.txt ~/tmp/2.txt
}

urpmi_media () {
# Check if urpmi media needs updating
# Input:
# - urpmi
# Output: none
# Configuration file ~/.slbd
# - CHROOT_PATH
# - INSTALLER
# - UPDATE
# - URPMI_PATH

urpmi=${1}

. ~/.slbd urpmi_${urpmi}

if [ -e ~/.${urpmi} ]
then
	if [ `stat -c %Y ${URPMI_PATH}` \
		!= `cat ~/.${urpmi}` ]
	then
		if	[ "${INSTALLER}" = "apt" ]
		then
			echo "not implemented yet"
			${SUDO_BIN} ${APT_BIN} \
				update
			if [ "${UPDATE}" = 1 ]
			then
				${SUDO_BIN} ${APT_BIN} \
				upgrade \
				--assume-yes \
				-o RPM::RootDir=${CHROOT_PATH}
			fi
		elif	[ "${INSTALLER}" = "urpmi" ]
		then
			${SUDO_BIN} ${URPMI_UPDATE_BIN} ${urpmi} --no-md5sum
			if	[ "${UPDATE}" = 1 ]
			then
				${SUDO_BIN} ${URPMI_BIN} -q \
				--auto-select \
				--auto \
				--keep \
				--media ${urpmi} \
				--no-uninstall \
				--no-verify-rpm \
				--root ${CHROOT_PATH} \
				--split-length 0
			fi
		elif	[ "${INSTALLER}" = "yum" ]
		then
			${YUM_BIN} \
			-c ${YUM_CONFIG} \
			--install-root=${CHROOT_PATH} \
			update
		fi
		stat -c %Y \
		${URPMI_PATH} \
		> ~/.${urpmi}
		continue
	fi
else
	echo "1" > ~/.${urpmi}
	continue
fi
}

#
# Main loop
#

DONE=0

if [ -e ~/.slbd ]
then
	. ~/.slbd
else
	break
fi

while		[ -e ${SLBD}/run/${LOGNAME}@`hostname -f` ]
do
#echo "A" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
	for i in `~/.slbd urpmi`
	do
#echo "B" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
		urpmi_media ${i}
#echo "C" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
	done
#echo "D" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
	for j in new old
	do
#echo "E" ${j} >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
		for i in `~/.slbd src`
		do
#echo "F" ${j} ${i} >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
			new_job=`find_${j} ${i}`
			if  [ "${new_job}" != "" ]
			then
#echo "G" ${new_job} ${j} ${i} >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
				build ${new_job} ${i}
				DONE=1
#echo "H" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
				continue 4
#echo "I" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
			fi
#echo "J" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
		done
#echo "K" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
	done
#echo "L" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
	break
#echo "M" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
done
#echo "N" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt

if [ "${DONE}" = "1" ]
then
	echo `date -R` "done" >> ${SLBD}/log/${LOGNAME}@`hostname -f`.txt
fi