#!/bin/sh
# Set initial variables:
CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-logrotate

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

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG # place for the package to be built

# Explode the package framework:
cd $PKG
explodepkg $CWD/_logrotate.tar.gz

cd $TMP
rm -rf logrotate-$VERSION
tar xzvf $CWD/logrotate-$VERSION.tar.gz
cd logrotate-$VERSION
chown -R root:root .
find . -perm 666 -exec chmod 644 {} \;
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 {} \;
zcat $CWD/logrotate.slackware.diff.gz | patch -p1
make clean
make || exit 1
strip logrotate
cat logrotate > $PKG/usr/sbin/logrotate
cat logrotate.8 | gzip -9c > $PKG/usr/man/man8/logrotate.8.gz
mkdir -p $PKG/usr/doc/logrotate-$VERSION
cp -a \
  CHANGES COPYING READM* \
  $PKG/usr/doc/logrotate-$VERSION
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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

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