#!/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://inconnu.islug.org/~ink/new/projects/xchat-xmms/"
PACKAGE="xchat-xmms"
SEP="-"
VERSION="0.8.1"
DESCRIPTION="xchat plugin to control xmms  (TESTING)"
AUTHORS="Craig Kelley"
HOMEPAGE="http://inconnu.islug.org/~ink/new/projects/xchat-xmms/"
LICENSE="Artistic"
ME="Kingdomcome"
DEPS="glib1.tcz gtk1.tcz xmms-1.2.11.tcz expat2.tcz cairo.tcz
      graphics-libs-1.tcz pango.tcz cairo.tcz pixman.tcz glib2.tcz
      fontconfig.tcz libxml2.tcz Xlibs_support.tcz openssl-0.9.8h.tcz
      glib2.tcz gtk2.tcz xchat-2.8.6.tcz perl5.tcz Xmms-Perl.tcz"
BUILDDEPS=""
FLAGS="--prefix=/usr/local"
DOCS=""

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

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} && wget ${MIRROR}${SRC}
   tar xzf ${SRC}
}

buildit(){
   cd ${SRCDIR}
   touch /tmp/mark
   install -Dm 755 xchat-xmms.pl \
   ${PKGDIR}/tmp/usr/local/share/xchat2/plugins/xchat-xmms.pl
}

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

   cd ${PKGDIR}/tmp
   rm -rf ${DOCS}

   mkdir -p usr/local/share/doc/License
   cp ${SRCDIR}/Artistic 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

}

packageit(){
   cd ${PKGDIR}/tmp
   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:	Depends on xchat-2.8.6, xmms-1.2.11, perl5 and deps
		
		Run "/xmms help" in xchat for usage
		
		This extension is PPI compatible
Change-log:	----
Current:	${TODAY} First Version
EOF
}

#here we go

environment
buildit
workit
packageit
