#!/bin/bash

# Slackware build script for libpst

# Copyright 2011-2012 Roberto Neri, Palermo, Italy
#
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20220510 bkw: old website went 404, project moved to github. thanks to
# Ozan Türkyılmaz for reporting this!
# 20211019 bkw:
# - upgrade to 0.6.76
# - build both python 2 and 3 modules
# - get rid of .la files
# 20201025 bkw: upgrade to 0.6.75
# 20200224 bkw: upgrade to 0.6.74
# 20180917 bkw: upgrade to 0.6.72
# 20170122 bkw: upgrade to 0.6.69
# 20161017 bkw: upgrade to 0.6.68
# 20160803 bkw: upgrade to 0.6.67
# 20150513 bkw: upgrade to 0.6.64

# 20140314 bkw
# - took over maintenance
# - upgrade to 0.6.63
# - make install-strip instead of slow find stuff

wget -c https://www.five-ten-sg.com/libpst/packages/libpst-0.6.76.tar.gz

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=libpst
VERSION=${VERSION:-0.6.76}
BUILD=${BUILD:-1}
TAG=${TAG:-_gfs}
PKGTYPE=${PKGTYPE:-txz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/gfs}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -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

set -e

# 20220510 bkw: the github release URL has a ton of cloud-ey CGI
# parameters. wget (with or without content-disposition) saves it
# with the right filename, but be paranoid about it anyway:
TARBALL=$CWD/$PRGNAM-$VERSION.tar.gz
[ -e $TARBALL ] || TARBALL="$( /bin/ls -1 $TARBALL* | head -1 )"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $TARBALL
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

buildit() {
  autoreconf -if

  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  ./configure \
    --prefix=/usr \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --mandir=/usr/man \
    --enable-dii \
    --disable-static \
    --enable-libpst-shared \
    --enable-python \
    --build=$ARCH-slackware-linux

  make
  # ./configure options --docdir and --htmldir have no effect,
  # it is necessary to override "htmldir" and "htmldeveldir"
  make install-strip DESTDIR=$PKG \
    htmldir=/usr/doc/$PRGNAM-$VERSION \
    htmldeveldir=/usr/doc/$PRGNAM-$VERSION/devel
}

# 20211019 bkw: python3 builds work now. Include support for both
# python versions, 2 and 3. Unfortunately this means building the
# entire project twice (keeping only the python2 stuff from the
# first build). autotools doesn't make it easy or even necessarily
# possible to build only part of a project :(

# Python 3 build
buildit
[ -e $PKG/.keep.python2 ] && \
  mv $PKG/.keep.python2 $PKG/usr/lib$LIBDIRSUFFIX/python$PY2VER

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

rm -f $PKG/usr/lib*/*.la $PKG/usr/lib*/python*/*/*.la

# Package documentation has aready been copied
# into $PKG/usr/doc/$PRGNAM-$VERSION by "make install"
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

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

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
