#!/bin/sh

# Slackware build script for qcad
# (2D Computer Aided Drafting)

# Written by Niels Horn <niels.horn@gmail.com>
# Revision date: 2010/06/01

PRGNAM=qcad
VERSION=${VERSION:-2.0.5.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

# Version of source is a bit different:
SRCVERSION=${VERSION}-1-community.src

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) ARCH=$( uname -m ) ;;
  esac
fi

# Language & Version of manual
#   When writing this Slackbuild, manuals were avaliable in cs/en/de/hu
MANLANG=${MANLANG:-en}
MANVERSION=${MANVERSION:-2.1.0.0}
SRCMAN=$MANLANG-$MANVERSION-1.html

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

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

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$SRCVERSION
tar xvf $CWD/$PRGNAM-$SRCVERSION.tar.gz
cd $PRGNAM-$SRCVERSION
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

### We'll do quite some patching here, but the sources are from 2005 and a lot has
### changed since then...

# Apply patch for gcc-4x
patch -p1 < $CWD/gcc4x.patch

# If 64-bits, apply patch needed for Slackware64
[ "$ARCH" = "x86_64" ] && patch -p1 < $CWD/slack64.patch

# Apply patch to show on-line help
#   (it needs to find the "assistant" from QT3)
patch -p1 < $CWD/help.patch
sed -i "s|@QTDIR@|$QTDIR|g" qcad/src/qc_applicationwindow.cpp

# And this is an ugly patch to force the compiler to use libpng12
#   This is needed as Slackware 13.1 uses libpng14 as the default, but Qt3
#   from extra/kde3-compat was still compiled with libpng12.
#   The *correct* solution would be to rebuild Qt3 against the new version
#   of libpng, but that might me a bit to steep for some users...
patch -p1 < $CWD/libpng.patch

# Make sure we'll use the qt3 tools to build qcad
sed -i "s|qmake|/opt/kde3/bin/qmake|g" scripts/build_qcad.sh

### End of patching ###

# Start the one-in-all build-script
( cd scripts
  CFLAGS="$SLKCFLAGS" \
  ./build_qcad.sh
)

# "Compile" all .ts files to .qm
find -name *.ts | while read FILE; do
  lrelease $FILE
done

# "Install" program
mkdir -p $PKG/opt/$PRGNAM
cp -a qcad/qcad $PKG/opt/$PRGNAM/

# "Install" data & translations
mkdir -p $PKG/opt/$PRGNAM/qm
cp -a qcad/data qcad/doc qcad/fonts qcad/library qcad/machines qcad/patterns \
  $PKG/opt/$PRGNAM/
find -name *.qm | while read FILE; do
  cp $FILE $PKG/opt/$PRGNAM/qm/
done

# "Install" manual
( cd $TMP
  rm -rf $PRGNAM-manual-$SRCMAN
  unzip $CWD/$PRGNAM-manual-$SRCMAN.zip
  cd $PRGNAM-manual-$SRCMAN
  cp -a * $PKG/opt/$PRGNAM/doc/
)

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

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  qcad/README \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/usr/share/{applications,pixmaps}
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop
cp qcad/src/xpm/qcad.xpm $PKG/usr/share/pixmaps/

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}_$MANLANG-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}