#!/bin/sh

. /etc/init.d/tc-functions

checkroot

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe -liconv"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe -fno-exceptions -fno-rtti"
export CPPFLAGS="-I/usr/local/include"
export LDFLAGS="-Wl,-O1"

TODAY=`date +%Y/%m/%d`
MIRROR="http://download.transmissionbt.com/files/"
PACKAGE="transmission"
SEP="-"
VERSION="2.21"
DESCRIPTION="Bittorrent client"
AUTHORS="Charles Kerr, Mitchell Livingston"
HOMEPAGE="www.transmissionbt.com"
LICENSE="GPL/MIT"
ME="Kingdomcome"
DEPS="curl.tcz libevent.tcz libiconv.tcz"
GTKDEPS="gtk2.tcz hicolor-icon-theme.tcz"
BUILDEPS="curl-dev.tcz gettext.tcz gtk2-dev.tcz intltool.tcz
          libiconv.tcz libiconv-dev.tcz libxcb-dev.tcz openssl-0.9.8-dev.tcz
          perl5.tcz libevent-dev.tcz"
FLAGS="--enable-gtk"
DOCS="usr/local/share/man"

TCUSER=`cat /etc/sysconfig/tcuser`
SRC="${PACKAGE}${SEP}${VERSION}.tar"
THISDIR=`pwd`
PKGDIR="${THISDIR}/${PACKAGE}"
SRCDIR="${PKGDIR}/${PACKAGE}${SEP}${VERSION}"

environment(){
   for each in squashfs-tools-4.x.tcz compiletc.tcz ${DEPS} \
   ${GTKDEPS} ${BUILDEPS}; do
      sudo -u ${TCUSER} tce-load -wi ${each}
   done
   [ -d "${PKGDIR}" ] && rm -rf ${PKGDIR}
   mkdir -pm 777 ${PKGDIR}/tmp
   cd ${PKGDIR} && wget ${MIRROR}${SRC}.xz
   xz -d ${SRC}.xz && tar xf ${SRC}

}

buildit(){
	cd ${SRCDIR}
	./configure ${FLAGS} || exit 1
	make || exit 1
	make DESTDIR=${PKGDIR}/tmp install-strip || 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/icons/hicolor/24x24/apps/${PACKAGE}.png" >> usr/local/share/applications/${PACKAGE}-gtk.desktop
#   ln -s usr/local/share/icons/hicolor/24x24/apps/${PACKAGE}.png usr/local/share/pixmaps/
   cd ${PKGDIR}/tmp

   #package specific stuff

   #locale
   mkdir -p ../locale/usr/local/share
   mv usr/local/share/locale ../locale/usr/local/share

   #gtk
   mkdir -p ../gtk/usr/local/bin
   mv usr/local/bin/transmission-gtk ../gtk/usr/local/bin/
   mkdir -p ../gtk/usr/local/share
   mv usr/local/share/icons usr/local/share/applications \
      usr/local/share/pixmaps ../gtk/usr/local/share
}

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

   [ -f "${PACKAGE}.tcz.dep" ] && rm -f ${PACKAGE}.tcz.dep
   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:	Jason W,  jls_legalize, ${ME}
Comments:	This extension contains transmission-cli,
		transmission-daemon, and transmission-remote.
		Depends on curl.tcz and openssl-0.9.8.tcz.
		-----------------------------
		This extension is PPI compatible.
Change-log:	2009/05/17 First version 1.61
		2009/09/11 Updated to 1.74.
		2010/02/25 Updated to 1.91
		2010/03/04 recompiled with tc 2.9
		2010/03/21 Updated to 1.92
		2010/06/18 Updated to 1.93
		2010/06/21 Updated to 2.00
		2010/07/02 Updated to 2.01
		2010/08/12 Updated to 2.04
		2010/10/13 Updated to 2.10
		2010/10/22 Updated to 2.11
		2010/12/30 Updated to 2.13
		2011/01/18 Updated to 2.13+
Current:	${TODAY} Split gtk binary out (Kingdomcome)
			   Updated to 2.21
EOF

#gtk
   cd ${PKGDIR}/gtk
   for dir in `ls -A`; do
      find ${dir} -not -type d | sort >> ../${PACKAGE}-gtk.tcz.list
   done

   mksquashfs . ../${PACKAGE}-gtk.tcz

   cd ${PKGDIR}
   md5sum ${PACKAGE}-gtk.tcz > ${PACKAGE}-gtk.tcz.md5.txt

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

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

   cat <<EOF> ${PACKAGE}-gtk.tcz.info
Title:		${PACKAGE}-gtk.tcz
Description:	GTK2 ${DESCRIPTION}
Version:	${VERSION}
Author:		${AUTHORS}
Original-site:	${HOMEPAGE}
Copying-policy:	${LICENSE}
Size:		${size}
Extension_by:	Jason W, jls_legalize, ${ME}
Comments:	This extension contains the gtk component of the
		transmission bittorrent client
		----------------------------------------
		This extension is PPI compatible
Change-log:	2009/05/17 First version 1.61
		2009/09/11 Updated to 1.74
		2010/02/25 Updated to 1.91
		2010/03/04 recompiled with tc 2.9
		2010/03/21 Updated to 1.92
		2010/06/18 Updated to 1.93
		2010/06/21 Updated to 2.00
		2010/07/02 Updated to 2.01
		2010/08/12 Updated to 2.04
		2010/10/13 Updated to 2.10
		2010/10/22 Updated to 2.11
		2010/11/17 Updated to 2.12
		2010/12/30 Updated to 2.13
		2011/01/18 Updated to 2.13+
Current:	${TODAY} Split gtk binary out (Kingdomcome)
			   Updated to 2.21
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}-gtk.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, jls_legalize, ${ME}
Comments:	locale files for Transmission
		----------------------------------------
		This extension is PPI compatible
Change-log:	2009/05/17 First version 1.61
		2009/09/11 Updated to 1.74
		2010/02/25 Updated to 1.91
		2010/03/04 recompiled with tc 2.9
		2010/03/21 Updated to 1.92
		2010/06/18 Updated to 1.93
		2010/06/21 Updated to 2.00
		2010/07/02 Updated to 2.01
		2010/08/12 Updated to 2.04
		2010/10/13 Updated to 2.10
		2010/10/22 Updated to 2.11
		2010/11/17 Updated to 2.12
		2010/12/30 Updated to 2.13
		2011/01/18 Updated to 2.13+
Current:	${TODAY} Split gtk binary out (Kingdomcome)
			   Updated to 2.21
EOF

}

#here we go
environment &&
buildit &&
workit &&
packageit
