#
# Build Debian-style cross compiler packages
#
# Copyright 2006-2008  Thiemo Seufer <ths@debian.org>
# Copyright 2006-2010  Hector Oron <hector.oron@gmail.com>
#
#  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
# Latest version of this file can be downloaded from:
#  http://emdebian.org/git/?p=debian/buildcross;a=summary

#----------------------------------------------------------------------
# Useful shell functions
## # Munge LD_LIBRARY_PATH, workaround for #453267
## DEF_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
## MY_LD_LIBRARY_PATH=/lib:/usr/lib:/lib32:/usr/lib32:/lib64:/usr/lib64
## MY_LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH:/$PKGARCH-linux-${PKGABI}/lib
## MY_LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH:/usr/$PKGARCH-linux-${PKGABI}/lib
## MY_LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH:/$PKGARCH-linux-${PKGABI}/lib32
## MY_LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH:/usr/$PKGARCH-linux-${PKGABI}/lib32
## MY_LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH:/$PKGARCH-linux-${PKGABI}/lib64
## MY_LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH:/usr/$PKGARCH-linux-${PKGABI}/lib64
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
dpink=$(tput setaf 5)
lblue=$(tput setaf 6)
white=$(tput setaf 7)
bold=$(tput bold)
reset=$(tput sgr0)
curdir=$(pwd)

debug () {
	if [ x$DEBUG = xyes ] ; then
		echo D: "$@"
	fi
}

#----------------------------------------------------------------------
# -- Logging capability
myecho () { 
        case $1 in
	  WARN)
		shift
		echo "$blue" "$@" "$reset"
		;;
	  INFO)
		shift
		echo "$green" "$@" "$reset"
		;;
	  ERROR)
		shift
		echo "$red" "$@" "$reset"
		;;
	esac
}

#----------------------------------------------------------------------
# check build-deps
checkinstalled () {
	while [ -n "$1" ]; do
		dpkg -l "$1" |grep -q "^ii" || {
			echo "E: $1 is not installed."
			if [ x${ROOTCMD} = xsudo ] ; then
				myecho INFO "I: Installing $1 and clean apt cache"
				${ROOTCMD} apt-get -y --force-yes install $1 > /dev/null 2>&1
				${ROOTCMD} apt-get -f -y --force-yes install  > /dev/null 2>&1
				${ROOTCMD} apt-get clean  > /dev/null 2>&1
			else
				echo "E: Unmet dependecies: $1"
				exit 1
			fi
		}
		shift
	done
}

#----------------------------------------------------------------------
# Update system
update-sys () {
	myecho INFO "I: Update and upgrade host system"
	${ROOTCMD} apt-get -f -y --force-yes install > /dev/null 2>&1
	${ROOTCMD} apt-get -f -y --force-yes update  > /dev/null 2>&1
	${ROOTCMD} apt-get -f -y --force-yes dist-upgrade  > /dev/null 2>&1
	${ROOTCMD} apt-get -f -y --force-yes install  > /dev/null 2>&1
}

#----------------------------------------------------------------------
# set ARCH and ABI names: set-arch-abi <debian architecture>
set-arch-abi () {
	case $DEBARCH in
		armel) PKGARCH=arm; PKGABI=gnueabi ;;
		gnuspe) PKGARCH=powerpc; PKGABI=gnuspe ;;
		*) PKGARCH=$DEBARCH; PKGABI=gnu ;;
	esac
}

#----------------------------------------------------------------------
# directory check or create it otherwise: dircheck <directory>
dircheck () {
	[ -d $1 ] || mkdir -p $1
}

#----------------------------------------------------------------------
# Change directory and create dir if it does not exists
mcd () {
	[ -d $1 ] || mkdir -p $1
	cd $1
}

#----------------------------------------------------------------------
# sanity check build environment
sanitycheck () {
	checkinstalled dpkg-cross
	checkinstalled wget
	checkinstalled dctrl-tools 
	checkinstalled sudo
	checkinstalled fakeroot
	checkinstalled dpatch
	checkinstalled time
	checkinstalled patchutils
	checkinstalled tar
	checkinstalled bzip2
	checkinstalled lsb-release
	checkinstalled autoconf
	checkinstalled autoconf2.13
	checkinstalled automake1.9
	checkinstalled libtool
	checkinstalled autogen
	checkinstalled gawk
	checkinstalled bison
	checkinstalled flex
	checkinstalled realpath
	checkinstalled texinfo
	checkinstalled devscripts
	checkinstalled reprepro
	checkinstalled liburi-perl
	checkinstalled less 
	checkinstalled coreutils
	checkinstalled automake1.4
	checkinstalled automake1.7
	checkinstalled dh-make
	checkinstalled debhelper
	#checkinstalled xapt
	
	# Following lines are gdb dependencies
	checkinstalled texinfo
	checkinstalled libreadline-dev 
	#checkinstalled gcj 
	checkinstalled gobjc 
	checkinstalled cdbs  
 	checkinstalled quilt 
	checkinstalled build-essential 
	#checkinstalled gcc-multilib 
	#checkinstalled libc6-dev-i386 
	#checkinstalled lib32gcc1 
	checkinstalled dejagnu 
	checkinstalled gperf
	checkinstalled libmpfr-dev 
	checkinstalled sharutils 
	checkinstalled pbuilder
	checkinstalled chrpath
	case ${SUITE} in
	lenny)
		#Lenny:
		checkinstalled libncurses-dev 
	;;
	squeeze)
		#Squeeze:
		checkinstalled libcloog-ppl-dev
		checkinstalled libncurses5-dev 
	;;
	esac
	myecho INFO "I: Sanity check passed"
}

#----------------------------------------------------------------------
# check identity: idcheck
idcheck () {
	if [ x`id -u` = x0 ] ; then
		echo "E: Please do not build packages as root"
		exit 1;
	else
	if [ x$WARNMSG = xyes ] ; then
		echo
		echo "******************************************************"
		myecho WARN "W: Please, make sure you have sudo rights on:         "
		myecho WARN "W:   apt-get and dpkg                                 "
		myecho WARN "W:                                                    "
		myecho WARN "W: This tool is best executed in a chroot environment."
		myecho WARN "W: To disable this message please visit config file.  "
		echo "******************************************************"
		read -n 1 -s -p "Do you want to stop now [y/N] " reply
          	if [ x"$reply" = xy ] || [ x"$reply" = xY ] ; then
              		myecho INFO "I: Aborted by user"
			exit 0
          	fi
		echo
	fi
	fi
}

