#!/bin/bash

# Slackware build script for dia

# Originally written by Matt Hayes <dominian at slackadelic dot com>
# Updated for 64bit by Ash Wiren <ash at spooksoftware dot com>    (19.08.2009)
# Maintained by Dugan Chen <thedoogster at gmail dot com>       (<= 18.09.2015)
#
# Updated and currently maintained by Philip Lacroix <slackph at posteo dot de>
#
# 10.10.2015:
# * enabled HTML documentation (based on patch by Mike Gorse, SUSE);
# * enabled SWIG, Python and Cairo plug-ins;
# * disabled Gnome code (deprecated).
# 19.09.2015:
# * reviewed and updated for dia 0.97.3.

# 20260815 bkw: Modified by SlackBuilds.org, BUILD=3:
# - make it pass sbopkglint's new typo detection. there weren't typos,
#   but there were unusual variable names.
# - fix the .desktop file.
# - verbose make (V=1) so we can see that the flags are used.

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

PRGNAM=dia
VERSION=${VERSION:-0.97.3}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

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/SBo}
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

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.xz --no-same-owner
cd $PRGNAM-$VERSION
find . ! -type l    -a \( \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \) \)

# Enable HTML documentation, necessary for Help functionality.
patch configure.in < $CWD/dia-enable-html-docs.patch

autoreconf -fi

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --with-swig \
  --with-cairo \
  --with-python \
  --disable-gnome \
  --disable-static \
  --build=$ARCH-slackware-linux

make V=1
make install-strip DESTDIR=$PKG

# 20260815 bkw: renamed these variables to avoid sbopklint thinking
# the first one was a typo for PRGNAM: s/PRG/PKG
PKGMAN=$PKG/usr/man
PKGSHR=$PKG/usr/share
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION

# 20260815 bkw: get rid of desktop-file-validate warning.
sed -i '/^Encoding/d' $PKGSHR/applications/dia.desktop

# Move French man page to correct location; compress.
mv $PKGSHR/man/fr $PKGMAN/
gzip -9 $PKGMAN/{,fr/}man1/$PRGNAM.1
rmdir $PKGSHR/man

# Install documentation.
cp -a AUTHORS COPYING ChangeLog* KNOWN_BUGS MAINTAINERS NEWS README \
      RELEASE-PROCESS THANKS TODO $PKGDOC/
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

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

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