#!/bin/sh
# Heavily based on the Slackware 12.2 SlackBuild
# http://it.software.canon-europe.com
# Packager Michelino Chionchio - www.slacky.eu
# Depends on: cndrvcups-common

CWD=`pwd`
TMP=${TMP:-/tmp/tgz}
PKG=$TMP/package-cndrvcups-lb
NAME=cndrvcups-lb
VERSION=1.80
TARGET=i486
ARCH=${ARCH:-i486}
BUILD=1mch

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O3 -march=i686 -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "athlon64" ]; then
  SLKCFLAGS="-O2 -march=athlon64 -pipe"
elif [ "$ARCH" = "athlonxp" ]; then
  SLKCFLAGS="-O3 -march=athlon-xp -pipe -fomit-frame-pointer"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

cd $TMP
tar xvzf $CWD/$NAME-$VERSION-1.tar.gz
cd $NAME-$VERSION

cd ppd/
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./autogen.sh \
    --prefix=/usr \
    --program-prefix="" \
    --program-suffix="" \
    --build=$TARGET-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1 


cd ../pstoufr2cpca/
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./autogen.sh \
    --prefix=/usr \
    --enable-progpath=/usr/bin \
    --enable-shared \
    --disable-static \
    --program-prefix="" \
    --program-suffix="" \
    --build=$TARGET-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1

cd ..

mkdir -p $PKG/usr/lib
install -c libs/libcanonufr2.la $PKG/usr/lib
install -c -s libs/libcanonufr2.so.1.0.0 $PKG/usr/lib
install -c -s libs/libufr2filter.so.1.0.0 $PKG/usr/lib

( 
cd $PKG/usr/lib
ln -sf libcanonufr2.so.1.0.0  liblibcanonufr2.so.1
ln -sf libcanonufr2.so.1.0.0  libcanonufr2.so
ln -sf libufr2filter.so.1.0.0 libufr2filter.so.1
ln -sf libufr2filter.so.1.0.0  libufr2filter.so
)

mkdir -p $PKG/usr/share/caepcm
install -c -m 644 data/C*   $PKG/usr/share/caepcm

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a COPYING LICENSE* README* \
$PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/usr/doc/$NAME-$VERSION/ppd
cp -a ./ppd/{AUTHORS,ChangeLog,NEWS,README} \
$PKG/usr/doc/$NAME-$VERSION/ppd

mkdir -p $PKG/usr/doc/$NAME-$VERSION/pstoufr2cpca
cp -a ./pstoufr2cpca/{AUTHORS,ChangeLog,LICENSE*,NEWS,README} \
$PKG/usr/doc/$NAME-$VERSION/pstoufr2cpca


( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

cd $CWD
cp -a *.SlackBuild slack-desc $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
chown -R root:root .
ADD="cndrvcups-common >= 1.60-i486-1mch" \
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-$VERSION-$ARCH-$BUILD.tgz

if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP
fi
