#!/bin/sh

[ `id -u` = 0 ] || { echo "must be root"; exit 1; }

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fno-exceptions -fno-rtti"
export CPPFLAGS="-I/usr/local/include"
export LDFLAGS="-L/usr/local/lib"
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig

TODAY=`date +%Y/%m/%d`
MIRROR="http://launchpad.net/bzr/2.1/2.1.0/+download/"
PACKAGE="bzr"
SEP="-"
VERSION="2.1.0"
DESCRIPTION="Bazaar VCS     (TESTING)"
AUTHORS="The Bazaar Team"
HOMEPAGE="http://bazaar.canonical.com/"
LICENSE="GPLv2"
ME="Kingdomcome"
DEPS="sqlite3.tcz openssl-0.9.8m.tcz tcl_tk.tcz bzip2-lib.tcz python.tcz"
BUILDDEPS="python-dev.tcz"
FLAGS="--prefix=/usr/local"
DEVS=""
DOCS=""
LOCALES=""

TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tar.gz"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}${SEP}${VERSION}"

MAINDIR="${PKGDIR}/tmp-main"
DEVDIR="${PKGDIR}/tmp-dev"
DOCDIR="${PKGDIR}/tmp-doc"
LOCALEDIR="${PKGDIR}/tmp-locale"

environment(){
   for each in compiletc.tcz squashfs-tools-4.0.tcz ${DEPS} ${BUILDDEPS}; do
      sudo -u ${TCUSER} tce-load -w -i ${each}
   done
   [ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
   mkdir -p ${MAINDIR} ${DEVDIR} ${DOCDIR} ${LOCALEDIR}
   cd ${PKGDIR} && wget ${MIRROR}${SRC}
   tar xf ${SRC}
}

buildit(){
   cd ${SRCDIR}
   touch /tmp/mark
   python setup.py install
}

workit(){
   if [ "`ls -A ${MAINDIR}`" ]; then
      rm /tmp/mark
   else
      cd /
      find usr/ -newer /tmp/mark -not -type d > /tmp/${PACKAGE}.list
      tar cO -T /tmp/${PACKAGE}.list | tar xf - -C ${MAINDIR}
      rm /tmp/${PACKAGE}.list
   fi

   cd ${MAINDIR}

   [ "${DEVS}" ] && tar cO ${DEVS} | tar xf - -C ${DEVDIR}
   [ "${DOCS}" ] && tar cO ${DOCS} | tar xf - -C ${DOCDIR}
   [ "${LOCALES}" ] && tar cO ${LOCALES} | tar xf - -C ${LOCALEDIR}
   rm -rf ${DEVS}
   rm -rf ${DOCS}
   rm -rf ${LOCALES}

   mkdir -p usr/local/share/doc/License
   cp ${SRCDIR}/COPYING.txt usr/local/share/doc/License/${PACKAGE}.txt

   find usr/ | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
   find usr/ | xargs file | grep "shared object" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip -g 2> /dev/null

#   mkdir -pm 775 usr/local/tce.installed
#cat <<EOF> usr/local/tce.install/${PACKAGE}
##!/bin/sh

#EOF
#   chmod 775 usr/local/tce.installed/${PACKAGE}

#   mkdir -pm 775 usr/local/tce.menu
#cat <<EOF> usr/local/tce.menu/${PACKAGE}
#<JWM>
#  <Program label=" "></Program>
#</JWM>
#EOF
#   chmod 644 usr/local/tce.menu/${PACKAGE}

#   mkdir -pm 775 usr/local/tce.icons
#cat <<EOF> usr/local/tce.icons/${PACKAGE}
#i:
#t:
#c:
#EOF
#   chmod 644 usr/local/tce.icons/*

#dont clobber tc setup
#   chown -R root:staff usr/local/tce.*

#package specific stuff

}

packagemain(){
   cd ${MAINDIR}
   for dir in `ls -A`; do
      find ${dir} -not -type d | sort >> ../${PACKAGE}.tcz.list
   done

   mksquashfs . ../${PACKAGE}.tcz

   cd ${PKGDIR}
   md5sum ${PACKAGE}.tcz > ${PACKAGE}.tcz.md5.txt

   [ -f "${PACKAGE}.tcz.dep" ] && rm -f ${PACKAGE}.tcz.dep
   for each in ${DEPS}; do echo ${each} >> ${PACKAGE}.tcz.dep; done

   size=`du -h ${PACKAGE}.tcz | cut -f 1`

cat <<EOF> ${PACKAGE}.tcz.info
Title:		${PACKAGE}.tcz
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	A version control system. Part of the GNU Project and
		used on launchpad and elsewhere
		
		Depends on python.tcz
		
		This extension is PPI compatible
Change-log:	----
Current:	${TODAY} First Version
EOF
}

packagedev(){
   cd ${DEVDIR}
   for dir in `ls -A`; do
      find ${dir} -not -type d | sort >> ../${PACKAGE}-dev.tcz.list
   done

   mksquashfs . ../${PACKAGE}-dev.tcz

   cd ${PKGDIR}
   md5sum ${PACKAGE}-dev.tcz > ${PACKAGE}-dev.tcz.md5.txt

   size=`du -h ${PACKAGE}-dev.tcz | cut -f 1`

cat <<EOF> ${PACKAGE}-dev.tcz.info
Title:		${PACKAGE}-dev.tcz
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	
		
		
		This extension is PPI compatible
Change-log:	
Current:	${TODAY} First Version
EOF
}

packagedoc(){
   cd ${DOCDIR}
   for dir in `ls -A`; do
      find ${dir} -not -type d | sort >> ../${PACKAGE}-doc.tcz.list
   done

   mksquashfs . ../${PACKAGE}-doc.tcz

   cd ${PKGDIR}
   md5sum ${PACKAGE}-doc.tcz > ${PACKAGE}-doc.tcz.md5.txt

   size=`du -h ${PACKAGE}-doc.tcz | cut -f 1`

cat <<EOF> ${PACKAGE}-doc.tcz.info
Title:		${PACKAGE}-doc.tcz
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	
		
		
		This extension is PPI compatible
Change-log:	
Current:	${TODAY} First Version
EOF
}


packagelocale(){
   cd ${LOCALEDIR}
   for dir in `ls -A`; do
      find ${dir} -not -type d | sort >> ../${PACKAGE}-locale.tcz.list
   done

   mksquashfs . ../${PACKAGE}-locale.tcz

   cd ${PKGDIR}
   md5sum ${PACKAGE}-locale.tcz > ${PACKAGE}-locale.tcz.md5.txt

   size=`du -h ${PACKAGE}-locale.tcz | cut -f 1`

cat <<EOF> ${PACKAGE}-locale.tcz.info
Title:		${PACKAGE}-locale.tcz
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	
		
		
		This extension is PPI compatible
Change-log:	
Current:	${TODAY} First Version
EOF
}

#here we go

environment &&
buildit &&
workit &&
packagemain
[ "${DEVS}" ] && packagedev
[ "${DOCS}" ] && packagedoc
[ "${LOCALES}" ] && packagelocale
