#!/bin/bash

# sane.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 13-March-2005

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

################## Build sane-backends ####################

# Extract source:
tar jxvvf $CWD/sane-backends-*.tar.bz2
cd sane-backends-*
slackhousekeeping

# 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 \
   --mandir=/usr/man \
   --sysconfdir=/etc \
   --disable-locking \
   --localstatedir=/var/lib \
   --with-group=scanner || failconfig

# Build:
make || failmake

# Install onto filesystem so that it can be picked up by sane frontends
# when we build it next.
make install

# Install into the package:
make install DESTDIR=$PKG
# 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

################## Build sane-frontends ####################

# Extract source:
cd $TMPBUILD
tar jxvvf $CWD/sane-frontends-*.tar.bz2
cd sane-frontends-*
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --sysconfdir=/etc || failconfig

# Build:
make || failmake

# Install onto file system (is this necessary?)
make install

# Install into package:
make install DESTDIR=$PKG

# 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

######################################################

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
