#!/bin/bash
# packager           : Frederic Boulet <bipbip> bipbiponlinux~AT~gmail.com
# homepage           : http://icculus.org/physfs/
# source location    : http://offload3.icculus.org:9090/physfs/downloads/physfs-2.0.0.tar.gz
# last revised       : 3.12.2009
# dep                : 

softname='physfs'
softversion='2.0.0'
packageversion='1fb'

arch='i486'
cpu='i686'

# log ;)
(

# parameters needed for the build process
buildir=$(pwd)
srcpkg="$buildir/$softname-$softversion.tar.gz"
src="$buildir/$softname-$softversion"
package="$softname-$softversion-$arch-$packageversion"
dest="$buildir/$package"
source=http://offload3.icculus.org:9090/physfs/downloads/$softname-$softversion.tar.gz

# prepare the build result directory
dest="$buildir/$package"
rm -rf $dest
mkdir -p $dest
mkdir -p $dest/install
mkdir -p $dest/usr/src/$softname-$softversion

# Slack-desc
cat <<EOF > $dest/install/slack-desc
      |-----handy-ruler------------------------------------------------------|
physfs: physfs (Library to provide abstract access to various archives)
physfs: 
physfs: PhysicsFS is intended for use in video games, and the design 
physfs: was somewhat inspired by Quake 3's file subsystem.
physfs: 
physfs: 
physfs: 
physfs: 
physfs: 
physfs:
physfs: 
EOF

# check if source is present
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

# extract the source code
cd $buildir
tar xvf $srcpkg
cd $src

# build
export CFLAGS="-O2 -march=$arch -mtune=$cpu"
export CXXFLAGS="-O2 -march=$arch -mtune=$cpu"

cmake . \
  -DCMAKE_INSTALL_PREFIX:STRING="/usr" \
  -DCMAKE_CXX_FLAGS:STRING="-O2 -march=i486 -mtune=i686" \
  -DCMAKE_C_FLAGS:STRING="-O2 -march=i486 -mtune=i686"

make
make install DESTDIR=$dest
mv $dest/usr/local/* $dest/usr
rm -rf $dest/usr/local

# add 'default' files
mkdir -p $dest/usr/doc/$softname-$softversion
cp -a \
   CHANGELOG.txt CREDITS.txt INSTALL.txt LICENSE.txt TODO.txt\
   $dest/usr/doc/$softname-$softversion
        
# strip executables
( cd $dest
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

# compress man pages
( cd $dest/usr/man
  find . -name "*.?" | xargs gzip -9
)

# ZenBuild
cp $buildir/build-$softname.sh $dest/usr/src/$softname-$softversion

# set target 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 {} \;

# real packaging work:
cd $dest
makepkg -l y -c n $dest.txz

# computes the md5sum signature for the package
cd $buildir
md5sum $package.txz > $package.md5

# .dep
cd $buildir
echo -e "zlib" > $package.dep

# src file
echo http://zenwalk.pinguix.com/user-accounts/bip/divers/salix/l/physfs/build-physfs.sh > $package.src
echo $source >> $package.src

# erase
rm -rf $src
rm -rf $dest

# log ;)
) 2>&1 | tee "$softname-$softversion-build.log"
