#!/bin/bash
#
#  wiringpi.SlackBuild 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 any later version.
#
#  wiringpi.SlackBuild is distributed in the hope that it will be useful, but WITHOUT ANY
#  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
#  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along with
#  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
#  Place - Suite 330, Boston, MA 02111-1307, USA
#
#  Note: at https://github.com/WiringPi/WiringPi is a development after 2.50, not
#  by Gordon but a comunity. This version cannot build on Slackware ARM. The code
#  probably only works on Debian.

# default settings
PACKAGE="wiringpi"
PACKAGE_EXT="gz"
VERSION=${VERSION:=2.50}
BUILD=${BUILD:=1}

if [ -f ~/.slackbuildrc ]; then
  . ~/.slackbuildrc
elif [ -f ../../runtime/slackbuildrc ]; then
  . ../../runtime/slackbuildrc
elif [ -f ../../source/runtime/slackbuildrc ]; then
  . ../../source/runtime/slackbuildrc
fi

check_required

SRC="$PACKAGE-$VERSION.tar.$PACKAGE_EXT"
unpack_source $SRC $PACKAGE-$VERSION


mkdir -p $PKG/lib

# Don't use the global build script.
# Library
cd wiringPi
  make DESTDIR="" PREFIX="/usr" || exit 1
  make install DESTDIR=$PKG PREFIX="/usr"
cd ..

cd devLib
  make DESTDIR=$PKG PREFIX="/usr" INCLUDE="-I. -I $PKG/usr/include" || exit 1
  make install DESTDIR=$PKG PREFIX="/usr"
cd ..

# Symlink the libraries
cd $PKG/usr/lib
  ln -s libwiringPi.so.${VERSION} libwiringPi.so
  ln -s libwiringPiDev.so.${VERSION} libwiringPiDev.so
cd -

cd gpio
  make DESTDIR="/tmp/package-wiringpi" PREFIX="/usr" || exit 1
  mkdir -p $PKG/usr/bin $PKG/usr/man/man1
  cp gpio $PKG/usr/bin
  chown root:root $PKG/usr/bin/gpio
  chmod 755 $PKG/usr/bin/gpio
  cp gpio.1 $PKG/usr/man/man1
cd ..

# Fix other links
cd $PKG/lib
  rm -f libwiringPi.so
  ln -s ../usr/lib/libwiringPi.so.${VERSION} libwiringPi.so
  rm -f libwiringPiDev.so
  ln -s ../usr/lib/libwiringPiDev.so.${VERSION} libwiringPiDev.so
cd -


mkdir -p $DOCS
cp -a COPYING.LESSER INSTALL People README.TXT $DOCS


fixup_package $PKG

# Clean up the extra stuff:
set +u
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/$PACKAGE-$VERSION
  rm -rf $PKG
fi