#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
# We want to build sendmail on top of this later.
PKG=$TMP/package-sendmail

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

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

echo "+===============+"
echo "| procmail-3.14 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/procmail-3.14.tar.gz
cd procmail-3.14
make
cd src
strip formail lockfile procmail
cat formail > $PKG/usr/bin/formail
cat lockfile > $PKG/usr/bin/lockfile
cat mailstat > $PKG/usr/bin/mailstat
cat procmail > $PKG/usr/bin/procmail
cd ../man
for file in formail.1 lockfile.1 procmail.1 ; do
  gzip -9c $file > $PKG/usr/man/man1/$file.gz
done
for file in procmailex.5 procmailrc.5 procmailsc.5 ; do
  gzip -9c $file > $PKG/usr/man/man5/$file.gz
done
mkdir -p $PKG/usr/doc/procmail-3.14
cd ..
cp -a Artistic COPYING FAQ FEATURES HISTORY INSTALL KNOWN_BUGS \
  README examples $PKG/usr/doc/procmail-3.14
chown -R root.root $PKG/usr/doc
find $PKG/usr/doc/procmail-3.14 -type d -exec chown 755 {} \;

# Build the package:
cd $PKG
tar czvf $TMP/procmail.tgz .

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

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