#!/bin/sh
# Set initial variables:
CWD=`pwd`

VERSION=1.1.11
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}

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

if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
PKG=$TMP/package-jfsutils
rm -rf $PKG
mkdir -p $PKG
cd $TMP
rm -rf jfsutils-$VERSION
tar xzvf $CWD/jfsutils-$VERSION.tar.gz
cd jfsutils-$VERSION
chown -R root:root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 665 -exec chmod 644 {} \;
find . -perm 664 -exec chmod 644 {} \;
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --disable-dependency-tracking \
  $ARCH-slackware-linux
make
mkdir -p $PKG/sbin
cat fsck/jfs_fsck > $PKG/sbin/jfs_fsck
( cd $PKG/sbin ; ln -sf jfs_fsck fsck.jfs )
cat mkfs/jfs_mkfs > $PKG/sbin/jfs_mkfs
( cd $PKG/sbin ; ln -sf jfs_mkfs mkfs.jfs )
cat logdump/jfs_logdump > $PKG/sbin/jfs_logdump
cat fscklog/jfs_fscklog > $PKG/sbin/jfs_fscklog
cat xpeek/jfs_debugfs > $PKG/sbin/jfs_debugfs
cat tune/jfs_tune > $PKG/sbin/jfs_tune
chmod 755 $PKG/sbin/*
mkdir -p $PKG/usr/man/man8
for file in `find . -name "*.8"` ; do
  cat $file | gzip -9c > $PKG/usr/man/man8/`basename $file`.gz
done
( cd $PKG/usr/man/man8
  ln -sf jfs_fsck.8.gz fsck.jfs.8.gz
  ln -sf jfs_mkfs.8.gz mkfs.jfs.8.gz
)
mkdir -p $PKG/usr/doc/jfsutils-$VERSION
cp -a \
  AUTHORS COPYING ChangeLog NEWS README \
  $PKG/usr/doc/jfsutils-$VERSION
( 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
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/jfsutils-$VERSION-$ARCH-$BUILD.tgz

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