#!/bin/sh
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-sane

VERSION=1.0.18
BACKVER=1.0.18
FRONTVER=1.0.14
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=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
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 \
  --mandir=/usr/man \
  --disable-locking \
  --localstatedir=/var/lib \
  --with-group=scanner
make -j3 || exit 1
make install || exit 1
make install DESTDIR=$PKG || exit 1
# Add the default udev rules:
mkdir -p $PKG/etc/udev/rules.d
cp -a tools/udev/libsane.rules $PKG/etc/udev/rules.d
chown root:root $PKG/etc/udev/rules.d/libsane.rules
chmod 644 $PKG/etc/udev/rules.d/libsane.rules
# 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 
rm -rf sane-frontends-$FRONTVER
tar xjvf $CWD/sane-frontends-$FRONTVER.tar.bz2
cd sane-frontends-$FRONTVER
chown -R root:root .
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --mandir=/usr/man
make -j3 || exit 1
make install || exit 1
make install DESTDIR=$PKG || exit 1

( 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
)

# List additional backends in /etc/sane.d/dll.conf.
# I don't think it will hurt anything to do this, even
# if these backends turn out not to be available:
zcat $CWD/dll.conf.additions.gz >> $PKG/etc/sane.d/dll.conf

# Move config files:
( cd $PKG/etc/sane.d
  for file in *.conf ; do
    mv $file ${file}.new
  done
)
rm -f /etc/sane.d/*.conf

# Compress and if needed symlink the man pages:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

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