#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-procps
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/_procps.tar.gz

echo "+========================+"
echo "| procps-1.2.7+psmisc-17 |"
echo "+========================+"
cd $TMP
tar xzvf $CWD/procps-1.2.7.tar.gz
cd procps-1.2.7
zcat $CWD/procps-1.2.7.diff.gz | patch
zcat $CWD/procps-1.2.7.top-nobright-diff.gz | patch
zcat $CWD/procps-1.2.7.nowarning.diff.gz | patch
tar xzvf $CWD/psmisc-17.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/procps.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/procps-1.2.7
  rm -rf $PKG
fi