#----------------------------------------------------------------------
# get list of packages in main for $DEBARCH
get-package-lists () {
	local _suite=${1:-$SUITE}
	mcd $CURDIR/cache/wget
	local _mirror="${MIRROR}/dists/${_suite}/main"

	if [ ! -f Sources.${_suite}.deb ] || [ x"$CLEANTREE" = x"yes" ]; then
		if [ ! $(wget -q -nv -nd -N ${_mirror}/source/Sources.bz2) > /dev/null 2>&1 ]; then
			bzip2 -df Sources.bz2 > /dev/null 2>&1
		elif [ ! $(wget -q -nv -nd -N ${_mirror}/source/Sources.gz) > /dev/null 2>&1 ]; then
			gzip -df Sources.gz > /dev/null 2>&1
		else
			wget -q -nv -nd -N ${_mirror}/source/Sources > /dev/null 2>&1
		fi
		cp Sources Sources.${_suite}.deb
	fi
	if [ ! -f Packages.${_suite}.deb ] || [ x"$CLEANTREE" = x"yes" ]; then
		if [ ! $(wget -q -nv -nd -N ${_mirror}/binary-${DEBARCH}/Packages.bz2) > /dev/null 2>&1 ]; then
			bzip2 -df Packages.bz2
		elif [ ! $(wget -q -nv -nd -N ${_mirror}/binary-${DEBARCH}/Packages.gz) > /dev/null 2>&1 ]; then
			gzip -df Packages.gz
		else
			wget -q -nv -nd -N ${_mirror}/binary-${DEBARCH}/Packages > /dev/null 2>&1
		fi
		cp Packages Packages.${_suite}.deb
	fi

	local _mirror="http://emdebian.org/debian/dists/${_suite}/main"
	if [ ! -f Packages.${_suite}.emdeb ] || [ x"$CLEANTREE" = x"yes" ]; then
		if [ ! $(wget -q -nv -nd -N ${_mirror}/binary-${HOSTARCH}/Packages.gz) > /dev/null 2>&1 ]; then
			gzip -df Packages.gz
		else
			wget -q -nv -nd -N ${_mirror}/binary-${HOSTARCH}/Packages > /dev/null 2>&1
		fi
		cp Packages Packages.${_suite}.emdeb
	fi

	cd $CURDIR

	SOURCELIST=$CURDIR/cache/wget/Sources.${_suite}.deb
	BINARYLIST=$CURDIR/cache/wget/Packages.${_suite}.deb
	EMDEBIANBIN=$CURDIR/cache/wget/Packages.${_suite}.emdeb
}
#----------------------------------------------------------------------
# find full version string: version <binarypackage>
# XXX: What about epochs?
version () {
		grep-aptavail -PX $1 -s Version: -n ${BINARYLIST} |sed 's/[0-9]\+://'
}

#----------------------------------------------------------------------
# source version: srcver <binarypackage>
srcver () {
	local x=$(grep-aptavail -PX $1 -s Version: -n ${SOURCELIST} |sed 's/[0-9]\+://')
	echo $(echo $x |sed 's/\(.*\)\(-[0-9]*$\)/\1/' |sed 's/1://')
}

#----------------------------------------------------------------------
# find upstream version string: upstreamversion <binarypackage>
upstreamversion () {
	echo $(version $1 |sed 's/\(.*\)\(-[0-9]*$\)/\1/')
}

#----------------------------------------------------------------------
# find debian revision string: debianrevision <binarypackage>
debianrevision () {
	echo $(version $1 |sed 's/\(.*-\)\([0-9]*$\)/\2/')
}

#----------------------------------------------------------------------
# find sourcepackage: sourcepackage <binarypackage>
sourcepackage () {
	local x=$(grep-aptavail -PX $1 -s Source: -n ${BINARYLIST})
	echo ${x}
}

#----------------------------------------------------------------------
# find filename path in the archive: filepackage <binarypackage>
filepackage () {
	local x=$(grep-aptavail -PX $1 -s Filename: -n ${BINARYLIST})
	echo ${x}
}

#----------------------------------------------------------------------
# find source directory: sourcedir <sourcepackage>
sourcedir () {
	echo $1-$(srcver $1)
}

#----------------------------------------------------------------------
# find source directory: sourcedir_v <sourcepackage>
sourcedir_v () {
        local _a=`sourcedir $(srcver $1)`
        local _b=`echo ${_a} | cut -d'-' -f1`
        echo $1-${_b}
}

#----------------------------------------------------------------------
# checks if the packages has already been built
is_new_package () {

	case $2 in
	lib)
	  TS=${DEBARCH}-cross
	  local debver=$(grep-aptavail -PX $1 -s Version: -n ${BINARYLIST})
	  local emdebver=$(grep-aptavail -PX $1-$TS -s Version: -n ${EMDEBIANBIN})
	;;
	*)
	  TS=$(dpkg-architecture -a${DEBARCH} -qDEB_HOST_GNU_TYPE 2> /dev/null)
	  # Find Debian version.. also var=7.0.1-2+b1; echo "${var%+*}" %:right #:left
	  local debver=$(grep-aptavail -PX $1 -s Version: -n ${BINARYLIST}|cut -d+ -f1)
	  local emdebver=$(grep-aptavail -PX $1-$TS -s Version: -n ${EMDEBIANBIN})
	;;
	esac

	echo " &nbsp; &nbsp; $1-$TS: <b> $emdebver </b>::<b> $debver </b> :$1 <br>" >> $LOGPATH/$DEBARCH-$LOGHTML

	# Fylesystem check
	if [ -z $emdebver ] ; then
		emdebver=none
		return 3
	elif [ x$FORCE_REBUILD = xyes ] ; then
		return 3
	elif dpkg --compare-versions $emdebver gt $debver ; then
		return 2
	elif dpkg --compare-versions $emdebver eq $debver ; then
		return 1
	else
		return 0
	fi
}

#----------------------------------------------------------------------
# At exit
at_exit () {
	if [ x${LOCALREPO} = xyes ] ; then
		add-to-repository
	fi

	rm -rf $SRCDIR/Sources
}

#----------------------------------------------------------------------
# scan source directory and generate Sources file: scan_srcdir
scan_srcdir () {
	mcd $SRCDIR
	dpkg-scansources . > Sources
}

#----------------------------------------------------------------------
# check if it is new source packages: is_new_source <sourcepackage>
is_new_source () {

	if [ -z $SRCDIR/Sources ] ; then
		scan_srcdir
	fi

	local debver=$(srcver $1)
	local emdebver=$(grep-aptavail -PX $1 -s Version: -n ${SRCDIR}/Sources)

	if [ -z $emdebver ] ; then
		return 0
	elif dpkg --compare-versions $debver gt $emdebver ; then
		return 0
	else
		# It is not new source
		return 1	
	fi
}

