#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

PKG=$TMP/package-ftchmail

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/_ftchmail.tar.gz

# Find the size of a file:
filesize() {
 SIZE=`ls -l -d -G $1 | cut -b23-32`
 echo -n $SIZE
}

echo "+=================+"
echo "| fetchmail-5.1.2 |"
echo "+=================+"
cd $TMP
tar xzvf $CWD/fetchmail-5.1.2.tar.gz
cd fetchmail-5.1.2
./configure --prefix=/usr --enable-nls i386-slackware-linux
make CFLAGS=-O2 LDFLAGS=-s
strip fetchmail
cat fetchmail > $PKG/usr/bin/fetchmail
#cat fetchmailconf > $PKG/usr/bin/fetchmailconf
cat fetchmail.man | gzip -9c > $PKG/usr/man/man1/fetchmail.1.gz
mkdir -p $PKG/usr/doc/fetchmail-5.1.2
cp -a ABOUT-NLS COPYING FAQ FEATURES INSTALL MANIFEST NEWS NOTES README \
  *.html contrib fetchmail.lsm fetchmailconf \
  sample.rcfile $PKG/usr/doc/fetchmail-5.1.2
chown -R root.root $PKG/usr/doc/fetchmail-5.1.2
cd po
for locale in cs es fr pl pt_BR ru ; do
  mkdir -p $PKG/usr/share/locale/$locale/LC_MESSAGES
  cat $locale.gmo > $PKG/usr/share/locale/$locale/LC_MESSAGES/fetchmail.mo
done

# Build the package:
cd $PKG
tar czvf $TMP/ftchmail.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/fetchmail-5.1.2
 rm -rf $PKG
fi
