#!/bin/bash
#
# source: http://ftp.gnome.org/pub/gnome/sources/
#

# packager : me !

NAME='seahorse'
VERSION=${VERSION:-$(echo $NAME-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
BUILD="1jp"

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) export ARCH=i486 ;;
    arm*) export ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) export ARCH=$( uname -m ) ;;
  esac
fi

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

CWD=$(pwd)
srcpkg="$CWD/$NAME-$VERSION.tar.?z"
src="$CWD/$NAME-$VERSION"
package="$NAME-$VERSION-$ARCH-$BUILD"
PKG="/tmp/$package"

# ----------------- Extract sources and get ready to build
cd $CWD
rm -rf $src
tar -xvf $srcpkg
rm -f $PKG.tgz
rm -rf $PKG

cd $src

chown -R root:root .

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \

./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --mandir=/usr/man \
  --localstatedir=/var \
  --disable-schemas-compile \
  --disable-libsystemd-login \
  --disable-static 

make -j6
make install DESTDIR=$PKG 


find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

gzip -9 $PKG/usr/man/man?/*.?

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
  AUTHORS ChangeLog COPYING INSTALL license_change NEWS README TODO \
  $PKG/usr/doc/$NAME-$VERSION


mkdir -p $PKG/install
cat << "EODESC" > $PKG/install/slack-desc
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

     |-----handy-ruler-------------------------------------------------------|
seahorse: seahorse (Manager for the Freedesktop keyring system)
seahorse: 
seahorse: Seahorse is an application for managing keyrings,
seahorse: in which any kind of sensitive information can be kept, mainly
seahorse: passwords. The default window shows 'default' keyrings with their
seahorse: stored items, and allows addition, removal and edition.
seahorse: The manager also has creation, opening and lock/unlock features. 
seahorse: 
seahorse:
seahorse: 
seahorse: 
seahorse: 

EODESC

cat << DOINST > $PKG/install/doinst.sh
if [ -x /usr/bin/update-desktop-database ]; then
  usr/bin/update-desktop-database -q usr/share/applications >/dev/null 2>&1
fi

if [ -x /usr/bin/gtk-update-icon-cache ]; then
  usr/bin/gtk-update-icon-cache usr/share/icons/hicolor >/dev/null 2>&1
fi

if [ -x /usr/bin/glib-compile-schemas ]; then
  usr/bin/glib-compile-schemas usr/share/glib-2.0/schemas >/dev/null 2>&1
fi
DOINST

mkdir -p $PKG/usr/src/$NAME-$VERSION
cp $CWD/$NAME.SlackBuild $PKG/usr/src/$NAME-$VERSION

# Packaging
cd $PKG
makepkg -l y -c n $PKG.txz
cd $CWD
rm -rf $src $PKG