#----------------------------------------------------------------------
# get source packages: get-source <sourcepackage>
get-source () {
	myecho INFO "I: Getting sources for $1/$SRCSUITE"
	mcd $SRCDIR
	aptget update
	is_new_source $1
	case $? in
	0)
		rm -rf $SRCDIR/$1-* $SRCDIR/$1_*
		aptget source $1
	;;
	esac
	rm -rf $BUILDPATH/$DEBARCH/$1/
	mcd $BUILDPATH/$DEBARCH/$1
	cp -Rp $SRCDIR/$1{-,_}* $BUILDPATH/$DEBARCH/$1/
}

#----------------------------------------------------------------------
# local sources.list check: sourcecheck <directory>
sourcecheck () {
[ -d $1 ] || \
cat <<EOF > $1
deb http://cdn.debian.net/debian/ $SRCSUITE main
deb-src http://cdn.debian.net/debian/ $SRCSUITE main
EOF
}

#----------------------------------------------------------------------
# get sources.list for any suite: aptget <$@ apt-get options> 
aptget () {
	cd $CURDIR
	dircheck $APTCACHE/apt-db
	dircheck $APTCACHE/apt-db/preferences.d
	dircheck $APTCACHE/apt-db/sources.list.d
	dircheck $APTCACHE/apt-db/trusted.gpg.d
	dircheck $APTCACHE/apt-db/sources.list.d
	dircheck $APTCACHE/apt-db/archives
	dircheck $APTCACHE/apt-db/archives/partial
	dircheck $APTCACHE/apt-db/lists
	dircheck $APTCACHE/apt-db/lists/partial
	sourcecheck $APTCACHE/apt-db/sources.list
	
	cd $SRCDIR/
	apt-get -o Dir::Etc=$APTCACHE/apt-db -o Dir::Etc::SourceList=$APTCACHE/apt-db/sources.list -o Dir::Cache=$APTCACHE/apt-db -o Dir::State=$APTCACHE/apt-db -o Debug::NoLocking=true $@
}

