#!/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-1.2.2+psmisc-16 |"
echo "+========================+"
cd $TMP
tar xzvf $CWD/procps-1.2.2.tar.gz
cd procps-1.2.2
zcat $CWD/procps-1.2.2.diff.gz | patch
zcat $CWD/procps-1.2.2.top-nobright-diff.gz | patch
tar xzvf $CWD/psmisc-16.tar.gz
( cd xproc ; xmkmf )
( cd xcpustate ; xmkmf )
make
strip free ps psupdate sessreg tload top uptime vmstat w watch
cat free > $PKG/bin/free
cat ps > $PKG/bin/ps
cat psupdate > $PKG/sbin/psupdate
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 sessreg > $PKG/usr/bin/sessreg
cat w > $PKG/usr/bin/w
cat watch > $PKG/usr/bin/watch
for page in sessreg.1 free.1 ps.1 tload.1 top.1 uptime.1 w.1 watch.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
cd skill
strip skill
cat skill > $PKG/usr/bin/skill
cat skill.1 | gzip -9c > $PKG/usr/man/man1/skill.1.gz
cd ../psmisc
make
strip fuser pstree killall
cat fuser > $PKG/usr/bin/fuser
cat pstree > $PKG/usr/bin/pstree
cat killall > $PKG/bin/killall
for page in fuser.1 killall.1 pstree.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
# X utils no longer supported...
#cd ../xproc
#strip XConsole xload
#cat XConsole > $PKG/usr/X11R6/bin/XConsole
#cat xload > $PKG/usr/X11R6/bin/xload
#cat XConsole.man | gzip -9c > $PKG/usr/X11R6/man/man1/XConsole.1.gz
#cat xload.man | gzip -9c > $PKG/usr/X11R6/man/man1/xload.1.gz
cd ../xcpustate
strip xcpustate
cat xcpustate > $PKG/usr/X11R6/bin/xcpustate
cat xcpustate.man | gzip -9c > $PKG/usr/X11R6/man/man1/xcpustate.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-1.2.2
  rm -rf $PKG
fi