#!/bin/sh


. /etc/init.d/tc-functions

checkroot

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe" 
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`
VERSION="3.0.7"
MIRROR="http://downloads.sourceforge.net/project/mldonkey/mldonkey/$VERSION/"
PACKAGE="mldonkey"
SEP="-"
DESCRIPTION="p2p application for ed2k kademila (amule emule), bittorrent..."
AUTHORS="Fabrice Le Fessant, Simon Patarin, amorphous-mld, mlpango, ovidr, schlumpf_, spiralvoice"
HOMEPAGE="http://mldonkey.sourceforge.net/"
LICENSE="GPLv2"
ME="jls_legalize <unsenepopiu at tin dot it>"
DEPS="gtk2.tcz \
libiconv.tcz \
librsvg.tcz \
file.tcz"
BUILDDEPS="gtk2-dev.tcz \
lablgtk-dev.tcz \
ocaml.tcz \
ocaml-dev.tcz \
librsvg-dev.tcz"
#FLAGS="--prefix=/usr/local --enable-batch --disable-gd --disable-magic --disable-donkeysui --disable-fileftp --disable-opennap --disable-gnutella --disable-gnutella2 --disable-fasttrack --disable-directconnect --disable-soulseek --enable-donkey --enable-bittorrent "
FLAGS="--prefix=/usr/local \
--enable-gui"
DOCS=""

TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tar.bz2"
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 ${BUILDDEPS} ${DEPS}; 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 xjf ${SRC}
}

buildit(){
	cd ${SRCDIR}
	make clean
	./configure ${FLAGS} || exit 1 
#	echo -n "Press Enter key."
#	read gagme
	make || exit 1 
#	echo -n "Press Enter key."
#	read gagme
	make DESTDIR=${PKGDIR}/tmp install || exit 1 
}

workit(){
   cd ${PKGDIR}/tmp
   rm -rf ${DOCS}

   mkdir -p usr/local/share/doc/License
   cp ${SRCDIR}/Copying.txt 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 -p usr/local/share/pixmaps
   cp ${THISDIR}/${PACKAGE}.png usr/local/share/pixmaps

   mkdir -p usr/local/share/applications
   cat <<EOF> usr/local/share/applications/${PACKAGE}.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name=MLDonkey
Comment=Graphical frontend for MLDonkey
Comment[fr]=Interface graphique pour MLDonkey
TryExec=mlgui
Exec=mldonkey+gui
Icon=${PACKAGE}
Categories=Network;FileTransfer;GTK;
X-FullPathIcon=/usr/local/share/pixmaps/${PACKAGE}.png
EOF
}

packageit(){
   cd ${PKGDIR}/tmp
   [ -e ../${PACKAGE}.tcz ] && rm -f ../${PACKAGE}.tcz*

   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:	protocols: 
			- eDonkey           enabled (eMule SUI enabled)
			- BitTorrent        enabled
			- FileTP (aka wget) enabled
			- Fasttrack         enabled
			- Gnutella          disabled - unmaintained
		  	- Gnutella2         disabled - unmaintained
		   	- Direct Connect    disabled
		    	- Open Napster      disabled - currently not usable
		     	- Soulseek          disabled - currently not usable
		      	- OpenFT            disabled - currently not usable
		
		U can use the provided gui or:
		after u've started the core via mlnet command u can
		control it from a browser at the address: http://localhost:4080/
		When u want to stop the core press ctrl+c in the terminal,
		otherwise closing the therminal the core wont stop.
		Another way to kill the core is to type kill in the browser 
		mldonkey page.
		Firefox MLdonkey/eMule/aMule Protocol Handler:
		https://addons.mozilla.org/en-US/firefox/addon/14043
		
		This extension is PPI compatible
Change-log:	2009/08/01 initial packaging
		2009/08/01 initial packaging
		2009/09/08 recompile with SUI and torrent enabled
		2009/11/19 updated to 3.0.1, removed dependencies
		2010/06/05 updated to 3.0.2 & removed from desktop menu
		2010/06/05 updated to 3.0.3
		2010/09/27 updated to 3.0.5 
		2011/01/08 updated to 3.0.6 
Current:	${TODAY} updated to ${VERSION} & enabled gui
EOF
}
checkit(){
	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
}
tarit(){	
	cd ${PKGDIR}
	[ -e ${PACKAGE}.tar.bz2 ] && rm ${PACKAGE}.tar.bz2
	cp ${THISDIR}/${PACKAGE}.tcbuild .
	cp ${THISDIR}/${PACKAGE}.png .
	tar cjvf ${PACKAGE}.tar.bz2 \
		${PACKAGE}.tcz* \
		${SRC} \
		${PACKAGE}.tcbuild \
		${PACKAGE}.png
}
#here we go

#clean
#environment
##echo -n "Press Enter key."
##read gagme
#buildit
##echo -n "Press Enter key."
##read gagme
#workit
packageit
#echo -n "Press Enter key."
#read gagme
checkit
#echo -n "Press Enter key."
#read gagme
tarit
