#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-tar
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/_tar.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 -s -p -t -e -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+==========+"
echo "| tar-1.12 |"
echo "+==========+"
cd $TMP
tar xzvf $CWD/tar-1.12.tar.gz
cd tar-1.12
cat scripts/dump-remind > $PKG/usr/sbin/dump-remind
cat scripts/level-0 > $PKG/usr/sbin/level-0
cat scripts/level-1 > $PKG/usr/sbin/level-1
cp -a ABOUT-NLS AUTHORS COPYING NEWS PORTS README THANKS TODO $PKG/usr/doc/tar
chown root.root $PKG/usr/doc/tar/*
chmod 644 $PKG/usr/doc/tar/*
configure --prefix=/usr --with-included-gettext
make CFLAGS=-O2 LDFLAGS=-s
cat src/tar > $PKG/bin/tar-incoming
cat src/rmt > $PKG/sbin/rmt
cat $CWD/tar.1.gz > $PKG/usr/man/man1/tar.1.gz
cat $CWD/rmt.8.gz > $PKG/usr/man/man8/rmt.8.gz
cd doc
for file in tar.info* ; do
  gzip -9c $file > $PKG/usr/info/$file.gz
done

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

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