#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild Andrea Sciucca ( gohanz at infinito.it) 
# http://qelectrotech.tuxfamily.org/
# Packager David666
# 
#
# 
# Exit on most errors
set -e

# current working directory, where the .txz file shall be generated
CWD=`pwd`
# absolute path to this SlackBuild
THIS_SLACKBUILD=$(readlink -f $0)
# information to fetch sources
NAME=qelectrotech
VERSION=0.3-src
SOURCE=http://download.tuxfamily.org/qet/tags/20130928/$NAME-$VERSION.tar.gz 

# Build tag (here, initials of the packager)
BUILD=dp
ARCH=${ARCH:-$(uname -m)}
JOBS=${JOBS:-$(expr $(grep -c ^processor /proc/cpuinfo) + 1)}
wget $SOURCE





echo -e "\E[0;32m+-------------------------------+\E[0;0m"
echo -e "\E[0;32m| Start SlackBuild QElectroTech |\E[0;0m"
echo -e "\E[0;32m+-------------------------------+\E[0;0m"

# directories and files are created with mode 644 and 755 resp.
umask 0022

TMP=${TMP:-/tmp/txz/$NAME}
PKG=$TMP/$NAME
mkdir -p $TMP $PKG
mv $NAME-$VERSION.tar.gz $TMP
cd $TMP

tar xvzf $NAME-$VERSION.tar.gz
cd $NAME-$VERSION

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
fi

# Build Qt4 Qelectrotech
# Configure:
sed -i s,usr\/local,usr,g qelectrotech.pro
sed -i s,..\/share\/mime\/,share\/mime\/,g qelectrotech.pro
sed -i s,..\/share\/mimelnk\/,share\/mimelnk\/,g qelectrotech.pro
qmake PREFIX=/usr QMAKE_LIBS="$SLKLDFLAGS" QMAKE_CFLAGS="$SLKCFLAGS" QMAKE_CXXFLAGS="$SLKCFLAGS" -o Makefile qelectrotech.pro

# Build and install:
LDFLAGS="$SLKLDFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
make -j "${JOBS}" 
INSTALL_ROOT=$PKG make install

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

mkdir -p $PKG/install
cat > $PKG/install/slack-desc <<EOF
# 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--------------------------------------------------|
qelectrotech: QelectroTech (Qt4 electric diagram editor)
qelectrotech:
qelectrotech: QElectroTech is a Qt4 application to design electric diagrams. It
qelectrotech: uses  XML  files for  elements and  diagrams, and includes both a
qelectrotech: diagram editor and an element editor.
qelectrotech:
qelectrotech:
qelectrotech:
qelectrotech:
qelectrotech:
qelectrotech:
EOF
cat > $PKG/install/doinst.sh <<EOF
if [ -x /usr/bin/update-mime-database ]; then
  /usr/bin/update-mime-database /usr/share/mime &> /dev/null
fi

if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database &> /dev/null
fi

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

# set file permissions
find "$PKG" -type d -print0 | xargs -0 chmod 755
find "$PKG" -type f -print0 | xargs -0 chmod 644
chmod ugo+x $PKG/install/doinst.sh $PKG/usr/bin/qelectrotech

mkdir -p $PKG/usr/doc/$NAME-$VERSION/SlackBuild
cp -a CREDIT ChangeLog ELEMENTS.LICENSE INSTALL LICENSE README $PKG/usr/doc/$NAME-$VERSION
cat $PKG/install/slack-desc > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/slack-desc
cat $THIS_SLACKBUILD > $PKG/usr/doc/$NAME-$VERSION/SlackBuild/$NAME.SlackBuild

cd $PKG
#requiredbuilder -v -y -s $CWD $PKG
su -c "chown -R root:root ${PKG}; /sbin/makepkg --linkadd n --chown n $CWD/$NAME-$VERSION-$ARCH-$BUILD.txz"

if [ "$1" = "--cleanup" ]; then
  rm -rf /tmp/txz
fi
