#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-sane

VERSION=1.0.16
BACKVER=1.0.16
FRONTVER=1.0.13
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

rm -rf $PKG
mkdir -p $PKG
cd $TMP
rm -rf sane-backends-$BACKVER sane-frontends-$FRONTVER
tar xjvf $CWD/sane-backends-$BACKVER.tar.bz2
cd sane-backends-$BACKVER
chown -R root.root .
# locking seems not to be working right now (grep -r -l LCK * finds nothing),
# so it will be disabled to play it safe.
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --disable-locking \
  --localstatedir=/var/lib \
  --with-group=scanner
make
make install
make install DESTDIR=$PKG
# This will be needed later when locking works:
mkdir -p $PKG/var/lib/lock/sane
chown root:scanner $PKG/var/lib/lock/sane
chmod 775 $PKG/var/lib/lock/sane
# Add hotplug support for USB scanners:
cp -a tools/hotplug/README $PKG/usr/doc/sane-$BACKVER/README.hotplug
mkdir -p $PKG/etc/hotplug/usb
cp -a tools/hotplug/libsane.usermap tools/hotplug/libusbscanner $PKG/etc/hotplug/usb
chmod 644 $PKG/etc/hotplug/usb/libsane.usermap
chmod 755 $PKG/etc/hotplug/usb/libusbscanner

cd $TMP 
tar xjvf $CWD/sane-frontends-$FRONTVER.tar.bz2
cd sane-frontends-$FRONTVER
chown -R root.root .
CFLAGS="$SLKCFLAGS" \
./configure --prefix=/usr --sysconfdir=/etc
make
make install
make install DESTDIR=$PKG
# Move config files:
( cd $PKG/etc/sane.d
  for file in *.conf ; do
    mv $file ${file}.new
  done
)
rm -f /etc/sane.d/*.conf
gzip -9 $PKG/usr/man/man?/*.?
chown -R root:bin $PKG/usr/bin $PKG/usr/sbin
( 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
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

cd $PKG
makepkg -l y -c n $TMP/sane-$VERSION-$ARCH-$BUILD.tgz

