#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# Packager Submax (Massimo Cavalleri)
# https://marrat.homelinux.org/D3lphin
# dolphin for kde3

NAME=d3lphin
VERSION=0.9.2
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3sm}
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-$NAME
CWD=$(pwd)
CHOST=i486

SOURCE="https://marrat.homelinux.org/D3lphin?action=AttachFile&do=get&target=$NAME-$VERSION.tar.gz"

# download source
if [ ! -e $NAME-$VERSION.tar.gz ]; then
   wget -c --no-check-certificate $SOURCE --output-document=$NAME-$VERSION.tar.gz
fi

# clean necessary before compile after old run
rm -rf $PKG
mkdir -p $PKG
rm -rf $TMP/$NAME-$VERSION

SLKLIBDIR=lib
case $ARCH in
 "i486") SLKCFLAGS="-O2 -march=i486 -mtune=i686" ;;
 "i686") SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer" ;;
 "athlon64") SLKCFLAGS="-O2 -march=athlon64 -pipe" ;;
 "athlonxp") SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer" ;;
 "x86_64") SLKCFLAGS="-O2"
           SLKLIBDIR=lib64 ;;
esac

# extract source
cd $TMP
tar xzvf $CWD/$NAME-$VERSION.tar.gz
cd $NAME-$VERSION

# Make sure ownerships and permissions are sane
chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
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 {} \;

LDFLAGS="-L/usr/$SLKLIBDIR -L/$SLKLIBDIR" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --disable-debug \
  --program-prefix= \
  --program-suffix= \
  --build=$CHOST-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1

# Strip binaries:
( cd $PKG
  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
  find . | xargs file | grep "current ar archive" | grep ELF | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

# Add a documentation directory
mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS COPYING ChangeLog INSTALL  NEWS README TODO $PKG/usr/doc/$NAME-$VERSION
cp -a $PKG/usr/share/doc/* $PKG/usr/doc/$NAME-$VERSION
rm -rf $PKG/usr/share/doc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
# man
gzip -9 $PKG/usr/man/*/*

# install directory
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# build package
cd $PKG
chown -R root:root $PKG
if [ ! -z "$(which requiredbuilder)" ] && [ -x "$(which requiredbuilder)" ]; then
   requiredbuilder -y -v -s $CWD $PKG
fi
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

# checksum and desc
(cd $CWD/..
 md5sum $NAME-$VERSION-$ARCH-$BUILD.tgz > $NAME-$VERSION-$ARCH-$BUILD.tgz.md5
cat $PKG/install/slack-desc | grep "^$NAME" > $NAME-$VERSION-$ARCH-$BUILD.txt )

# optional clean
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP
fi
