#!/bin/sh

# Slackware build script for ZFS-Fuse
# Steven King revision date 2010/01/17
# Updated for 0.7.0 by rworkman on 20110403
# Updated for 20120201_6abfdcf by ponce on 20120923

PRGNAM=zfs-fuse
VERSION=20120201_6abfdcf
BUILD=${BUILD:-1}
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}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
rm -rf $TMP/$PRGNAM-$VERSION
cd $TMP
tar xvf $CWD/$PRGNAM-$VERSION.tar.?z*
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 {} \;

# Patch for glib>=2.14
# http://zfs-fuse.net/issues/137
patch -p1 < $CWD/glibc-2.14.patch

cd src
  scons
  scons install install_dir=$PKG/usr/sbin
cd ..

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

# Install init script and zfsrc sample
mkdir -p $PKG/etc/rc.d
cat $CWD/rc.zfs-fuse > $PKG/etc/rc.d/rc.zfs-fuse.new
cat contrib/zfsrc > $PKG/etc/zfsrc.sample

mkdir -p $PKG/usr/man/man8
cp doc/*.8 $PKG/usr/man/man8
gzip -9 $PKG/usr/man/man8/*.8

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  BUGS CHANGES HACKING INSTALL LICENSE README* STATUS TESTING TODO \
  $PKG/usr/doc/$PRGNAM-$VERSION
find $PKG/usr/doc/$PRGNAM-$VERSION -type f -exec chmod 644 {} \;
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

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}
