#!/bin/bash

[ `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"
export CPPFLAGS="-I/usr/local/include"
export LDFLAGS="-Wl,-O1"
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.8"
DESCRIPTION="IRC Client     (TESTING)"
AUTHORS="Peter Zelezny"
HOMEPAGE="http://xchat.org"
LICENSE="GPLv2"
ME="JasonW/Kingdomcome"
DEPS="gtk2.tcz"
###  DO NOT HAVE OPENSSL
BUILDDEPS="python.tcz tcl-dev.tcz python-dev.tcz perl5.tcz gtk2-dev.tcz gettext.tcz shared-mime-info.tcz openssl-1.0.0-static.tcz"
FLAGS="--prefix=/usr/local --enable-openssl=/usr/local/openssl-1.0.0"
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}/pkg"
DEVDIR="${PKGDIR}/dev"
DOCDIR="${PKGDIR}/doc"
LOCALEDIR="${PKGDIR}/loc"

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}
   ./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}

 

   mkdir -p usr/local/share/doc/License
   cp ${SRCDIR}/COPYING usr/local/share/doc/License/${PACKAGE}.txt
   mkdir -p usr/local/share/applications
   cp ${SRCDIR}/xchat.desktop usr/local/share/applications/
   mkdir -p usr/local/share/pixmaps
   cp ${SRCDIR}/xchat.png usr/local/share/pixmaps/xchat2.png
  
   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}

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

#package specific stuff
   echo "X-FullPathIcon=/usr/local/share/pixmaps/xchat.png" >> usr/local/share/applications/xchat.desktop
   mv usr/local/share/applications/xchat.desktop usr/local/share/applications/xchat2.desktop

}

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
		2010/04/04 Rebuilt against openssl-0.9.8(n)
		2010/06/22 Bumped to 2.8.8
		           Rebuilt against openssl-0.9.8(o)
Current:	"$TODAY"   Rebuilt against static openssl-1.0.0d	

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)
# 2010/06/22 Bumped to 2.8.8, Rebuilt against openssl-0.9.8(o)

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
		2010/06/22 Rebuilt against openssl-0.9.8m
Current:	"$TODAY"   Rebuilt against static openssl-1.0.0d

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
		2010/04/04 Rebuilt against openssl-0.9.8(n)
Current:	${TODAY} Bumped to 2.8.8
		           Rebuilt against openssl-0.9.8(o)
EOF
}



#here we go

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