#!/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
mkdir elm2.4
cd elm2.4
tar xzvf $CWD/elm2.4.tar.gz
zcat $CWD/elm2.4.diff.gz | patch -p1
mkdir bin
make
cd bin
cat answer > $PKG/usr/bin/answer
cat checkalias > $PKG/usr/bin/checkalias
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 listalias > $PKG/usr/bin/listalias
cat messages > $PKG/usr/bin/messages
cat newalias > $PKG/usr/bin/newalias
cat newmail > $PKG/usr/bin/newmail
cat printmail > $PKG/usr/bin/printmail
cat prlong > $PKG/var/lib/elm/prlong
cat readmsg > $PKG/usr/bin/readmsg
cd ../doc
for file in answer.1 chkalias.1 elm.1 elmalias.1 fastmail.1 filter.1 frm.1 \
listalias.1 messages.1 newalias.1 newmail.1 printmail.1 readmsg.1 \
wnewmail.1 ; do
  cat $file | gzip -9c > $PKG/usr/man/man1/$file.gz 
done

# 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
