#!/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`
MIRROR="http://freshmeat.net/urls"
PACKAGE="libv4l"
SEP="-"
VERSION="0.7.91"
EXT="tcz"
DESCRIPTION=" Collection of video4linux support libraries"
AUTHORS="?"
HOMEPAGE="http://freshmeat.net/projects/libv4l"
LICENSE="gplv2"
ME="jls_legalize (unsenepopiu at tin dot it)"
DEPS="v4l-dvb-2.6.29.1-tinycore.tcz hwmon-2.6.29.1-tinycore.tcz"
BUILDDEPS="squashfs-tools-4.0.tcz compiletc.tcz"
FLAGS="--prefix=/usr/local"
DOCS=""
COPYING="COPYING.LIB"
TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tar.gz"
SRCFILE="5ecd7149a259c2cb1cc8b84ae8d3efe1"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="v4l-utils${SEP}${VERSION}"

environment(){
	for each in ${DEPS} ${BUILDEPS}; 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 -p ${PKGDIR}/tmp
	cd ${PKGDIR} && wget ${MIRROR}/${SRCFILE}
	mv ${SRCFILE} ${SRC}
	tar xzf ${SRC}
}

buildit(){
   cd ${SRCDIR}
   #./configure ${FLAGS}
   make
   touch /tmp/mark
   make DESTDIR=${PKGDIR}/tmp install
}

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}/${COPYING}" 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
#dev
	for I in $(find `ls` -name *.h); do 
		export DIR=`dirname "$I"`; 
		[ -d ../dev/"$DIR" ] || mkdir -p ../dev/"$DIR"; 
		mv "$I" ../dev/"$DIR"/; 
	done

	for I in $(find `ls` -name *.a); do 
		export DIR=`dirname "$I"`; 
		[ -d ../dev/"$DIR" ] || mkdir -p ../dev/"$DIR"; 
		mv "$I" ../dev/"$DIR"/; 
	done

	for I in $(find `ls` -name *.la); do 
		export DIR=`dirname "$I"`; 
		[ -d ../dev/"$DIR" ] || mkdir -p ../dev/"$DIR"; 
		mv "$I" ../dev/"$DIR"/; 
	done
	
	for I in $(find `ls` -name *.pc); do 
		export DIR=`dirname "$I"`; 
		[ -d ../dev/"$DIR" ] || mkdir -p ../dev/"$DIR"; 
		mv "$I" ../dev/"$DIR"/; 
	done

}

packageit(){
   cd ${PKGDIR}/tmp
   for dir in `ls -A`; do
      find ${dir} -not -type d | sort >> ../${PACKAGE}.${EXT}.list
   done

   mksquashfs . ../${PACKAGE}.${EXT}

   cd ${PKGDIR}
   md5sum ${PACKAGE}.${EXT} > ${PACKAGE}.${EXT}.md5.txt

   [ -f "${PACKAGE}.${EXT}.dep" ] && rm -f ${PACKAGE}.${EXT}.dep
   for each in ${DEPS}; do echo ${each} >> ${PACKAGE}.${EXT}.dep; done

   size=`du -h ${PACKAGE}.${EXT} | cut -f 1`

cat <<EOF> ${PACKAGE}.${EXT}.info
Title:		${PACKAGE}.${EXT}
Description:	${DESCRIPTION}
Version:		${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	v4l-utils is a collection of various video4linux (V4L) 
			and DVB utilities. libv4l is an accompanying collection 
			of libraries that adds a thin abstraction layer on top of 
			video4linux2 (V4L2) devices. The purpose of this layer 
			is to make it easy for application writers to support a 
			wide variety of devices without having to write separate 
			code for different devices in the same class. It consists 
			of 3 different libraries. libv4lconvert offers functions to 
			convert from any (known) pixel format to 
			V4l2_PIX_FMT_BGR24 or V4l2_PIX_FMT_YUV420. 
			libv4l1 offers the (deprecated) v4l1 API on top of v4l2 
			devices, independent of the drivers for those devices 
			supporting v4l1 compatibility (which many v4l2 drivers do not). 
			libv4l2 offers the v4l2 API on top of v4l2 devices, 
			while adding support for the application transparent 
			libv4lconvert conversion where necessary.
		
			example: LD_PRELOAD=/usr/local/lib/libv4l/v4l1compat.so mplayer...
			---------------------------------
			This extension is PPI compatible.
Change-log:	2009/05/27 Initial packaging
			2009/11/09  updated to version 0.6.1
			2009/12/04  updated to version 0.6.3
			2010/01/24  updated to version 0.6.4
Current:		${TODAY} updated to version ${VERSION}
EOF
#dev
   cd ${PKGDIR}/dev
   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} dev files
Version:		${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:${LICENSE}
Size:		${size}
Extension_by:	${ME}
Comments:	development files
			----------------------------------------
			This extension is PPI compatible
Change-log:	2009/05/27 Initial packaging
			2009/11/09  updated to version 0.6.1
			2009/12/04  updated to version 0.6.3
			2010/01/24  updated to version 0.6.4
Current:		${TODAY} updated to version ${VERSION}
EOF
}
tarit(){	
	cd ${THISDIR}
	tar cvf ${PACKAGE}.tar ${PKGDIR}/${PACKAGE}.tcz* ${PKGDIR}/${PACKAGE}-dev* ${PACKAGE}.tcbuild
}	

#here we go

environment
buildit
workit
packageit
tarit
