#!/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 CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fexceptions -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="amsn"
SEP="-"
VERSION="0.98.4"
MIRROR="http://sourceforge.net/projects/amsn/files/amsn/${VERSION}/"
DESCRIPTION="a free open source MSN Messenger clone"
AUTHORS="amsn team"
HOMEPAGE="http://www.amsn-project.net/"
LICENSE="GPLv2"
ME="jls_legalize (unsenepopiu at tin dot it)"
DEPS="farsight2.tcz \
gstreamer.tcz \
libv4l.tcz \
gupnp-igd.tcz \
tk.tcz"
BUILDEPS="Xorg-7.5-dev.tcz \
farsight2-dev.tcz \
gstreamer-dev.tcz \
libv4l-dev.tcz \
gupnp-igd-dev.tcz \
tk-dev.tcz"
FLAGS=""
DOCS="usr/local/share/amsn/docs"
TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}-src.tar.gz"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}${SEP}${VERSION}"
TCEDIR="$(cat /opt/.tce_dir)"

clean(){
	rm -rf ${THISDIR}/${PACKAGE}
}
environment(){
	for each in squashfs-tools-4.x.tcz compiletc.tcz ${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}${SRC}
	tar xzf ${SRC}
}

buildit(){
	cd ${SRCDIR}
	./configure ${FLAGS} || exit 1
	make || exit 1
	make DESTDIR=${PKGDIR}/tmp install || exit 1
}

workit(){
	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

 	echo "X-FullPathIcon=/usr/local/share/pixmaps/${PACKAGE}.png" >> usr/local/share/applications/${PACKAGE}.desktop
	cd usr/local/share/pixmaps/
	rm ${PACKAGE}.png
	ln -s ../amsn/desktop-icons/48x48/apps/${PACKAGE}.png
	cd ${PKGDIR}/tmp

	#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="$PACKAGE">$PACKAGE</Program>
#</JWM>
#EOF
#	chmod 644 usr/local/tce.menu/${PACKAGE}
#
#	mkdir -pm 775 usr/local/tce.icons
#	cp ${SRCDIR}/gtk/$PACKAGE.png usr/local/tce.icons/
#	cat <<EOF> usr/local/tce.icons/${PACKAGE}
#i: /usr/local/tce.icons/$PACKAGE.png
#t: $PACKAGE
#c: $PACKAGE
#EOF
#	chmod 644 usr/local/tce.icons/*

	#dont clobber tc setup
#	chown -R root:staff usr/local/tce.*

	#package specific stuff
	#locale
	#mkdir -p ../locale/usr/local/share/amsn/lang
	#mv usr/local/share/amsn/lang ../locale/usr/local/share/amsn/
}

packageit(){
   rm ${PKGDIR}/${PACKAGE}.tcz*

   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:	Webcam is working only between amsn and amsn
		This extension is PPI compatible.
Change-log:	2010/10/15 First version
Current:	${TODAY} Updated to ${VERSION} 
EOF

#   #locale
#   cd ${PKGDIR}/locale
#   for dir in `ls -A`; do
#      find ${dir} -not -type d | sort >> ../${PACKAGE}-locale.tcz.list
#   done
#
#   mksquashfs . ../${PACKAGE}-locale.tcz
#
#   cd ${PKGDIR}
#   md5sum ${PACKAGE}-locale.tcz > ${PACKAGE}-locale.tcz.md5.txt
#
#   echo ${PACKAGE}.tcz > ${PACKAGE}-locale.tcz.dep
#   
#   size=`du -h ${PACKAGE}-locale.tcz | cut -f 1`
#
#   cat <<EOF> ${PACKAGE}-locale.tcz.info
#Title:		${PACKAGE}-locale.tcz
#Description:	${DESCRIPTION} locale files
#Version:	${VERSION}
#Author:		${AUTHORS}
#Original-site:	${HOMEPAGE}
#Copying-policy:	${LICENSE}
#Size:		${size}
#Extension_by:	Jason W, ${ME}	
#Comments:	locale files
#		----------------------------------------	
#		This extension is PPI compatible
#Change-log:	-------------
#Current:	${TODAY} First version
#EOF
}
tarit(){	
	cd ${PKGDIR}
	rm ${PACKAGE}.tar.bz2
	cp ../${PACKAGE}.tcbuild .
	tar	cjvf ${PACKAGE}.tar.bz2 \
		${PACKAGE}.tcz* \
		${PACKAGE}.tcbuild 
#		${SRC}
}
	
#here we go
#clean
#environment
#buildit
#workit
#packageit
tarit
