#!/bin/bash

# pine.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 15-Mar-2004

PINEBUILD=2
IMAPDBUILD=2
PINEPGP=0.18.0

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PACKAGE
export PORTCWD=$PWD


# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) SLKCFLAGS="-O2" ;;
   sparc)   SLKCFLAGS="-O2" ;;
   *)       SLKCFLAGS="" ;;
esac

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
export PKG2=$TMP/package-imapd
mkpkgdirs # Delete and re-create temporary directories
rm -rf $PKG2 ; mkdir -p $PKG2

# Start fakeroot server:
start_fakeroot

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

# Build PINE:
cd $TMPBUILD
tar jxvvf $CWD/pine$VERSION.tar.bz2
cd pine$VERSION
zcat $CWD/pine-slackware.config.diff.gz | patch -p1 --backup --verbose -E || failpatch

# Set default debug level to 0:
zcat $CWD/pine.debug.diff.gz | patch -p1 --backup --verbose -E || failpatch
echo "#define PASSFILE        \".pine.pwd\"" >> pine/osdep/os-lnx.h

# 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/imap$VERSION
cp -a README CPYRIGHT imap/docs/md5.txt \
      $PKG2/usr/doc/imap$VERSION
cat << EOF > $PKG2/usr/doc/imap$VERSION/additional-imap-documentation
Additional documentation for imapd may be found in the pine
sources in the /imap/docs directory.
EOF

# Compile PINE:
./build slx SSLLIB=/usr/lib \
            SSLDIR=/etc/ssl \
            SSLCERTS=/etc/ssl/certs \
            SSLINCLUDE=/usr/include/openssl \
            SSLTYPE=unix \
            OPTIMIZE="$SLKCFLAGS" \
            DEBUG="$SLKCFLAGS" \
            'LDAPLIBS=-lldap -llber' \
            'LDAPCFLAGS=-DENABLE_LDAP' || failmake

# Install man pages:
install -m644 doc/pico.1 doc/pilot.1 doc/pine.1 \
              $PKG/usr/man/man1
install -m644 imap/src/imapd/imapd.8 imap/src/ipopd/ipopd.8 \
              $PKG2/usr/man/man8

# Install the IMAP bits into the imap package:
cd imap/imapd
install -m755 imapd $PKG2/usr/sbin
cd ../ipopd
install -m755 ipop3d $PKG2/usr/sbin
cd ../../bin

# Install PINE binaries into package:
install -m755 pico pilot pine $PKG/usr/bin
# Add a default system-wide config file:
$PKG/usr/bin/pine -conf > $PKG/etc/pine.conf

# Add slack-desc files:
mkdir -p $PKG/install $PKG2/install
install -m644 $CWD/slack-desc.pine $PKG/install/slack-desc
sed 's/%VER%/'"$VERSION"'/g' $PORTCWD/slack-desc.imapd > $PKG2/install/slack-desc
#install -m644 $CWD/slack-desc.imapd $PKG2/install/slack-desc

# Add pinepgp support:
cd $TMP
tar zxvvf $CWD/pinepgp-$PINEPGP.tar.gz
cd pinepgp-$PINEPGP
slackhousekeeping
./configure \
   --prefix=/usr || failconfig
# The Makefile has some old stuff that make-3.81 isn't compatible with:
make-3.80 || failmake
make-3.80 install DESTDIR=$PKG
mkdir -p $PKG/usr/doc/pinepgp-$PINEPGP
cp -a COPYING README $PKG/usr/doc/pinepgp-$PINEPGP

# Set policies for both packages:
for i in $PKG $PKG2 ; do
  cd $i
  # Slackware policies:
  slackgzpages -i # compress man & info pages and delete usr/info/dir
  slackslack      # set all files to root.root, chmod -R og-w, slackchown, slack644docs
done

# Build packages:
if [ $PORTARCH = arm ]; then
   # Build PINE package:
   ( cd $PKG  && slackmp )      
   # Build IMAP package:
   ( cd $PKG2 && PACKAGE=imapd BUILD=$IMAPDBUILD slackmp )
 else
   cd $PKG  && makepkg -l y -c n $PKGSTORE/$PKGSERIES/pine-$VERSION-$ARCH-$BUILD.tgz
   cd $PKG2 && makepkg -l y -c n $PKGSTORE/$PKGSERIES/imapd-$VERSION-$ARCH-$BUILD.tgz
fi
