#!/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="git://supybot.git.sourceforge.net/gitroot/supybot/"
PACKAGE="supybot"
SEP="-"
VERSION="git ${TODAY}"
EXT="tcz"
DESCRIPTION="IRC bot      (TESTING)"
AUTHORS="ddipaolo, jamessan, jemfinch"
HOMEPAGE="http://supybot.com"
LICENSE="BSD"
ME="Kingdomcome"
DEPS="sqlite3.tczl openssl-0.9.8h.tczl tcl_tk.tczl python.tczl
      pysqlite.tcz"
BUILDDEPS="git.tcz python-dev.tcz"

TCUSER=`cat /etc/sysconfig/tcuser`
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}"

environment(){
   for each in squashfs-tools-4.0.tcz ${DEPS} ${BUILDDEPS}; do
      sudo -u ${TCUSER} tce-load -w -i ${each}
   done
   [ -d ${PKGDIR} ] && rm -rf ${PKGDIR}
   mkdir -p ${PKGDIR}/tmp
   cd ${PKGDIR} && git clone ${MIRROR}${PACKAGE}
   rm -rf .git*
   tar czf supybot-git.tar.gz supybot
}

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

workit(){
   cd ${PKGDIR}
   git clone git://github.com/jamessan/Supybot-Weather.git
   rm -r Supybot-Weather/README.txt Supybot-Weather/.git*
   tar czf Supybot-Weather.tar.gz Supybot-Weather
   mv Supybot-Weather /usr/local/lib/python2.6/site-packages/supybot/plugins/Weather

   cd /
   find usr/ -newer /tmp/mark -not -type d | sort > ${PKGDIR}/${PACKAGE}.${EXT}.list
   tar czf /tmp/${PACKAGE}.tgz -T ${PKGDIR}/${PACKAGE}.${EXT}.list
   cd ${PKGDIR}/tmp
   tar xzf /tmp/${PACKAGE}.tgz

   mkdir -p usr/local/share/doc/License
   cp ${SRCDIR}/LICENSE 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 -p usr/local/tce.installed
#cat <<EOF> usr/local/tce.install/${PACKAGE}
#!/bin/sh

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

#   mkdir -p 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 -p 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.*
#   chmod 775 usr/local/tce.*

#package specific stuff

}

packageit(){
   cd ${PKGDIR}/tmp
   mksquashfs . ../${PACKAGE}.${EXT}
   cd ${PKGDIR}
   md5sum ${PACKAGE}.${EXT} > ${PACKAGE}.${EXT}.md5.txt
   [ -f "${PACKAGE}.${EXT}.dep" ] && rm -f ${PACKAGE}.${EXT}.dep
   for each in ${DEPS}; do echo ${each} >> ${PACKAGE}.${EXT}.dep; done
size=`du -h ${PACKAGE}.${EXT} | cut -f 1`
cat <<EOF> ${PACKAGE}.${EXT}.info
Title:		${PACKAGE}.${EXT}
Description:	${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	Depends on python.tczl
		Optionally depends on:
		pysqlite.tcz for multiple plugins that use DBs
		procps.tczl for Status plugin
		
		Run supybot-wizard first to create conf file and
		necessary sub-directories. Then run supybot <conf file>
		to start the bot.
		
		This extension is PPI compatible
Change-log:	2009/09/15 First Version
Current:	${TODAY} Bumped to latest
			   Added missing binaries
EOF
}
# 2009/09/15 First Version
# 2009/11/29 added missing file, bumped


#here we go

environment
buildit
workit
packageit
