#!/bin/sh
# claws-mail a GTK+ based e-mail client
# http://sourceforge.net/project/showfiles.php?group_id=25528
#
# Maintainer: til <rob DOT til DOT freedman AT googlemail DOT com>
#
#set -u -e -x

version='3.7.8'
build='66.1'
arch=${arch:-i486}
tune=${tune:-i686}

MCFLAGS="-Os -march=$arch -mtune=$tune"
THEME_TAR=claws-mail-themes-20100115.tar.gz
THEME_DIR=$(basename $THEME_TAR .tar.gz)

# extra plugins packaged with main
plugins='notification_plugin att_remover'

#CONFIG_NLS="--disable-nls "
CONFIG_NLS=${CONFIG_NLS:-""}
CONFIG_IPV6="--disable-ipv6"

PKG_SRC="http://zenwalk.pinguix.com/user-accounts/tilderb/extra/xap/claws-mail/build-claws-mail.sh
http://www.claws-mail.org/themes/$THEME_TAR
http://downloads.sourceforge.net/project/sylpheed-claws/Claws%20Mail/$version/claws-mail-$version.tar.bz2
http://downloads.sourceforge.net/project/sylpheed-claws/extra%20plugins/$version/claws-mail-extra-plugins-$version.tar.bz2"

pkgname=claws-mail-$version 
xtrname=claws-mail-extra-plugins-$version

package=$pkgname-$arch-$build
pkgplug=$xtrname-$arch-$build
pkgthemes=$THEME_DIR-noarch-$build

CWD=$(pwd)
TMP=${TMP:-$CWD/tmp}

PKG=$TMP/$package
XTR=$TMP/$pkgplug
THM=$TMP/$pkgthemes

[ -d $PKG ] && rm -fr $PKG
[ -d $XTR ] && rm -fr $XTR
[ -d $THM ] && rm -fr $THM

mkdir -p $PKG/{install,usr/{src,doc}/$pkgname}
mkdir -p $XTR/{install,usr/{src,doc}/$xtrname}
mkdir -p $THM/{install,usr/share/claws-mail/themes}

tar xf $CWD/$xtrname.tar.bz2 -C $TMP || exit 1
tar xf $CWD/$pkgname.tar.bz2 -C $TMP || exit 1
tar xf $CWD/$THEME_TAR --strip-components=1 -C $THM/usr/share/claws-mail/themes 

## add some themes in default build
mkdir -p $PKG/usr/share/claws-mail/themes
mv $THM/usr/share/claws-mail/themes/{Tango_v1.2.1,Graphitte-0.9.7} \
	$PKG/usr/share/claws-mail/themes 
#
## main package
#
cd $TMP/$pkgname
# helper line
#./configure --help

CFLAGS="$MCFLAGS" CXXFLAGS="$MCFLAGS" \
./configure \
    --prefix=/usr \
    --mandir=/usr/man \
    --sysconfdir=/etc \
    --docdir=/usr/doc/claws-mail-$version \
    --with-config-dir=.config/claws-mail \
    --disable-networkmanager-support \
    --disable-spamassassin-plugin \
    --disable-gnomeprint \
    --disable-compface \
    --disable-static \
    --disable-jpilot \
    --disable-ldap \
    --disable-rpath \
    ${CONFIG_IPV6} \
    ${CONFIG_NLS} \
    --build=$arch-slackware-linux \
    --host=$arch-slackware-linux 
#   
make || exit 1
make install DESTDIR=$PKG

gzip -9c ChangeLog >$PKG/usr/doc/$pkgname/ChangeLog.gz
(cp -a AUTHORS COPYING NEWS README TODO RELEASE_NOTES $PKG/usr/doc/$pkgname)

cp -a COPYING $XTR/usr/doc/$xtrname
#
## claws-mail-extra-plugins
#
cd $TMP/$xtrname
cp -a README $XTR/usr/doc/$xtrname

# fixup pkg-config path
sed -i "s,prefix=/usr,prefix=$PKG/usr,g" \
		$PKG/usr/lib/pkgconfig/claws-mail.pc
		

allplugins=$(find . -type d -maxdepth 1 -mindepth 1 -printf "%f\n"|sort)
echo $allplugins

export PKG_CONFIG_PATH="$PKG/usr/lib/pkgconfig:$PKG_CONFIG_PATH"
for plugin in $allplugins; do
  pushd $plugin
# ./configure --help >$CWD/$plugin.help
  
  CFLAGS="$MCFLAGS" CXXFLAGS="$MCFLAGS" \
  	./configure --prefix=/usr \
  	--disable-static ${CONFIG_NLS} \
  	--host=$arch-slackware-linux \
  	--build=$arch-slackware-linux && make &&\
  (
  	if echo "$plugins" | grep -q "${plugin%-*}"; then
		make install DESTDIR=$PKG
    	DOCS=$PKG/usr/doc/$pkgname
  	else
    	make install DESTDIR=$XTR
    	DOCS=$XTR/usr/doc/$xtrname
  	fi
  	mkdir -p $DOCS/$plugin
  	cp -a AUTHORS NEWS README ChangeLog $DOCS/$plugin
  )
  popd