#----------------------------------------------------------------------
# make cross all package: make-cross-package <binarypackage>[force-dummy-package]
make-cross-package () {
	local _version=$(version $1)
	local _sourcepkg=$(sourcepackage $1)
	local _hashdir=$(echo ${_sourcepkg} |sed 's/^\(lib.\|.\)\(.*\)/\1/')
	local _mirror="${MIRROR}/pool/main/${_hashdir}/${_sourcepkg}"
	local _origfile="${1}_${_version}_all.deb"
	local _origfile2=$(filepackage $1)
	local _crossfile="${1}-${DEBARCH}-cross_${_version}_all.deb"
	local _force_dummy=${2:+"-A"}

	is_new_package $1 lib
	case $? in
	0|3)
		myecho INFO "I: Installing and building cross $1"
	
		mcd ${BUILDPATH}/${DEBARCH}/cross-libs
		case $FETCH in
		apt-cross)
			apt-cross -v -k -x debconf -x debconf-2.0 \
				-a ${DEBARCH} -S ${SUITE} -m ${MIRROR} -f -i $1
			apt-cross -v -k -x debconf -x debconf-2.0 \
				-a ${DEBARCH} -S ${SUITE} -m ${MIRROR} -f -b $1
		;;
		wget)
			[ -f ${_origfile} ] || wget -nv -nd -N ${MIRROR}/${_origfile2}
			[ -f ${_crossfile} ] || dpkg-cross -A -M -X gcc-4.3-base \
				-X gcc-4.4-base -X debconf -X debconf-2.0 \
				-X gcc-4.5-base -X gcc-4.6-base -X multiarch-support \
				${_force_dummy} -a ${DEBARCH} -b ${_origfile}
			${ROOTCMD} dpkg -i ${_crossfile}
		;;
		xapt)
			${ROOTCMD} xapt -a ${DEBARCH} -M ${MIRROR} -S ${SUITE} -k $1 
			cp -Rp /var/lib/xapt/output/*.deb $REPOPATH/incoming
			${ROOTCMD} xapt -c 
		;;
		esac
	;;
	1)
		myecho INFO "I: $1 already latest version"
		${ROOTCMD} apt-get install -y $1-$DEBARCH-cross
	;;
	2)
		myecho WARN "W: $1 is newer than Debian one"
	;;
	*)
		myecho ERROR "E: Unknown option"
	;;
	esac
	cd $CURDIR
}

#----------------------------------------------------------------------
# make cross $arch package: make-cross-arch-package <binarypackage> [force-dummy-package]
make-cross-arch-package () {
	local _version=$(version $1)
	local _sourcepkg=$(sourcepackage $1)
	local _hashdir=$(echo ${_sourcepkg} |sed 's/^\(lib.\|.\)\(.*\)/\1/')
	local _mirror="${MIRROR}/pool/main/${_hashdir}/${_sourcepkg}"
	local _origfile="${1}_${_version}_${DEBARCH}.deb"
	local _origfile2=$(filepackage $1)
	local _crossfile="${1}-${DEBARCH}-cross_${_version}_all.deb"
	local _force_dummy=${2:+"-A"}

	is_new_package $1 lib
	case $? in
	0|3)
		myecho INFO "I: Installing and building cross $1"

		mcd ${BUILDPATH}/${DEBARCH}/cross-libs
		case $FETCH in
		apt-cross)
			apt-cross -v -k -x debconf -x debconf-2.0 \
				-a ${DEBARCH} -S ${SUITE} -m ${MIRROR} -f -i $1
			apt-cross -v -k -x debconf -x debconf-2.0 \
				-a ${DEBARCH} -S ${SUITE} -m ${MIRROR} -f -b $1
		;;
		wget)
			[ -f ${_origfile} ] || wget -nv -nd -N ${MIRROR}/${_origfile2}
			[ -f ${_crossfile} ] || dpkg-cross -A -M -X gcc-4.3-base \
				-X gcc-4.4-base -X debconf -X debconf-2.0 \
				-X gcc-4.5-base -X gcc-4.6-base -X multiarch-support \
				${_force_dummy} -a ${DEBARCH} -b ${_origfile}
			$ROOTCMD dpkg --force-depends -i ${_crossfile}
		;;
		xapt)
			${ROOTCMD} xapt -a ${DEBARCH} -M ${MIRROR} -S ${SUITE} -k $1 
			cp -Rp /var/lib/xapt/output/*.deb $REPOPATH/incoming
			${ROOTCMD} xapt -c 
		;;
		esac
	;;
	1)
		myecho INFO "I: $1 already latest version"
		${ROOTCMD} apt-get install -y $1-$DEBARCH-cross
	;;
	2)
		myecho WARN "W: $1 is newer than Debian one"
	;;
	*)
		myecho ERROR "E: Unknown option"
	;;
	esac
	cd $CURDIR
}

#----------------------------------------------------------------------
# applies patches 
apply-patches () {
	local _pkg=$1
	local _pkgver=$2
	local aux=`cat /usr/lib/buildcross/patches/series-${1}`

	cd ${BUILDPATH}/${DEBARCH}/${1}
	# XXX: Test whatever patch system is better
## 	if [ x${QUILTPATCH} != xyes ] ; then
		cd ${1}-*
		for patchfile in $aux; do
			myecho INFO "I: Applying $patchfile patch "
			patch -p1 -E < /usr/lib/buildcross/patches/$patchfile
		done
## 	else
## 		if [ -f patches/${_pkg}/series ]; then
## 			cd ${_pkg}/${_pkgver}
## 			rm -f patches
## 			ln -s ../../patches/${_pkg} patches
## 			quilt push -a
## 			rm patches
## 			cd ../../
## 		fi
## 	fi
}

init-repository () {
	mcd $REPOPATH
	mcd repository/conf
	if [ ! -f distributions ]; then
		cat > distributions <<-EOF
		Codename: cross-toolchain
		Suite: ${SUITE}
		AlsoAcceptFor: unstable testing testing-proposed-updates testing-security
		Version: 0.1
		Origin: Emdebian
		Description: Cross toolchains
		Architectures: i386 amd64 source
		Components: main
		UDebComponents: main
		Update: - ${SUITE}
		DebIndices: Packages Release . .gz
		UDebIndices: Packages Release . .gz
		Tracking: all includechanges keepsources
		Log: reprepro.log
		EOF
	fi
	if [ ! -f incoming ]; then
		cat > incoming <<-EOF
		Name: cross-toolchain
		IncomingDir: ../incoming
		TempDir: ../incoming-tmp
		Allow: unstable>cross-toolchain
		EOF
	fi

	cd $REPOPATH
	dircheck incoming 
	dircheck incoming-tmp

	mcd ${BUILDPATH}
}

move_incoming () {
	myecho INFO "I: Moving $1 files into $REPOPATH/incoming "
	cd ${BUILDPATH}/${DEBARCH}
	TS=$(dpkg-architecture -a${DEBARCH} -qDEB_HOST_GNU_TYPE 2> /dev/null)
	mkdir -p $REPOPATH/incoming/
	case $1 in
	cross-libs)
		mv $1/*${DEBARCH}-cross*_all.deb $REPOPATH/incoming/
	;;
	*)
		mv $1/*-$TS_*.deb $REPOPATH/incoming/
	;;
	esac
	if test $? != 0 ; then
		myecho ERROR "E: Failure to move $1 DEB packages for ${DEBARCH}"
	else
		rm -rf $1/*.{deb,dsc,gz,changes} $1/*_all.deb $1/*-$TS_*.deb
	fi

	cd ${BUILDPATH}/${DEBARCH}
}

add-to-repository () {
	cd $REPOPATH/repository
	#reprepro processincoming cross-toolchain
	reprepro includedeb cross-toolchain ../incoming/*.deb
	cd ${BUILDPATH}/${DEBARCH}
}

#----------------------------------------------------------------------
# build and install libraries needed to build the cross toolchain
build-libs () {
	#case $FETCH in
	#*)
	mcd $BUILDPATH/$DEBARCH/cross-libs
	make-cross-arch-package linux-libc-dev
	# TODO: Update whenever default compiler upgrades
	make-cross-arch-package gcc-4.6-base

	## I do not think debconf depends is good idea.
	## make-cross-package debconf yes
	## make-cross-package debconf-english yes
	make-cross-package tzdata

	make-cross-arch-package libgcc1

	if [ ${DEBARCH} != "alpha" ] && [ ${DEBARCH} != "ia64" ] ; then
		make-cross-arch-package libc6
		make-cross-arch-package libc6-dev
	fi

	case ${DEBARCH} in
	hppa)
		make-cross-arch-package libgcc4
		;;
	m68k)
		make-cross-arch-package libgcc2
		;;
	amd64)
		make-cross-arch-package lib32gcc1
		;;
	ia64)
		make-cross-arch-package lib32gcc1
                make-cross-arch-package libc6.1
                make-cross-arch-package libc6.1-dev
		make-cross-arch-package libatomic-ops-dev
		make-cross-arch-package libunwind7
		make-cross-arch-package libunwind7-dev
		;;
	alpha)
                make-cross-arch-package libc6.1
                make-cross-arch-package libc6.1-dev
		;;
	powerpc)
		make-cross-arch-package libc6-ppc64
		make-cross-arch-package libc6-dev-ppc64
		;;
	i386)
		make-cross-arch-package libc6-amd64
		make-cross-arch-package lib64gcc1
		make-cross-arch-package libc6-dev-amd64
		;;
	sparc)
		make-cross-arch-package libc6-sparc64
		make-cross-arch-package libc6-dev-sparc64
		;;
	s390)
		make-cross-arch-package libc6-s390x
		make-cross-arch-package libc6-dev-s390x
		;;
	mipsel|mips)
		make-cross-arch-package libc6-mipsn32
		make-cross-arch-package libc6-mips64
		make-cross-arch-package libc6-dev-mips64
		make-cross-arch-package libc6-dev-mipsn32
		make-cross-arch-package libn32gcc1
		make-cross-arch-package libn32z1
		make-cross-arch-package libn32z1-dev
		;;
	esac	

	if [ x$SUITE != xlenny ] ; then
		make-cross-arch-package libc-bin
		make-cross-arch-package libc-dev-bin
	fi

	make-cross-arch-package zlib1g
	make-cross-arch-package zlib1g-dev

	make-cross-arch-package libgmp3-dev 
	make-cross-arch-package libgmp10-dev 
	make-cross-arch-package libgmp3c2 
 	make-cross-arch-package libgmpxx4ldbl 
	case $SUITE in
	lenny)
 	  #make-cross-arch-package libmpfr
 	  make-cross-arch-package libmpfr-dev 
 	  make-cross-arch-package libmpfr1ldbl 
	;;
	*)
	  make-cross-arch-package libmpfr4
	  make-cross-arch-package libmpfr-dev
	;;
	esac
 	make-cross-arch-package libstdc++6
	# Since gcc-4.4
#	make-cross-arch-package libgomp1
#	make-cross-package uclibc-toolchain
#	make-cross-package libuclibc0
#	make-cross-package libuclibc-dev
#	make-cross-arch-package gdb
	#;;
	#esac

	# FIXME
	(grep -v grep $LOGPATH/$HOSTARCH-$DEBARCH-$PKG.log \
	| grep error \
	|| echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> : <b> Build succesful </b> <br>" >> $LOGHTMLPATH/$LOGHTML && \
echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> :<b><i> Build maybe failed </i></b> <br>" >> $LOGHTMLPATH/$LOGHTML)

	# Final work: Install libraries and do clean up
	if [ x${FETCH} = xwget ] ; then
		cd $BUILDPATH/$DEBARCH/cross-libs
		${ROOTCMD} dpkg -i *all.deb
	fi
#	${ROOTCMD} chown $USER.$GROUP *.deb
#	rm -rf *${DEBARCH}.deb

	move_incoming cross-libs
	cd ${BUILDPATH}/${DEBARCH}

}

#----------------------------------------------------------------------
# build and install cross binutils
build-binutils () {
	# XXX Clean up paths
	local _upver=$(upstreamversion binutils)
	local _version=$(version binutils)
	local _dir=$(sourcedir binutils)
	local _debarch=${DEBARCH}

	mcd ${BUILDPATH}/${DEBARCH}/binutils

	is_new_package binutils
	case $? in
	0|3)
		myecho INFO "I: Building Binutils"
		$ROOTCMD apt-get -y build-dep binutils > /dev/null 2>&1
	
		if [ ! -d binutils-${_version} ] || [ x"$CLEANTREE" = x"yes" ]; then
			rm -rf $_dir binutils-${_version}
			get-source binutils
			cd ${_dir}*
			echo "${DEBARCH}" > debian/target
	 		apply-patches binutils
		fi
		${ROOTCMD} /usr/lib/pbuilder/pbuilder-satisfydepends-classic
		if dpkg --compare-versions ${_version} lt "2.20.51" ; then
			TARGET=${DEBARCH} fakeroot debian/rules binary-cross
		else
			TARGET=${DEBARCH} dpkg-buildpackage -b -uc -us
		fi
		if test $? -ne 0 ; then
			myecho ERROR "E: Failure to build binutils for ${DEBARCH}"
			echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> :<b><i> Build maybe failed </i></b> <br>" >> $LOGHTMLPATH/$LOGHTML
		else
			echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> : <b> Build succesful </b> <br>" >> $LOGHTMLPATH/$LOGHTML
		fi
	
		#dpkg-genchanges > binutils_${_version}_${debarch}.changes
		cd ${BUILDPATH}/${DEBARCH}/binutils/
		set-arch-abi ${DEBARCH}
	 	${ROOTCMD} dpkg -i ${DPKGFLAGS} \
			binutils-${PKGARCH}-linux-${PKGABI}_${_version}_${HOSTARCH}.deb
		move_incoming binutils
	;;
	1)
		myecho INFO "I: $1 already latest version"
		echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> : <b> Build succesful </b> <br>" >> $LOGHTMLPATH/$LOGHTML
	  	TS=$(dpkg-architecture -a${DEBARCH} -qDEB_HOST_GNU_TYPE 2> /dev/null)
		${ROOTCMD} apt-get install -y binutils-$TS
	;;
	2)
		myecho WARN "W: $1 is newer than Debian one"
	;;
	*)
		myecho ERROR "E: Unknown option"
	;;
	esac
	#// Do clean up
	# rm -rf *.gz *.bz2 *.tgz *.dsc *.changes *.tar binutils-${_version}/ 
	if [ x"${CLEANDIRS}" = x"yes" ] ; then
		cd ${BUILDPATH}/${DEBARCH}/
		rm -rf binutils/binutils-${_upver}
	fi

	cd ${BUILDPATH}/${DEBARCH}/
}

#----------------------------------------------------------------------
# install build deps gcc versions: install-gcc-build-dep <majorversion>
install-gcc-build-deps () {
	$ROOTCMD apt-get -y build-dep gcc-$1 > /dev/null 2>&1
}

#----------------------------------------------------------------------
# build cross gcc versions: build-gcc <majorversion>
build-gcc () {
	local _upver=$(upstreamversion gcc-$1)
	local _version=$(version gcc-$1)
	local _dir=$(sourcedir gcc-$1)
	local _vers=$(sourcedir_v gcc-$1)

	mcd ${BUILDPATH}/${DEBARCH}/gcc-$1

	is_new_package gcc-$1
	case $? in
	0|3)
		myecho INFO "I: Building GCC-$1"
		if [ ! -d gcc-$1-${_version} ] || [ x"$CLEANTREE" = x"yes" ]; then
			rm -rf $_dir gcc-$1-${_version}
			get-source gcc-$1
			cd $_vers*
			echo "${DEBARCH}" > debian/target
			apply-patches gcc-$1 gcc-$1-${_version}
		 	${ROOTCMD} /usr/lib/pbuilder/pbuilder-satisfydepends-classic
			GCC_TARGET=${DEBARCH} DEB_CROSS=yes fakeroot debian/rules control
		fi
		# XXX Make LD_LIBRARY_PATH set an option
		# export LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH
#		case $DEBARCH in
#		# On lenny mips* multilibs suffer of nasty bug
#		#FIXME:
#		powerpc|hppa)
#		  case $1 in
#		  4.4)
#		 		DEB_CROSS_NO_BIARCH=yes DH_VERBOSE=1 \
#				GCC_TARGET=${DEBARCH} DEB_CROSS=yes dpkg-buildpackage -us -uc -rfakeroot
#		  ;;
#		  esac
#		;;
#		mips*|sparc|ia64)
#		  case $1 in
#		  4.3)
#		 		DEB_CROSS_NO_BIARCH=yes DH_VERBOSE=1 \
#				GCC_TARGET=${DEBARCH} DEB_CROSS=yes dpkg-buildpackage -us -uc -rfakeroot
#		  ;;
#		  *)
#		 		DH_VERBOSE=1 \
#				GCC_TARGET=${DEBARCH} DEB_CROSS=yes dpkg-buildpackage -us -uc -rfakeroot
#		  ;;
#		  esac
#		;;
#		*)
				GCC_TARGET=${DEBARCH} DEB_CROSS=yes dpkg-buildpackage -us -uc -rfakeroot
#		;;
#		esac
		if test $? -ne 0 ; then
			myecho ERROR "E: Failure to build GCC $1 for ${DEBARCH}"
			echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> :<b><i> Build maybe failed </i></b> <br>" >> $LOGHTMLPATH/$LOGHTML
		else
			echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> : <b> Build succesful </b> <br>" >> $LOGHTMLPATH/$LOGHTML
			move_incoming gcc-$1
		fi
	;;
	# export LD_LIBRARY_PATH=$DEF_LD_LIBRARY_PATH
	1)
		myecho INFO "I: $1 already latest version"
		echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> : <b> Build succesful </b> <br>" >> $LOGHTMLPATH/$LOGHTML
	  	TS=$(dpkg-architecture -a${DEBARCH} -qDEB_HOST_GNU_TYPE 2> /dev/null)
		${ROOTCMD} apt-get install -y gcc-$1-$TS
	;;
	2)
		myecho WARN "W: $1 is newer than Debian one"
	;;
	*)
		myecho ERROR "E: Unknown option"
	;;
	esac
	#// Do clean up
	# rm -rf *.gz *.bz2 *.tgz *.tar gcc-$1-${_version}/ *.changes *.dsc
	if [ x"${CLEANDIRS}" = x"yes" ] ; then
		cd ${BUILDPATH}/${DEBARCH}/
		rm -rf  gcc-$1/gcc-$1-${_upver}/
	fi

	cd ${BUILDPATH}/${DEBARCH}
}

#----------------------------------------------------------------------
# build and install cross gdb
build-gdb () {
	local _upver=$(upstreamversion gdb)
	local _version=$(version gdb)
	local _dir=$(sourcedir gdb)
	local _debarch=${DEBARCH}

	mcd ${BUILDPATH}/${DEBARCH}/gdb

	is_new_package gdb
	case $? in
	0|3)
		myecho INFO "I: Building GDB"
		if [ ! -d gdb-${_version} ] || [ x"$CLEANTREE" = x"yes" ]; then
			rm -rf $_dir gdb-${_version}
			get-source gdb
			cd gdb-${_version}
			echo "${DEBARCH}" > debian/target
			apply-patches gdb gdb-${_version}
		 	${ROOTCMD} /usr/lib/pbuilder/pbuilder-satisfydepends-classic
		fi
		# XXX Make LD_LIBRARY_PATH set an option
		# export LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH
		GDB_TARGET=${DEBARCH} dpkg-buildpackage -rfakeroot -uc -us
		# export LD_LIBRARY_PATH=$DEF_LD_LIBRARY_PATH
	
		if test $? -ne 0 ; then
			myecho ERROR "E: Failure to build GDB for ${DEBARCH}"
			echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> :<b><i> Build maybe failed </i></b> <br>" >> $LOGHTMLPATH/$LOGHTML
		else
			echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> : <b> Build succesful </b> <br>" >> $LOGHTMLPATH/$LOGHTML
			move_incoming gdb
		fi
	;;
	1)
		myecho INFO "I: $1 already latest version"
		echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> : <b> Build succesful </b> <br>" >> $LOGHTMLPATH/$LOGHTML
	  	TS=$(dpkg-architecture -a${DEBARCH} -qDEB_HOST_GNU_TYPE 2> /dev/null)
		# FIXME Fails to install
		#${ROOTCMD} apt-get install -y gdb-$TS
	;;
	2)
		myecho WARN "W: $1 is newer than Debian one"
	;;
	*)
		myecho ERROR "E: Unknown option"
	;;
	esac
	#// Do clean up
	# rm -rf *.gz *.bz2 *.dsc *.changes *.tgz *.tar gdb-${_version}/ 
	if [ x"${CLEANDIRS}" = x"yes" ] ; then
		cd ${BUILDPATH}/${DEBARCH}/
		rm -rf gdb/gdb-${_upver}/
	fi

	cd ${BUILDPATH}/${DEBARCH}
}

#----------------------------------------------------------------------
# install cross gcc versions: install-gcc <majorversion>
install-gcc () {
	# XXX: FIXME
	mcd ${BUILDPATH}/${DEBARCH}
	set-arch-abi ${DEBARCH}
	local _version=$(version gcc-$1)
	local _versionrepo=$(echo gcc-$1 | sed -e 's/-//' | sed -e 's/\.//')
	local _filelist="cpp-$1-${PKGARCH}-linux-${PKGABI}_${_version}_${HOSTARCH}.deb \
		g++-$1-${PKGARCH}-linux-${PKGABI}_${_version}_${HOSTARCH}.deb \
		gcc-$1-${PKGARCH}-linux-${PKGABI}_${_version}_${HOSTARCH}.deb \
		libstdc++6-$1-dev-${DEBARCH}-cross_${_version}_all.deb \
		libstdc++6-$1-pic-${DEBARCH}-cross_${_version}_all.deb \
		libstdc++6-${DEBARCH}-cross_${_version}_all.deb"

	mcd gcc-$1
	if [ -f libgcc1-${DEBARCH}-cross_${_version}_all.deb ]; then
		_filelist="$_filelist libgcc1-${DEBARCH}-cross_${_version}_all.deb"
	fi
	if [ -f gcc-$1-${PKGARCH}-linux-${PKGABI}-base_${_version}_${HOSTARCH}.deb ]; then
		_filelist="$_filelist gcc-$1-${PKGARCH}-linux-${PKGABI}-base_${_version}_${HOSTARCH}.deb"
	fi
	$ROOTCMD dpkg -i $_filelist

	cd ${BUILDPATH}/${DEBARCH}

## 	# $ROOTCMD dpkg -i *gcc*4.4*.deb >> dpkg.log
## 
}

#----------------------------------------------------------------------
# build cross gcc-snapshot
build-gcc-snapshot () {
	# Hack, hack.
	cd /usr/lib
	$ROOTCMD ln -sf /usr gcc-snapshot
	cd - > /dev/null 2>&1

	install-gcc-build-deps snapshot
	build-gcc snapshot
#	install-gcc snapshot
}

#----------------------------------------------------------------------
# build cross gcc from SVN HEAD: build-gcc-svn <majorversion>
# XXX Incomplete, TODO
build-gcc-svn () {
	local _upver=$(upstreamversion gcc-$1)
	local _version=$(version gcc-$1)
	local _dir=$(sourcedir gcc-$1)
	local _vers=$(sourcedir_v gcc-$1)

	mcd ${BUILDPATH}/${DEBARCH}/gcc-$1
	if [ ! -d gcc-$1-${_version} ] || [ x"$CLEANTREE" = x"yes" ]; then
		rm -rf $_dir gcc-$1-${_version}
		get-source gcc-$1
		cd $_vers
		echo "${DEBARCH}" > debian/target
		GCC_TARGET=${DEBARCH} DEB_CROSS=yes fakeroot debian/rules control
		cd ../
		# mv $_dir gcc-$1-${_version}
		mv $_vers gcc-$1-${_version}
	fi
	cd ../
	apply-patches gcc-$1 gcc-$1-${_version}
	cd gcc-$1/gcc-$1-${_version}
	# XXX Make LD_LIBRARY_PATH set an option
	# export LD_LIBRARY_PATH=$MY_LD_LIBRARY_PATH
	GCC_TARGET=${DEBARCH} DEB_CROSS=yes dpkg-buildpackage -us -uc -B -rfakeroot
	# export LD_LIBRARY_PATH=$DEF_LD_LIBRARY_PATH

	if test $? -ne 0 ; then
		myecho ERROR "E: Failure to build GCC SVN for ${DEBARCH}"
		echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> :<b><i> Build maybe failed </i></b> <br>" >> $LOGHTMLPATH/$LOGHTML
	else
		echo " &nbsp; $HOSTARCH : $DEBARCH : $SRCSUITE : $PKG : <a href=\"./logs/$DEBARCH-$LOGHTML\">version</a> : <a href=\"./logs/$HOSTARCH-$DEBARCH-$PKG.log\">logfile</a> : <b> Build succesful </b> <br>" >> $LOGHTMLPATH/$LOGHTML
	move_incoming gcc-$1
	fi
	#// Do clean up
	# rm -rf *.gz *.bz2 *.tgz *.tar gcc-$1-${_version}/ *.changes *.dsc
	if [ x"${CLEANDIRS}" = x"yes" ] ; then
		cd ${BUILDPATH}/${DEBARCH}/
		rm -rf  gcc-$1/gcc-$1-${_upver}/
	fi

	cd ${BUILDPATH}/${DEBARCH}

}

#----------------------------------------------------------------------
# Finally, do the work.
em_cross () {

# unset with_sysroot
# with_sysroot=/usr/s390-linux-gnu/

PKG=$1
DEBARCH=$2

checkinstalled build-essential dpkg-cross devscripts fakeroot sudo quilt reprepro

mcd ${BUILDPATH}/${DEBARCH}

case ${1} in
	libs) 
		myecho INFO "I: Building Cross libraries"
		build-libs > $LOGPATH/$HOSTARCH-$DEBARCH-$PKG.log 2>&1 ;;
	binutils) 
		myecho INFO "I: Building Binutils"
		build-binutils > $LOGPATH/$HOSTARCH-$DEBARCH-$PKG.log 2>&1 ;;
	gdb) 
		myecho INFO "I: Building GDB"
		build-gdb > $LOGPATH/$HOSTARCH-$DEBARCH-$PKG.log 2>&1 ;;
	snapshot) 
		myecho INFO "I: Building GCC snapshot"
		build-gcc-snapshot > $LOGPATH/$HOSTARCH-$DEBARCH-$PKG.log 2>&1;;
	svn) 
		myecho INFO "I: Building SVN HEAD"
		build-gcc-snapshot > $LOGPATH/$HOSTARCH-$DEBARCH-$PKG.log 2>&1;;
	gcc-3.[3-4]|gcc-4.[0-6])
		GCCVER=${1#gcc-}
		myecho INFO "I: Building GCC ${GCCVER}"
		checkinstalled gcc-${GCCVER}
		install-gcc-build-deps ${GCCVER}
		build-gcc ${GCCVER} > $LOGPATH/$HOSTARCH-$DEBARCH-$PKG.log 2>&1
		#install-gcc ${GCCVER}
		;;
	test) 
		myecho INFO "I: Running test in (`pwd`) "
		myecho INFO "I: buildcross should now run" ; mkdir test; cd test; 
		apply-patches binutils; cd .. ;;
esac

cd ${CURDIR}

}

#----------------------------------------------------------------------
# clean directories and installed bins
# FIXME
em_clean () {
for DEBARCH in ${ARCHLIST}; do
	[ "x${DEBARCH}" = "x" ] && continue
	${ROOTCMD} apt-get remove -y --force-yes `dpkg -l | grep ${DEBARCH} | cut -d' ' -f3`
done
${ROOTCMD} apt-get remove -y --force-yes `dpkg -l | grep gnueabi | cut -d' ' -f3`
# FIXME: Maybe it is better clean conf_vars arches?
rm -rf  powerpc arm armel mips mipsel i386 sparc s390 amd64 \
	sh4 m68k hppa alpha ia64 logs logs.html dpkg.log

}

#----------------------------------------------------------------------
# Get a tarball with the tools
#// Buggy
# XXX Use alien
# XXX Create RPM
em_tgz () {
filepath=`pwd`
files=`ls -1 *.deb`
filename=`basename $filepath`

mkdir $filename-toolchain

for i in $files; do
	dpkg -x $i  $filename-toolchain
done

cd $filename-toolchain
	tar zcf ../$filename-toolchain.tgz .
cd ../

}

#----------------------------------------------------------------------
# Generate web page
web_arch () {
cd $CURDIR
cat << 'EOF' > $LOGPATH/$DEBARCH-$LOGHTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emdebian: Buildcross results</title>  <link rev="made" href="mailto:webmaster\@emdebian.org">
  <link rel="shortcut icon" href="http://www.emdebian.org/favicon.ico">
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#0000FF" vlink="#800080" alink="#FF0000">
<table width="100%" align="center" border="0" cellpadding="3" cellspacing="0" summary="">
<tr>
  <td align="left" valign="middle">
  <a href="http://www.emdebian.org/"><img src="http://www.emdebian.org/Pics/emdebian_swirl.jpg" border="0" hspace="0" 
  vspace="0" alt="emdebian logo" width="310" height="61"></a>
  </td>
</tr>
</table>
<table bgcolor="#DF0451" border="0" cellpadding="0" cellspacing="0" width="100%" summary="">
<tr>
<td valign="top">
<img src="http://www.emdebian.org/Pics/red-upperleft.png" align="left" border="0" hspace="0" vspace="0" 
alt="" width="15" height="16">
</td>
<!--
<td rowspan="2" align="center">
<a href="http://www.emdebian.org/News/index.html"><img src="http://www.emdebian.org/Pics/news.en.png" align="middle" 
border="0" hspace="4" vspace="7" alt="news" width="48" height="18"></a>
<a href="http://www.emdebian.org/projects.html"><img src="http://www.emdebian.org/Pics/projects.en.png" align="middle" 
border="0" hspace="4" vspace="7" alt="emdebian projects" width="62" height="18"></a>
<a href="http://www.emdebian.org/support.html"><img src="http://www.emdebian.org/Pics/support.en.png" align="middle" 
border="0" hspace="4" vspace="7" alt="support" width="62" height="18"></a>
<a href="http://www.emdebian.org/develinfo.html"><img src="http://www.emdebian.org/Pics/devel.en.png" align="middle" 
border="0" hspace="4" vspace="7" alt="development" width="89" height="18"></a>
-->
<td valign="top">
<img src="http://www.emdebian.org/Pics/red-upperright.png" align="right" border="0" hspace="0" vspace="0" 
alt="" width="16" height="16">
</td>
</tr>
<tr>
<td valign="bottom">
<img src="http://www.emdebian.org/Pics/red-lowerleft.png" align="left" border="0" hspace="0" vspace="0" 
alt="" width="16" height="16">
</td>
<td valign="bottom">
<img src="http://www.emdebian.org/Pics/red-lowerright.png" align="right" border="0" hspace="0" vspace="0" 
alt="" width="15" height="16">
</td>
</tr>
</table>
<div><h1>
EOF

echo "Emdebian buildcross toolchain: ${SRCSUITE}</h1>" >> $LOGPATH/$DEBARCH-$LOGHTML

cat << 'EOF' >> $LOGPATH/$DEBARCH-$LOGHTML
<p>This page is generated automatically by buildcross infrastructure.</p>
<table border="1"><colgroup><col width="30%"><col width="10%">
<col width="30%"></colgroup>
<br>
EOF

echo " Versions found for <b>$DEBARCH: </b><br>" >> $LOGPATH/$DEBARCH-$LOGHTML

cd - > /dev/null 2>&1
}

#----------------------------------------------------------------------
# Wrapper HTML to display results
em_wrap () {

cat << 'EOF' > $LOGHTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Emdebian: Buildcross results</title>  <link rev="made" href="mailto:webmaster\@emdebian.org">
  <link rel="shortcut icon" href="http://www.emdebian.org/favicon.ico">
</head>
<body text="#000000" bgcolor="#FFFFFF" link="#0000FF" vlink="#800080" alink="#FF0000">
<table width="100%" align="center" border="0" cellpadding="3" cellspacing="0" summary="">
<tr>
  <td align="left" valign="middle">
  <a href="http://www.emdebian.org/"><img src="http://www.emdebian.org/Pics/emdebian_swirl.jpg" border="0" hspace="0" 
  vspace="0" alt="emdebian logo" width="310" height="61"></a>
  </td>
</tr>
</table>
<table bgcolor="#DF0451" border="0" cellpadding="0" cellspacing="0" width="100%" summary="">
<tr>
<td valign="top">
<img src="http://www.emdebian.org/Pics/red-upperleft.png" align="left" border="0" hspace="0" vspace="0" 
alt="" width="15" height="16">
</td>
<!--
<td rowspan="2" align="center">
<a href="http://www.emdebian.org/News/index.html"><img src="http://www.emdebian.org/Pics/news.en.png" align="middle" 
border="0" hspace="4" vspace="7" alt="news" width="48" height="18"></a>
<a href="http://www.emdebian.org/projects.html"><img src="http://www.emdebian.org/Pics/projects.en.png" align="middle" 
border="0" hspace="4" vspace="7" alt="emdebian projects" width="62" height="18"></a>
<a href="http://www.emdebian.org/support.html"><img src="http://www.emdebian.org/Pics/support.en.png" align="middle" 
border="0" hspace="4" vspace="7" alt="support" width="62" height="18"></a>
<a href="http://www.emdebian.org/develinfo.html"><img src="http://www.emdebian.org/Pics/devel.en.png" align="middle" 
border="0" hspace="4" vspace="7" alt="development" width="89" height="18"></a>
-->
<td valign="top">
<img src="http://www.emdebian.org/Pics/red-upperright.png" align="right" border="0" hspace="0" vspace="0" 
alt="" width="16" height="16">
</td>
</tr>
<tr>
<td valign="bottom">
<img src="http://www.emdebian.org/Pics/red-lowerleft.png" align="left" border="0" hspace="0" vspace="0" 
alt="" width="16" height="16">
</td>
<td valign="bottom">
<img src="http://www.emdebian.org/Pics/red-lowerright.png" align="right" border="0" hspace="0" vspace="0" 
alt="" width="15" height="16">
</td>
</tr>
</table>
<div><h1>
EOF

echo "Emdebian buildcross toolchain: ${SRCSUITE}</h1>" >> $LOGHTML

cat << 'EOF' >> $LOGHTML
<p>This page is generated automatically by buildcross infrastructure.</p>
<table border="1"><colgroup><col width="30%"><col width="10%">
<col width="30%"></colgroup>
<br>
<p>You can check <a href="./logs/build.log">buildcross complete log</a></p>
<b>BUILD-HOST : HOSTARCH : SUITE : PKG : LOGFILE : STATUS</b> <br>
EOF

# -- Main
sanitycheck
## #idcheck
scan_srcdir

# dpkg-cross version check
#if [[ "2.4.0" > $(dpkg-cross --help 2>&1 |awk '{print $3; exit;}') ]]; then
# armhf needs 2.6.0
if dpkg --compare-versions $(dpkg-query -W -f='${Version}\n' dpkg-cross) lt "2.4.0" ; then
	myecho WARN "W: dpkg-cross is too old."
	${ROOTCMD} apt-get install -f dpkg-cross > /dev/null 2>&1
fi
 

mcd ${REPOPATH}
mkdir -p $REPOPATH/incoming/
if [ x${LOCALREPO} = xyes ] ; then
	init-repository
fi

mcd ${BUILDPATH}
for DEBARCH in $ARCHLIST; do
	web_arch
	if [ x$SUITE = xunstable ] || [ x$SUITE = xsid ] ; then
		case ${DEBARCH} in
		sh4|armhf|powerpcspe|sparc64|m68k)
			unset MIRROR
			MIRROR=${MIRROR:="http://ftp.debian-ports.org/debian/"}
		;;
		esac
	fi
	case $FETCH in
	apt-cross)
		myecho INFO "I: Updating $DEBARCH apt-cross cache"
		apt-cross -v -f -a ${DEBARCH} -S ${SUITE} -m ${MIRROR} -u > /dev/null 2>&1
	;;
	wget)
		myecho INFO "I: Updating $DEBARCH cross cache via wget"
		get-package-lists
	;;
	xapt)
		myecho INFO "I: Updating $DEBARCH xapt cache"
		# Update and install happens at once on xapt
	;;
	*)
		myecho ERROR "E: Need a fetch method: wget, xapt, apt-cross (deprecated)"
	;;
	esac
	myecho INFO "I: Building for ${DEBARCH} (`pwd`)" 
	for PKG in $PKGLIST; do
		em_cross $PKG $DEBARCH
	done
	cat << 'EOF' >> $LOGPATH/$DEBARCH-$LOGHTML
	</table>
	</body></html>
EOF
done

at_exit

cat << 'EOF' >> $LOGHTML
</table>
</body></html>
EOF

}

