#!/bin/sh

. /etc/init.d/tc-functions

checkroot

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`
PACKAGE="snack-alsa"
SEP="-"
VERSION="2.2.10"
MIRROR="http://ftp.it.debian.org/debian/pool/main/s/snack/"
DESCRIPTION="The Snack Sound Toolkit for alsa"
AUTHORS="Kåre Sjölander"
HOMEPAGE="http://www.speech.kth.se/snack/"
LICENSE="GPL"
ME="jls_legalize (unsenepopiu at tin dot it)"
DEPS="alsa.tcz"
BUILDDEPS=""
DOCS=""
TCUSER=`cat /etc/sysconfig/tcuser`
SRC="libsnack2-alsa_2.2.10-dfsg1-9_i386.deb"
#SRC="libsnack2-alsa_2.2.10-dfsg1-9_i386.deb"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/snack${VERSION}/unix"
TCEDIR="$(cat /opt/.tce_dir)"

clean(){
	rm -rf ${THISDIR}/${PACKAGE}
}
environment(){
	for each in squashfs-tools-4.x.tcz ${DEPS} ${BUILDDEPS}; do
		APPNAME=$(getbasefile "$each" 1)
		if [ ! -f /usr/local/tce.installed/"${APPNAME}" ]; then
			if [ ! -e ${TCEDIR}/optional/${each} ]; then
				sudo -u ${TCUSER} tce-load -w ${each}
			fi
			sudo -u ${TCUSER} tce-load -i ${TCEDIR}/optional/${each}
		fi
	done
	[ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
	mkdir ${PKGDIR}
	cd ${PKGDIR} && wget ${MIRROR}${SRC}
	ar p ${SRC}  data.tar.gz > data.tar.gz 
	tar xzvf data.tar.gz
}

workit(){
	mkdir -p ${PKGDIR}/tmp/usr/local/lib/tcl8.5
	cp -a ${PKGDIR}/usr/lib/tcltk/snack2.2 ${PKGDIR}/tmp/usr/local/lib/tcl8.5/
}

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

   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:	----------
Current:	${TODAY} First Version ${VERSION}
EOF
}
tarit(){	
	cd ${PKGDIR}
	rm ${PACKAGE}.tar.bz2
	cp ../${PACKAGE}.tcbuild .
	tar	cjvf ${PACKAGE}.tar.bz2 \
		${PACKAGE}.tcz* \
		${PACKAGE}.tcbuild \
		${SRC}
}
checkit(){
	sudo chown ${TCUSER}:staff ${THISDIR}
	cd ${PKGDIR}
	if [ ! -f /usr/local/tce.installed/submitqc ]; then
		if [ ! -e ${TCEDIR}/optional/submitqc ]; then
			sudo -u ${TCUSER} tce-load -w submitqc
		fi
		su ${TCUSER} -c "tce-load -is submitqc"
	fi
	submitqc
}
#here we go
clean
environment
workit
packageit
checkit
tarit
