#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-ps
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/_ps.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 "| procps-0.97 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/procps-0.97.tar.gz
cd procps-0.97
zcat $CWD/procps-0.97.diff.gz | patch
make
cat free > $PKG/bin/free
cat killall > $PKG/bin/killall
cat ps > $PKG/bin/ps
cat psupdate > $PKG/usr/sbin/psupdate
cat fuser > $PKG/usr/bin/fuser
cat pstree > $PKG/usr/bin/pstree
cat tload > $PKG/usr/bin/tload
cat top > $PKG/usr/bin/top
cat uptime > $PKG/usr/bin/uptime
cat vmstat > $PKG/usr/bin/vmstat
cat w > $PKG/usr/bin/w.procps
for page in free.1 fuser.1 killall.1 ps.1 pstree.1 tload.1 top.1 uptime.1 w.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
for page in vmstat.8 psupdate.8 ; do
  cat $page | gzip -9c > $PKG/usr/man/man8/$page.gz
done

echo "+===========+"
echo "| w.bassman |"
echo "+===========+"
tar xzvf $CWD/w.bassman.tar.gz
gcc -s -O2 -o w.bassman sysinfo.c whattime.c w.bassman.c
cat w.bassman > $PKG/usr/bin/w.bassman
cat w.bassman.1 | gzip -9c > $PKG/usr/man/man1/w.bassman.1.gz

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

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