#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-sysvinit
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
tar xzvf $CWD/_sysvinit.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 "| SysVinit-2.4 |"
echo "+==============+"
cd $TMP
tar xzvf $CWD/SysVinit-2.4.tar.gz
cd sysvinit
zcat $CWD/SysVinit-2.4.diff.gz | patch
make
cat halt > $PKG/sbin/halt
cat init > $PKG/sbin/init
cat shutdown > $PKG/sbin/shutdown
cat powerd > $PKG/usr/sbin/powerd
man2gz inittab.5 $PKG/usr/man/preformat/cat5/inittab.5.gz $SRC/usr/man/man5/inittab.5
man2gz halt.8 $PKG/usr/man/preformat/cat8/halt.8.gz $SRC/usr/man/man8/halt.8
man2gz init.8 $PKG/usr/man/preformat/cat8/init.8.gz $SRC/usr/man/man8/init.8
man2gz shutdown.8 $PKG/usr/man/preformat/cat8/shutdown.8.gz $SRC/usr/man/man8/shutdown.8

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

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