#!/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://xchat.org/files/source/2.8/"
PACKAGE="xchat2"
SEP="-"
VERSION="2.8.6"
DESCRIPTION="IRC Client     (TESTING)"
AUTHORS="Peter Zelezny"
HOMEPAGE="http://xchat.org"
LICENSE="GPLv2"
ME="JasonW/Kingdomcome"
DEPS="gtk2.tcz openssl-0.9.8.tcz"
BUILDDEPS="gtk2-dev.tcz gettext.tcz shared-mime-info.tcz openssl-0.9.8-dev"
FLAGS="--prefix=/usr/local"
DEVS=""
DOCS=""
LOCALES="usr/local/share/locale"

TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE%2}${SEP}${VERSION}.tar.bz2"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE%2}${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 xjf ${SRC}
}

buildit(){
   cd ${SRCDIR}
   sed -i 's/GtkType/GType/' src/fe-gtk/xtext.c
   sed -i 's/GtkType/GType/' src/fe-gtk/xtext.h
   ./configure ${FLAGS}
   make
   touch /tmp/mark
   make DESTDIR=${MAINDIR} 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 usr/local/share/doc/License/${PACKAGE}.txt

   find . | xargs file | grep "executable" | grep ELF | grep "not stripped" | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
   find . | 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="XChat IRC">/usr/local/bin/xchat</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: /usr/local/share/pixmaps/xchat.png
t: XChat
c: /usr/local/bin/xchat
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:	This extension requires openssl-0.9.8 gtk2 and deps
		Compiled with Perl plugin support enabled
		
		This extension is PPI compatible
Change-log:	2008/08/06 First Version
		2008/12/11 Rebuilt for i486, size, and packed with
		           cramfs
		2010/01/04 Rebuilt against Perl 5.10.1
		2010/03/10 Rebuilt against openssl-0.9.8m
Current:	${TODAY} Rebuilt against openssl-0.9.8(n)
EOF
}
# 2008/08/06 First Version
# 2008/12/11 Rebuilt for i486, size, cramfs
# 2010/01/04 Rebuilt against Perl 5.10.1
# 2010/03/09 Rebuilt against openssl-0.9.8m
# 2010/04/04 Rebuilt against openssl-0.9.8(n)

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:	Dev files for xchat2
		
		This extension is PPI compatible
Change-log:	2008/08/06 First Version
		2008/12/11 Rebuilt for i486, size, and packed with
		           cramfs
		2010/01/04 Rebuilt against Perl 5.10.1
Current:	${TODAY} Rebuilt against openssl-0.9.8m
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:	Doc files for xchat2
		
		This extension is PPI compatible
Change-log:	2008/08/06 First Version
		2008/12/11 Rebuilt for i486, size, and packed with
		           cramfs
		2010/01/04 Rebuilt against Perl 5.10.1
Current:	${TODAY} Rebuilt against openssl-0.9.8m
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}
   echo "${PACKAGE}.tcz" > ${PACKAGE}-locale.tcz.dep

   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:	Locale files for xchat2
		
		This extension is PPI compatible
Change-log:	2008/08/06 First Version
		2008/12/11 Rebuilt for i486, size, and packed with
		           cramfs
		2010/01/04 Rebuilt against Perl 5.10.1
		2010/03/10 Rebuilt against openssl-0.9.8m
Current:	${TODAY} Rebuilt against openssl-0.9.8(n)
EOF
}



#here we go

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