#!/bin/sh

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

TODAY=`date +%Y/%m/%d`
MIRROR="http://files.andreineculau.com/projects/pidgin/original-smileys/"
PACKAGE="pidgin-smileys"
SEP="-"
VERSION="1.9"
DESCRIPTION="Original Smileys Theme for Pidgin"
AUTHORS="AndreiNeculau"
HOMEPAGE="http://files.andreineculau.com/projects/pidgin/original-smileys/"
LICENSE="gplv2"
ME="jls_legalize (unsenepopiu at tin dot it)"
DEPS="pidgin.tcz"
BUILDDEPS="squashfs-tools-4.0.tcz"

TCUSER=`cat /etc/sysconfig/tcuser`
TCEDIR="$(cat /opt/.tce_dir)"
SRC="pidgin-original-latest.tgz"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}${SEP}${VERSION}"

clean(){
	rm -rf ${THISDIR}/${PACKAGE}
}

environment(){
	for each in ${DEPS} ${BUILDDEPS}; do
		if [ -e ${TCEDIR}/optional/${each} ]; then
			sudo -u ${TCUSER} tce-load -i ${TCEDIR}/optional/${each}
		else
			sudo -u ${TCUSER} tce-load -w -i ${each}
		fi
	done
   [ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
   mkdir -p ${PKGDIR}/tmp
   cd ${PKGDIR}
   echo ${MIRROR}/${SRC}
   wget ${MIRROR}${SRC}
   tar xzf ${SRC}
}

workit(){
	cd ${PKGDIR}/tmp
	mkdir -p usr/local/share/pixmaps/pidgin/emotes
	rm ../pidgin-original-latest.tgz
	cp -a ../pidgin* usr/local/share/pixmaps/pidgin/emotes
}

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:
		This extension is PPI compatible.
Change-log:	2009/09/08 Initial packaging
			2009/12/04 updated to version 1.8
Current:	${TODAY} updated to version 1.9
EOF
}

#here we go
clean
environment
workit
packageit
