#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-elm
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/_elm.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 "| elm2.4 |"
echo "+========+"
cd $TMP
tar xzvf $CWD/elm2.4.tar.gz
cd elm2.4
zcat $CWD/elm2.4.diff.gz | patch -p1
make
mkdir bin
cd lib
ranlib libutil.a
cd ..
make
cd bin
cat answer > $PKG/usr/bin/answer
cat elm > $PKG/usr/bin/elm
cat elmalias > $PKG/usr/bin/elmalias
cat fastmail > $PKG/usr/bin/fastmail
cat filter > $PKG/usr/bin/filter
cat frm > $PKG/usr/bin/frm
cat newalias > $PKG/usr/bin/newalias
cat newmail > $PKG/usr/bin/newmail
cat readmsg > $PKG/usr/bin/readmsg
cat prlong > $PKG/var/lib/elm/prlong
cd ../doc
man2gz answer.1 $PKG/usr/man/preformat/cat1/answer.1.gz $SRC/usr/man/man1/answer.1
man2gz chkalias.1 $PKG/usr/man/preformat/cat1/chkalias.1.gz $SRC/usr/man/man1/chkalias.1
man2gz elm.1 $PKG/usr/man/preformat/cat1/elm.1.gz $SRC/usr/man/man1/elm.1
man2gz elmalias.1 $PKG/usr/man/preformat/cat1/elmalias.1.gz $SRC/usr/man/man1/elmalias.1
man2gz fastmail.1 $PKG/usr/man/preformat/cat1/fastmail.1.gz $SRC/usr/man/man1/fastmail.1
man2gz filter.1 $PKG/usr/man/preformat/cat1/filter.1.gz $SRC/usr/man/man1/filter.1
man2gz frm.1 $PKG/usr/man/preformat/cat1/frm.1.gz $SRC/usr/man/man1/frm.1
man2gz listalias.1 $PKG/usr/man/preformat/cat1/listalias.1.gz $SRC/usr/man/man1/listalias.1
man2gz messages.1 $PKG/usr/man/preformat/cat1/messages.1.gz $SRC/usr/man/man1/messages.1
man2gz newalias.1 $PKG/usr/man/preformat/cat1/newalias.1.gz $SRC/usr/man/man1/newalias.1
man2gz newmail.1 $PKG/usr/man/preformat/cat1/newmail.1.gz $SRC/usr/man/man1/newmail.1
man2gz printmail.1 $PKG/usr/man/preformat/cat1/printmail.1.gz $SRC/usr/man/man1/printmail.1
man2gz readmsg.1 $PKG/usr/man/preformat/cat1/readmsg.1.gz $SRC/usr/man/man1/readmsg.1
man2gz wnewmail.1 $PKG/usr/man/preformat/cat1/wnewmail.1.gz $SRC/usr/man/man1/wnewmail.1

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

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