done

# helperline
#(cd $PKG; find . -printf "%y %m %u:%g %12s %p  %l\n" >$CWD/pkgfiles)
#(cd $XTR; find . -printf "%y %m %u:%g %12s %p  %l\n" >$CWD/xtrfiles)

# strip symbols
find $PKG/usr $XTR/usr/lib | xargs -r file | \
	grep -e "executable" -e "shared object" | grep ELF |\
    cut -f 1 -d : | xargs -r strip --strip-unneeded
#
## fixup base pkg
#
cd $PKG
gzip -9 $PKG/usr/man/man1/claws-mail.1

# icons
rm -fr usr/share/icons/hicolor/128x128/
mkdir -p usr/share/icons/hicolor/{24x24,32x32}/apps
for size in 24x24 32x32; do
  convert -size $size -depth 8 usr/share/icons/hicolor/48x48/apps/claws-mail.png \
    usr/share/icons/hicolor/$size/apps/claws-mail.png
done
# keep only default locale 
#rm -fr usr/doc/$pkgname/manual/{es,fr,pl}/

# html & txt should be enough
[ -d usr/doc/$pkgname/manual ] && \
  find usr/doc/$pkgname/manual -type f \
  	\( -iname '*.ps' -or -iname '*.pdf' \) | xargs -r rm -v

find usr/doc -type f ! -size +4c | xargs -r rm -v
rm -vfr usr/{include,lib/pkgconfig}

# make a slack-desc file
#         |-----handy-ruler-------------------------------------------------------|
cat <<EOF > install/slack-desc
claws-mail: claws-mail (GTK+ based e-mail client)
claws-mail: 
claws-mail: An email client, based on GTK+, aiming for quick response gracefull 
claws-mail: and sophisticated interface, easy configuration intuitive operation,
claws-mail: abundant features... 
claws-mail: 
EOF

# doinst.sh
cat <<"EOF" > install/doinst.sh
#!/bin/sh
#
# move config dir ~/.claws-mail to ~/.config/claws-mail
#
grep ":x:\([0-9]\)\{4\}:" /etc/passwd | awk -F":" '{ print $1, $6 }' |\
	while read huser hdir; do 
    [ -d $hdir/.claws-mail ] || continue;
    [ -d $hdir/.config/claws-mail ] && continue;
    ( cd $hdir;
    	echo "Move ~/.claws-mail .config/claws-mail for user $huser"
    	su $huser -c "mv .claws-mail .config/claws-mail "
    )
done
EOF
cp $CWD/build-claws-mail.sh usr/src/$pkgname
fakeroot /sbin/makepkg -l y -c n $CWD/$package.txz
#
## fixup extra plugins
#
cd $XTR
find usr/doc -type f ! -size +4c | xargs -r rm -v
rm -vfr usr/include

#  slack-desc file      |-----handy-ruler-------------------------------------------------------|
cat <<EOF > install/slack-desc
claws-mail-extra-plugins: claws-mail-extra-plugins (used by claws-mail)
claws-mail-extra-plugins: 
claws-mail-extra-plugins: An email client, based on GTK+, aiming for quick response gracefull 
claws-mail-extra-plugins: and sophisticated interface, easy configuration intuitive operation,
claws-mail-extra-plugins: abundant features...
claws-mail-extra-plugins: 
EOF

# build script
( cd usr/src/$xtrname
  ln -s ../$pkgname/build-claws-mail.sh build-claws-mail-extra-plugins.sh
)
fakeroot /sbin/makepkg -l y -c n $CWD/$pkgplug.txz

cat <<EOF > $THM/install/slack-desc
claws-mail-extra-themes: claws-mail-extra-themes (to be used by claws-mail)
claws-mail-extra-themes: 
claws-mail-extra-themes: An email client, based on GTK+, aiming for quick response gracefull 
claws-mail-extra-themes: and sophisticated interface, easy configuration intuitive operation,
claws-mail-extra-themes: abundant features...
claws-mail-extra-themes: 
EOF
cd $THM
fakeroot /sbin/makepkg -l y -c n $CWD/$pkgthemes.txz

cd $CWD
echo "$PKG_SRC" > $package.src
echo "$PKG_SRC" > $pkgplug.src
echo "$PKG_SRC" > $pkgthemes.src

depfinder $PKG > $package.dep
depfinder $XTR | sed "s:cairo,:cairo,claws-mail,:" > $pkgplug.dep
:> $pkgthemes.dep

md5sum $package.txz > $package.md5
md5sum $pkgplug.txz > $pkgplug.md5
md5sum $pkgthemes.txz >$pkgthemes.md5
