#!/bin/bash
#
# got it from ftp://ftp.slackware.com/pub/slackware/slackware-10.2/patches/source/pine/pine.SlackBuild
# small changes by rhatto
#

if [ -s "slack-required" ]; then
  echo Recomended and required packages for building pine are:
  cat slack-required | sed -e 's/^/\t/'
  if [ "$INTERACT" != "no" ]; then
    echo If you dont have those installed, press Ctrl-C. Otherwise, hit ENTER.
    read crap
#  else
#    echo Sleeping 3 seconds...
#    sleep 3
  fi
fi

CWD="`pwd`"

if [ -f ~/.slackbuildrc ]; then
  source ~/.slackbuildrc
elif [ -f /etc/slackbuildrc ]; then
  source /etc/slackbuildrc
fi

PACKAGE="pine"
VERSION=${VERSION:=4.64}
PINEPGP=${PINEPGP:=0.18.0}
ARCH=${ARCH:=i486}
PINEBUILD=${BUILD:=1rha}
IMAPDBUILD=${BUILD:=1rha}

# -------  error codes for createpkg  --------------
ERROR_WGET=31;      ERROR_MAKE=32;      ERROR_INSTALL=33
ERROR_MD5=34;       ERROR_CONF=35;      ERROR_HELP=36
ERROR_TAR=37;       ERROR_MKPKG=38;     ERROR_GPG=39
ERROR_PATCH=40

TMP=${TMP:=/tmp}
SRC_DIR=${SRC:=$CWD}
REPOS=${REPOS:=$TMP}

TMP="$TMP/$PACKAGE"
rm -rf $TMP
mkdir -p $TMP

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

RTOOL="wget"
SRC="pine$VERSION.tar.bz2"
URL="ftp://ftp.slackware.com/pub/slackware/slackware-10.2/patches/source/pine/$SRC"
PGP="pinepgp-$PINEPGP.tar.gz"
PGP_URL="ftp://ftp.slackware.com/pub/slackware/slackware-10.2/patches/source/pine/$PGP"

SRC_DIR="$SRC_DIR/$PACKAGE"
mkdir -p $SRC_DIR

if [ "$RTOOL" == "wget" ]; then

  if [ ! -f "$SRC_DIR/$SRC" ]; then
    wget "$URL" -O "$SRC_DIR/$SRC" || exit $ERROR_WGET
  fi

  if [ ! -f "$SRC_DIR/$PGP" ]; then
    wget "$PGP_URL" -O "$SRC_DIR/$PGP" || exit $ERROR_WGET
  fi

fi

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG1 $PKG2
mkdir -p $PKG1 $PKG2

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

echo "+==========+"
echo "| pine$VERSION |"
echo "+==========+"
cd $TMP
rm -rf pine$VERSION
tar xjvf $SRC_DIR/$SRC
cd pine$VERSION

if [ "$ARCH" == "x86_64" ]; then
 ( cd imap/src/osdep/unix; zcat $CWD/fpic.diff.gz | patch -p0 --verbose -E || exit $ERROR_PATCH )
fi

zcat $CWD/pine-slackware.config.diff.gz | patch -p1 --backup --verbose -E || exit $ERROR_PATCH

# Set default debug level to 0:
zcat $CWD/pine.debug.diff.gz | patch -p1 --backup --verbose -E || exit $ERROR_PATCH
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/pine$VERSION
cp -a README CPYRIGHT $PKG2/usr/doc/pine$VERSION
chown root.root $PKG2/usr/doc/pine$VERSION/*

if [ "$ARCH" == "x86_64" ]; then
  ./build slx SSLLIB=/usr/lib SSLDIR=/etc/ssl SSLCERTS=/etc/ssl/certs SSLINCLUDE=/usr/include/openssl SSLTYPE=unix OPTIMIZE="-O2" DEBUG=-O2 CC="gcc -fPIC"
else
  ./build slx SSLLIB=/usr/lib SSLDIR=/etc/ssl SSLCERTS=/etc/ssl/certs SSLINCLUDE=/usr/include/openssl SSLTYPE=unix OPTIMIZE=-O2 DEBUG=-O2
fi

#( 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.8 imap/src/ipopd/ipopd.8 ; 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/etc/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

# Add pinepgp support:
cd $TMP
tar xzvf $SRC_DIR/pinepgp-$PINEPGP.tar.gz
cd pinepgp-$PINEPGP
chown -R root.root .
./configure --prefix=/usr || exit $ERROR_CONF
make || exit $ERROR_MAKE
make install DESTDIR=$PKG1 || exit $ERROR_INSTALL
chown root.bin $PKG1/usr/bin/*
mkdir -p $PKG1/usr/doc/pinepgp-$PINEPGP
cp -a COPYING README $PKG1/usr/doc/pinepgp-$PINEPGP 
chmod 644 $PKG1/usr/doc/pinepgp-$PINEPGP/*

# Build the package:
cd $PKG1
makepkg -l y -c n $REPOS/pine-$VERSION-$ARCH-$PINEBUILD.tgz || exit $ERROR_MKPKG
cd $PKG2
makepkg -l y -c n $REPOS/imapd-$VERSION-$ARCH-$IMAPDBUILD.tgz || exit $ERROR_MKPKG

# Clean up the extra stuff:
if [ "$CLEANUP" == "yes" ]; then
  rm -rf $TMP
fi