#!/bin/bash
#
# Copyright Jean-Philippe Guillemin <h1p8r10n@gmail.com>. This program is free software; you can redistribute
# it and/or modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at your option)
# any later version. Please take a look at http://www.gnu.org/copyleft/gpl.htm


NAME='gkeyring'
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" \

python setup.py install --root=$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-------------------------------------------------------|
gkeyring: gkeyring (Python CLI for Gnome keyring)
gkeyring: 
gkeyring: Python CLI for Gnome keyring allow keyring management from 
gkeyring: Command line
gkeyring: 
gkeyring: 
gkeyring: 
gkeyring: 
gkeyring:
gkeyring: 
gkeyring: 
gkeyring: 

EODESC

# Unlock startup service

mkdir -p $PKG/usr/bin
cat $CWD/gnome-keyring-unlock > $PKG/usr/bin/gnome-keyring-unlock
chmod 755 $PKG/usr/bin/gnome-keyring-unlock

mkdir -p $PKG/etc/xdg/autostart
cat $CWD/gnome-keyring-unlock.desktop > $PKG/etc/xdg/autostart/gnome-keyring-unlock.desktop
chmod 644 $PKG/etc/xdg/autostart/gnome-keyring-unlock.desktop

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
