#!/bin/sh
. /etc/init.d/tc-functions
[ `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`
PACKAGE="flvstreamer"
SEP="-"
VERSION="2.1c1"
MIRROR="http://mirror.lihnidos.org/GNU/savannah/flvstreamer/source/"
DESCRIPTION="an open source command-line RTMP client"
AUTHORS="fsck"
HOMEPAGE="http://savannah.nongnu.org/projects/flvstreamer"
LICENSE="GNU General Public License v2 or later"
ME="jls_legalize (unsenepopiu at tin dot it)"
DEPS=""
BUILDDEPS="compiletc.tcz squashfs-tools-4.0.tcz"
FLAGS=""
DOCS=""

TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tar.gz"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}"
TCEDIR="$(cat /opt/.tce_dir)"
COPYING="COPYING"

clean(){
	rm -rf ${THISDIR}/${PACKAGE}
}
environment(){
	for each in ${DEPS} ${BUILDDEPS}; do
		APPNAME=$(getbasefile "$each " 1)
		if [ -f /usr/local/tce.installed/"${APPNAME}" ]; then
			continue
		else
			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
		fi
	done
	[ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
	mkdir -p ${PKGDIR}/tmp
	cd ${PKGDIR} && wget ${MIRROR}${SRC}
	tar xzf ${SRC}
}
buildit(){
	cd ${SRCDIR}
	#./configure ${FLAGS}
	make posix
	#make DESTDIR=${PKGDIR}/tmp install
}
workit(){
	cd ${PKGDIR}/tmp
	
	mkdir -p usr/local/share/doc/License
	cp "${SRCDIR}"/"${COPYING}" usr/local/share/doc/License/${PACKAGE}.txt

	mkdir -p usr/local/bin
	cp "${SRCDIR}"/"${PACKAGE}" usr/local/bin
	cp "${SRCDIR}"/streams usr/local/bin
	cp "${SRCDIR}"/rtmpsrv usr/local/bin
	cp "${SRCDIR}"/rtmpsuck usr/local/bin
	
	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
}

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:	10/03/04	First version 
Current:	${TODAY} Updated 2 ${VERSION}
EOF
}
tarit(){	
	cd ${PKGDIR}
	tar cvf ${PACKAGE}.tar ${PACKAGE}.tcz* ../${PACKAGE}.tcbuild
}
#here we go
clean
environment
buildit
workit
packageit
tarit