#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-sh_utils
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/_sh_utils.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 "| sh-utils-1.12 |"
echo "+===============+"
cd $TMP
tar xzvf $CWD/sh-utils-1.12.tar.gz
cd sh-utils-1.12
configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cd src
cat date > $PKG/bin/date
cat echo > $PKG/bin/echo
cat false > $PKG/bin/false
# We use the hostname from util-linux...
# cat hostname > $PKG/bin/hostname
cat pwd > $PKG/bin/pwd
cat stty > $PKG/bin/stty
cat su > $PKG/bin/su
cat true > $PKG/bin/true
cat uname > $PKG/bin/uname
cat basename > $PKG/usr/bin/basename
cat dirname > $PKG/usr/bin/dirname
cat env > $PKG/usr/bin/env
cat expr > $PKG/usr/bin/expr
cat groups > $PKG/usr/bin/groups
cat id > $PKG/usr/bin/id
cat logname > $PKG/usr/bin/logname
cat nice > $PKG/usr/bin/nice
cat nohup > $PKG/usr/bin/nohup
cat pathchk > $PKG/usr/bin/pathchk
cat printenv > $PKG/usr/bin/printenv
cat printf > $PKG/usr/bin/printf
cat sleep > $PKG/usr/bin/sleep
cat tee > $PKG/usr/bin/tee
cat test > $PKG/usr/bin/test
cat tty > $PKG/usr/bin/tty
cat users > $PKG/usr/bin/users
cat who > $PKG/usr/bin/who
cat whoami > $PKG/usr/bin/whoami
cat yes > $PKG/usr/bin/yes
cd ../man
man2gz basename.1 $PKG/usr/man/preformat/cat1/basename.1.gz $SRC/usr/man/man1/basename.1
man2gz date.1 $PKG/usr/man/preformat/cat1/date.1.gz $SRC/usr/man/man1/date.1
man2gz dirname.1 $PKG/usr/man/preformat/cat1/dirname.1.gz $SRC/usr/man/man1/dirname.1
man2gz echo.1 $PKG/usr/man/preformat/cat1/echo.1.gz $SRC/usr/man/man1/echo.1
man2gz env.1 $PKG/usr/man/preformat/cat1/env.1.gz $SRC/usr/man/man1/env.1
man2gz expr.1 $PKG/usr/man/preformat/cat1/expr.1.gz $SRC/usr/man/man1/expr.1
man2gz false.1 $PKG/usr/man/preformat/cat1/false.1.gz $SRC/usr/man/man1/false.1
man2gz groups.1 $PKG/usr/man/preformat/cat1/groups.1.gz $SRC/usr/man/man1/groups.1
# We use the hostname man page from util-linux...
# man2gz hostname.1 $PKG/usr/man/preformat/cat1/hostname.1.gz $SRC/usr/man/man1/hostname.1
man2gz id.1 $PKG/usr/man/preformat/cat1/id.1.gz $SRC/usr/man/man1/id.1
man2gz logname.1 $PKG/usr/man/preformat/cat1/logname.1.gz $SRC/usr/man/man1/logname.1
man2gz nice.1 $PKG/usr/man/preformat/cat1/nice.1.gz $SRC/usr/man/man1/nice.1
man2gz nohup.1 $PKG/usr/man/preformat/cat1/nohup.1.gz $SRC/usr/man/man1/nohup.1
man2gz pathchk.1 $PKG/usr/man/preformat/cat1/pathchk.1.gz $SRC/usr/man/man1/pathchk.1
man2gz printenv.1 $PKG/usr/man/preformat/cat1/printenv.1.gz $SRC/usr/man/man1/printenv.1
man2gz printf.1 $PKG/usr/man/preformat/cat1/printf.1.gz $SRC/usr/man/man1/printf.1
man2gz pwd.1 $PKG/usr/man/preformat/cat1/pwd.1.gz $SRC/usr/man/man1/pwd.1
man2gz sleep.1 $PKG/usr/man/preformat/cat1/sleep.1.gz $SRC/usr/man/man1/sleep.1
man2gz stty.1 $PKG/usr/man/preformat/cat1/stty.1.gz $SRC/usr/man/man1/stty.1
man2gz su.1 $PKG/usr/man/preformat/cat1/su.1.gz $SRC/usr/man/man1/su.1
man2gz tee.1 $PKG/usr/man/preformat/cat1/tee.1.gz $SRC/usr/man/man1/tee.1
man2gz test.1 $PKG/usr/man/preformat/cat1/test.1.gz $SRC/usr/man/man1/test.1
man2gz true.1 $PKG/usr/man/preformat/cat1/true.1.gz $SRC/usr/man/man1/true.1
man2gz tty.1 $PKG/usr/man/preformat/cat1/tty.1.gz $SRC/usr/man/man1/tty.1
man2gz uname.1 $PKG/usr/man/preformat/cat1/uname.1.gz $SRC/usr/man/man1/uname.1
man2gz users.1 $PKG/usr/man/preformat/cat1/users.1.gz $SRC/usr/man/man1/users.1
man2gz who.1 $PKG/usr/man/preformat/cat1/who.1.gz $SRC/usr/man/man1/who.1
man2gz whoami.1 $PKG/usr/man/preformat/cat1/whoami.1.gz $SRC/usr/man/man1/whoami.1
man2gz yes.1 $PKG/usr/man/preformat/cat1/yes.1.gz $SRC/usr/man/man1/yes.1
cd ../doc
cat sh-utils.info | gzip -9c > $INFO/sh-utils.info.gz
cp sh-utils.texi $TEX/sh-utils.texi

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

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