#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-joe

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

echo "+===========+"
echo "| joe-2.9.5 |"
echo "+===========+"
cd $TMP
tar xzvf $CWD/joe-2.9.5.tar.gz
cd joe-2.9.5
zcat $CWD/joe-2.9.5.diff.gz | patch -p1 --backup --verbose --suffix=.orig
mkdir -p $PKG/usr/doc/joe-2.9.5
cp -a ChangeLog INFO LIST README TODO VERSION $PKG/usr/doc/joe-2.9.5
chmod 644 $PKG/usr/doc/joe-2.9.5/*
chown root.root $PKG/usr/doc/joe-2.9.5/*
cat joerc > $PKG/usr/lib/joe/joerc
cat jstarrc > $PKG/usr/lib/joe/jstarrc
cat jmacsrc > $PKG/usr/lib/joe/jmacsrc
cat jpicorc > $PKG/usr/lib/joe/jpicorc
cat rjoerc > $PKG/usr/lib/joe/rjoerc
make
make termidx
strip joe termidx
cat joe > $PKG/usr/bin/joe
cat termidx > $PKG/usr/bin/termidx
cat joe.1 | gzip -9c > $PKG/usr/man/man1/joe.1.gz

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

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

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