#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-sendmail
SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

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

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+=================+"
echo "| sendmail.8.6.12 |"
echo "+=================+"
cd $TMP
mkdir sendmail
cd sendmail
tar xzvf $CWD/sendmail.8.6.12.base.tar.gz
tar xzvf $CWD/sendmail.8.6.12.cf.tar.gz
tar xzvf $CWD/sendmail.8.6.12.misc.tar.gz
zcat $CWD/sendmail.linux.diff.gz | patch
zcat $CWD/sendmail.linux.diff-2.gz | patch
make -f Makefile.Linux
cd src
strip sendmail
cat sendmail > $PKG/usr/sbin/sendmail
man2gz sendmail.8 $PKG/usr/man/preformat/cat8/sendmail.8.gz $SRC/usr/man/man8/sendmail.8
cd ../makemap
strip makemap
cat makemap > $PKG/usr/sbin/makemap
man2gz makemap.8 $PKG/usr/man/preformat/cat8/makemap.8.gz $SRC/usr/man/man8/makemap.8
cd ../mailstats
strip mailstats
cat mailstats > $PKG/usr/sbin/mailstats
cd ../praliases
strip praliases
cat praliases > $PKG/usr/bin/praliases
cd ../rmail
strip rmail
cat rmail > $PKG/usr/bin/rmail
man2gz rmail.8 $PKG/usr/man/preformat/cat8/rmail.8.gz $SRC/usr/man/man8/rmail.8
cat $TMP/deliver/deliver > $PKG/usr/bin/deliver
cat $TMP/deliver/header > $PKG/usr/bin/header
man2gz $TMP/deliver/deliver.8 $PKG/usr/man/preformat/cat8/deliver.8.gz $SRC/usr/man/man8/deliver.8

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

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