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

PKG1=$TMP/package-pine
PKG2=$TMP/package-imapd

VERSION=4.44
ARCH=i386
BUILD=2

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

# Explode the package framework:
cd $PKG1
explodepkg $CWD/_pine.tar.gz
cd $PKG2
explodepkg $CWD/_imapd.tar.gz

echo "+==========+"
echo "| pine$VERSION |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/pine$VERSION.tar.gz
cd pine$VERSION
zcat $CWD/pine-slackware.config.diff.gz | patch -p1 --backup --verbose -E
zcat $CWD/pine-cflags.diff.gz | patch -p1 --backup --verbose -E
# Remove ~ files:
find . -name "*~" | xargs rm --verbose
mkdir $PKG1/usr/doc/pine$VERSION
cp -a README CPYRIGHT $PKG1/usr/doc/pine$VERSION
( cd doc ; cp -a * $PKG1/usr/doc/pine$VERSION )
( cd $PKG1/usr/doc/pine$VERSION ; rm *.1 tech-notes.txt )
( cd $PKG1/usr/doc/pine$VERSION ; chown -R root.root * )
mkdir -p $PKG2/usr/doc/pine$VERSION
cp -a README CPYRIGHT $PKG2/usr/doc/pine$VERSION
chown root.root $PKG2/usr/doc/pine$VERSION/*
./build slx SSLLIB=/usr/lib SSLDIR=/etc/ssl SSLCERTS=/etc/ssl/certs SSLINCLUDE=/usr/include/openssl
#( cd imap ; make clean )
#( cd imap ; make slx )
for file in doc/pico.1 doc/pilot.1 doc/pine.1 ; do
  cat $file | gzip -9c > $PKG1/usr/man/man1/`basename $file`.gz
done
for file in imap/src/imapd/imapd.8c imap/src/ipopd/ipopd.8c ; do
  cat $file | gzip -9c > $PKG2/usr/man/man8/`basename $file`.gz
done
cd imap/imapd
strip imapd
cat imapd > $PKG2/usr/sbin/imapd
cd ../ipopd
strip ipop3d
cat ipop3d > $PKG2/usr/sbin/ipop3d
cd ../../bin
strip *
cat pico > $PKG1/usr/bin/pico
cat pilot > $PKG1/usr/bin/pilot
cat pine > $PKG1/usr/bin/pine
# Add a default system-wide config file:
$PKG1/usr/bin/pine -conf > $PKG1/usr/lib/pine/pine.conf

# Add slack-desc files:
mkdir -p $PKG1/install
cat $CWD/slack-desc.pine > $PKG1/install/slack-desc
mkdir -p $PKG2/install
cat $CWD/slack-desc.imapd > $PKG2/install/slack-desc

# Build the package:
cd $PKG1
echo "n" | makepkg $TMP/pine-$VERSION-$ARCH-$BUILD.tgz
cd $PKG2
echo "n" | makepkg $TMP/imapd-$VERSION-$ARCH-$BUILD.tgz

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