#!/bin/bash
 
# Packager : Markus Muttilainen <stillborn.zenwalk@gmail.com>
#
# Packaging day : 28/03/2010
#
#package			: cowsay
#homepage			: http://www.nog.net/~tony/warez/cowsay.shtml
#source location                : http://www.nog.net/~tony/warez/cowsay-3.03.tar.gz
#dependency: perl
#> log
(

softname='cowsay'
softversion='3.03'
zenver='64.1'
arch='noarch'
cpu='i686'
numjobs='-j3'

#>

buildir=$(pwd)
src="$buildir/$softname-$softversion"
srcpkg="$buildir/$softname-$softversion.tar.gz"
package="$softname-$softversion-$arch-$zenver"
dest="$buildir/$package"
source="http://www.nog.net/~tony/warez/cowsay-3.03.tar.gz"
patch="http://zenwalk.pinguix.com/user-accounts/stillborn/cowsay/cowsay.zen.patch"
desc="http://zenwalk.pinguix.com/user-accounts/stillborn/cowsay/slack-desc"
sourcetemplate="http://zenwalk.pinguix.com/user-accounts/stillborn/cowsay"

#>
set -e -u -x
rm -rfv $dest
rm -fv $package.{txz,src,dep,md5}
mkdir -p $dest/{install,usr/{doc/$softname-$softversion,src/$softname-$softversion}}

#> check for sources
if [ -f $softname-$softversion.tar.gz ]; then
	echo "Source file already here, no need to download";
else
 	echo "Downloading source"
	wget -c --no-check-certificate $source
fi

#> check for patch
if [ -f $softname.zen.patch ]; then
        echo "Patch file already here, no need to download";
else
        echo "Downloading patch file"
        wget -c  $patch
fi

#> check for slack-desc
if [ -f slack-desc ]; then
        echo "slack-desc file already here, no need to download";
else
        echo "Downloading slack-desc file"
        wget -c  $desc
fi

#> extract sources
# tar.gz : tar zxvf $srcpkg
# tar.bz2: tar xjvf $srcpkg
# tgz    : tar xzvf $srcpkg
cd $buildir
tar zxvf $srcpkg
cd $src	

patch -Np1 -i $buildir/cowsay.zen.patch
DESTDIR=$dest sh ./install.sh /usr

#> prepare slack-desc
cp -v $buildir/slack-desc $dest/install

#> add docs,infos ...
(cd $src
cp -av \
      ChangeLog INSTALL LICENSE MANIFEST README pgp_public_key.txt \
      $dest/usr/doc/$softname-$softversion
)


#> Strip executables
( cd $dest
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null
)

#> Compress man
( 
  cd $dest/usr/man
  find . -type f -name "*.?" -exec gzip -9 {} \;
  for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

#> store buildscript 
(
cd $buildir
cp -v build-$softname.sh $dest/usr/src/$softname-$softversion
cp -v $softname.zen.patch $dest/usr/src/$softname-$softversion
)

#> fix permissions
(
chown -R root.root $dest
cd $dest
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
)

#> helperline
(cd $dest;find . -printf "%y %m %u:%g %p %l\n") >$buildir/pkgtree.log

#> do the actual zenwalk package
(
cd $dest
makepkg -l y -c n $dest.txz
)

#> prepare .src .dep .md5
cd $buildir
md5sum $package.txz > $package.md5
echo perl > $package.dep
echo $sourcetemplate/build-$softname.sh > $package.src
echo $patch >> $package.src
echo $source >> $package.src

#>
rm -rf $src
rm -rf $dest


#> log
) 2>&1 | tee "cowsay-build.log.txt"
