#!/bin/sh
# Heavily based on the Slackware 13.0 SlackBuild
# http://www.arg0.net/encfs
# http://www.slacky.eu
# Packager Kc
# Set initial variables:
#
# Required rlog

CWD=$(pwd)
TMP=${TMP:-/tmp/txz}
PKGNAM=encfs
VERSION=${VERSION:-1.5}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-3slacky}
SRCPACK=${PKGNAM}-$VERSION-2.tgz
PKG=$TMP/package-${PKGNAM}
SRC=$TMP/source-${PKGNAM}
SOURCE=http://encfs.googlecode.com/files/${PKGNAM}-$VERSION-2.tgz

if [ ! -e ${PKGNAM}-$VERSION-2.tgz ]; then
wget -c $SOURCE
fi


if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
fi

mkdir -p $TMP
rm -rf $PKG
mkdir -p $PKG
rm -rf $SRC
mkdir -p $SRC
cd $SRC || exit 1
tar vxzf $CWD/${SRCPACK}

##################################################
cd $(ls) || exit 1 # SOURCE TREE
##################################################

chown -R root:root .
find . -perm +111 -exec chmod 755 {} \;
find . ! -perm +111 -exec chmod 644 {} \;

LDFLAGS="$SLKLDFLAGS" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure 	--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--libdir=/usr/lib${LIBDIRSUFFIX} \
		--mandir=/usr/man \
		--docdir=/usr/doc/$NAME-$VERSION \
		--disable-static \
		--program-prefix= \
		--program-suffix= \
		--build=$ARCH-slackware-linux
make || exit 1
make install DESTDIR=$PKG || exit 1


# Add documentation:
mkdir -p $PKG/usr/doc/${PKGNAM}-$VERSION/SlackBuild
cp -a \
  ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL README encfs/encfs-man.html \
  $PKG/usr/doc/${PKGNAM}-$VERSION

##################################################
cd $PKG || exit 1 # PACKAGE TREE
##################################################

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


# Add install files:
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/${PKGNAM}-$VERSION/SlackBuild/slack-desc
cat $CWD/${PKGNAM}.SlackBuild > $PKG/usr/doc/${PKGNAM}-$VERSION/SlackBuild/${PKGNAM}.SlackBuild


# List files with unusual perm, user, group:
find . -type d ! -perm 755 -exec ls -ld {} \;
find . ! -type d ! -type l -perm +6022 -exec ls -ld {} \;
find . ! -user root -exec ls -ld {} \;
find . ! -group root ! -group bin -exec ls -ld {} \;


gzip -9 $PKG/usr/man/*/*


# Build the package:
requiredbuilder -v -y -s $CWD $PKG
makepkg -l y -c n $CWD/${PKGNAM}-$VERSION-$ARCH-$BUILD.txz

# Clean up the extra stuff:  
[ "$1" = "--cleanup" ] && rm -rf $SRC $PKG
