#!/bin/sh

# Slackware build script for openct

# Copyright 2008-2013  LukenShiro, 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.

PRGNAM=openct
VERSION=${VERSION:-0.6.20}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}

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

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

APIDOC=${APIDOC:-0}	# api documentation not built by default
NONPRIV=${NONPRIV:-0}	# disable not-privileged mode (it will be used by root)

DOCFILES="NEWS TODO LGPL-2.1"

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-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
 -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# building API doc
case "$APIDOC" in
    0)	APIDOC_FLAG="dis"
	;;
    *)	APIDOC_FLAG="en"
	;;
esac

# daemon user/group seem not to be really used unless for /var/run/openct,
# so set them to a saner 'root:root' or 'root:plugdev' (instead of upstream's
# 'openctd:usb'): for now it's unnecessary to add other user/group.
# If NONPRIV is enabled you'll have to verify /etc/rc.d/rc.openctd accordingly.
case "$NONPRIV" in
    0)	NONPRIV_FLAGS="--with-daemon-groups=root \
    --disable-non-privileged"	# root-only
	;;
    *)	NONPRIV_FLAGS="--with-daemon-groups=plugdev \
    --enable-non-privileged"	# non privileged mode
	;;
esac

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --localstatedir=/var \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --datarootdir=/usr/share \
  --mandir=/usr/man \
  --enable-usb \
  --enable-pcsc \
  --enable-doc \
  --with-bundle=/usr/lib${LIBDIRSUFFIX}/pcsc/drivers \
  --${APIDOC_FLAG}able-api-doc \
  --with-ifddir=/usr/lib${LIBDIRSUFFIX}/ifd \
  --disable-static \
  --with-udev=/lib/udev \
  --with-daemon-user=root \
  $NONPRIV_FLAGS \
  --build=$ARCH-slackware-linux

make
make install DESTDIR=$PKG

# copy Slackware-compliant openct daemon (instead of original 'init-script' file)
mkdir -p $PKG/etc/rc.d
cat $CWD/rc.openctd > $PKG/etc/rc.d/rc.openctd.new
chmod 0755 $PKG/etc/rc.d/rc.openctd.new
case "$NONPRIV" in
    0)  true	# do nothing
        ;;
    *)  # enable non-privileged in rc.openctd
        sed -i "s|#chown root:plugdev|chown root:plugdev|g" $PKG/etc/rc.d/rc.openctd.new
        sed -i "s|#chmod 750|chmod 750|g" $PKG/etc/rc.d/rc.openctd.new
        ;;
esac

# Don't clobber the config file
mv $PKG/etc/openct.conf $PKG/etc/openct.conf.new

# Used for serial devices within pcsc-lite
# entries decommented by default (as they could tamper with pcsc-lite's device
# detection) renamed to avoid pcsc-lite's reader.conf overwriting
mkdir -p $PKG/etc/reader.conf.d
cp -a etc/reader.conf $PKG/etc/reader.conf.d/reader-openct.conf.new
sed -i "s|^FRIENDLYNAME|#FRIENDLYNAME|g" $PKG/etc/reader.conf.d/reader-openct.conf.new
sed -i "s|^DEVICENAME|#DEVICENAME|g" $PKG/etc/reader.conf.d/reader-openct.conf.new
sed -i "s|^LIBPATH|#LIBPATH|g" $PKG/etc/reader.conf.d/reader-openct.conf.new
sed -i "s|^CHANNELID|#CHANNELID|g" $PKG/etc/reader.conf.d/reader-openct.conf.new

# Hal detection is now deprecated by upstream, so we use udev detection.
# Install udev scripts for openct
mkdir -p $PKG/lib/udev
install -m755 etc/openct_usb $PKG/lib/udev/openct_usb
install -m755 etc/openct_pcmcia $PKG/lib/udev/openct_pcmcia
install -m755 etc/openct_serial $PKG/lib/udev/openct_serial

# Install udev rules for openct
mkdir -p $PKG/lib/udev/rules.d
cp -a etc/openct.udev $PKG/lib/udev/rules.d/95-openct.rules
# modification taken to prevent bug (as happened in other distributions:
# i.e. https://bugzilla.redhat.com/287871):
# it seems 'sleep' program would otherwise be applied to every usb add
# event, not only to this one
sed -i "s|^PROGRAM=|#PROGRAM=|g" $PKG/lib/udev/rules.d/95-openct.rules

# Directory for status, socket and lock files
mkdir -p $PKG/var/run/openct
case "$NONPRIV" in
    0)	USR_GRP="root:root"		# root-only
	;;
    *)	USR_GRP="root:plugdev"		# non privileged mode
	;;
esac
chown $USR_GRP $PKG/var/run/openct

# Make directory for openct modules (not bundled with openct)
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/ifd

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

find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $(find . -type l) ; do ln -s $(readlink $i).gz $i.gz ; rm $i ; done

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION/
cp -a $DOCFILES $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
cat $CWD/README.SLACKWARE > $PKG/usr/doc/$PRGNAM-$VERSION/README.SLACKWARE
[ "$APIDOC" = "0" ] && rm -rf $PKG/usr/doc/$PRGNAM-$VERSION/api

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:-tgz}
