#!/bin/sh
# Heavily based on the Slackware 12.0 SlackBuild
# http://mozcache.sourceforge.net/
# http://www.slacky.it

NAME=mozcache
VERSION=1.0.4
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1bj}

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-$NAME
rm -rf $PKG 
mkdir -p $PKG/usr/share/{applications,pixmaps} 

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

SOURCE= http://ovh.dl.sourceforge.net/sourceforge/mozcache/$NAME-$VERSION.tar.gz

if [ ! -e $NAME-$VERSION.tar.gz ]; then
  wget -c $SOURCE
fi

cd $TMP
rm -rf $NAME-$VERSION

# unpack the sources and fix their permissions
tar -xvzf $CWD/$NAME-$VERSION.tar.gz || exit 1
cd $NAME-$VERSION
chown -R root:root .
chmod -R u+w,go+r-w,a-s .
echo ""
echo "Starting compilation :)"

# run configure script
CFLAGS="$SLKCFLAGS" CXXFLAGS="$SLKCFLAGS" \
./configure \
 --prefix=/usr \
 --program-prefix= \
 --program-suffix= \
 --build=$ARCH-slackware-linux || exit 1
 
make || exit 1

# redirect installation of the package
make DESTDIR=$PKG install-strip || exit 1

# copy the docs
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS COPYING ChangeLog README $PKG/usr/doc/$NAME-$VERSION/

# Fix menu entry
convert mozcache.xpm -resize 48x48 mozcache.png; cp mozcache.png $PKG/usr/share/pixmaps
sed -i -e "s|Icon=.*|Icon=/usr/share/pixmaps/mozcache.png|g" $NAME.desktop
cp -a $NAME.desktop $PKG/usr/share/applications

# copy the slack-desc
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild

# build the package
cd $PKG
chown -R root:root .
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

# clean up the extra stuff
if [ "$1" = "--cleanup" ]; then 
  rm -rf $TMP 
fi
