#!/bin/sh
# zfs-fuse:
#   Sun's ZFS ported to the FUSE framework.

# This package requires 'scons' to be installed on your build host.
scons -v >/dev/null || exit 1

test -n "${DEBUG}" && set -x
set -o pipefail
set -e


### Set some initial variables
PKGNAME=zfs-fuse
VERSION=0.6.0git
ARCH=${ARCH:-$(uname -m)}
REVISION=${REVISION:-0}

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=${TMP}/package-${PKGNAME}
OUTPUT=${OUTPUT:-${TMP}}

CPUJOBS=${CPUJOBS:-1}
if [ -z "${SLKCFLAGS}" ]; then
   if   [ "${ARCH}" = "i386" ]; then
      SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
   elif echo "${ARCH}" | grep -q "i[456]86" ; then
      SLKCFLAGS="-O2 -march=i486 -mtune=i686"
   else
      SLKCFLAGS="-O2"
   fi
fi

# Tunable for ZFS thread count.  (default=40)
# If this is set, modify the FUSE listener to
# spawn more or fewer threads on launch.
# This may reduce processor starvation for other
# apps at the expense of filesystem throughput.
# ZFSTHREADS=



### Housekeeping
# Sanitize the build site
rm -rf "${PKG}"
mkdir -p "${TMP}"
cp -a "${CWD}/files/" "${PKG}" 2>/dev/null || \
   mkdir -p "${PKG}"


# Unpack the source archive
cd "${TMP}"
rm -rf "${PKGNAME}-${VERSION}"
# Try the local git repo first, then fall back on an archive of some type.
# (hint:  The tarball is a full git clone,
#  unpack it and use "git pull" to begin building upstream changes.  :-)
if [ -d "${CWD}/${PKGNAME}-${VERSION}" ]; then
   cp -a "${CWD}/${PKGNAME}-${VERSION}" .
else
   tar -xvf "${CWD}/${PKGNAME}-${VERSION}".tar.*z?
fi

# Fix permissions
cd "${PKGNAME}-${VERSION}"
chown -R 0:0 .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;



### Patch
# If requested, cause zfs-fuse to spawn fewer/more threads.
if [ -n "${ZFSTHREADS}" ]; then
   sed -i "s/#define NUM_THREADS.*/#define NUM_THREADS ${ZFSTHREADS}/" \
      src/zfs-fuse/fuse_listener.c
fi



### Build
## Configure
# Not required

## Compile
# SLKCFLAGS seem to be ignored.  Hmm.
scons -C src/ debug=0 -j"${CPUJOBS}"

## Install
# We will put this in /sbin just in case you want to
# mount other toplevel filesystems before /usr is ready.
scons -C src/ debug=0 install install_dir="${PKG}/sbin/"



### Fix-ups
# Add an rc script.
# It is up to the user to add the hook to their other rc scripts.
mkdir -p "${PKG}/etc/rc.d/"
cp -a "${CWD}/rc.zfs-fuse" "${PKG}/etc/rc.d/"



### Documentation
## man pages
mkdir -p "${PKG}/usr/man/man8/"
cp -a doc/*.8* "${PKG}/usr/man/man8/"
# These pages come pre-compressed,
# but add a compression loop in case upstream ever changes.
for PAGE in $(ls "${PKG}"/usr/man/man8/*.8 2>/dev/null) ; do
   gzip -9 "${PAGE}"
done

## User docs
mkdir -p "${PKG}/usr/doc/${PKGNAME}-${VERSION}/"
cp -a \
   BUGS \
   HACKING \
   LICENSE \
   README.NFS \
   TESTING \
   CHANGES \
   INSTALL \
   README \
   STATUS \
   TODO \
   doc/caching.dia \
   "${PKG}/usr/doc/${PKGNAME}-${VERSION}/"




### Build the package:
cd ${PKG}
mkdir -p ${PKG}/install
cat ${CWD}/slack-desc > ${PKG}/install/slack-desc
test -r ${CWD}/doinst.sh && cat ${CWD}/doinst.sh > ${PKG}/install/doinst.sh
makepkg -c n -l y ${OUTPUT}/${PKGNAME}-${VERSION}-${ARCH}-${REVISION}.txz || \
   makepkg -c n -l y ${OUTPUT}/${PKGNAME}-${VERSION}-${ARCH}-${REVISION}.tgz



### Clean up the extra stuff:
if [ "${1}" = "--cleanup" ]; then
   rm -rf ${TMP}/${PKGNAME}-${VERSION}
   rm -rf ${PKG}
fi
