#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-textutils

VERSION=2.0
ARCH=i386
BUILD=1

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/_textutils.tar.gz

echo "+================+"
echo "| textutils-$VERSION |"
echo "+================+"
cd $TMP
tar xzvf $CWD/textutils-$VERSION.tar.gz
cd textutils-$VERSION
mkdir $PKG/usr/doc/textutils-$VERSION
cp -a NEWS README $PKG/usr/doc/textutils-$VERSION
chmod 644 $PKG/usr/doc/textutils-$VERSION/*
chown root.root $PKG/usr/doc/textutils-$VERSION/*
./configure --prefix=/usr i386-slackware-linux
make
cd src
strip *
cat cat > $PKG/bin/cat
cat cksum > $PKG/usr/bin/cksum
cat comm > $PKG/usr/bin/comm
cat csplit > $PKG/usr/bin/csplit
cat cut > $PKG/bin/cut
cat expand > $PKG/usr/bin/expand
cat fmt > $PKG/usr/bin/fmt
cat fold > $PKG/usr/bin/fold
cat head > $PKG/bin/head
cat join > $PKG/usr/bin/join
cat md5sum > $PKG/usr/bin/md5sum
cat nl > $PKG/usr/bin/nl
cat od > $PKG/usr/bin/od
cat paste > $PKG/usr/bin/paste
cat pr > $PKG/usr/bin/pr
cat ptx > $PKG/usr/bin/ptx
cat sort > $PKG/usr/bin/sort
cat split > $PKG/usr/bin/split
cat sum > $PKG/usr/bin/sum
cat tac > $PKG/usr/bin/tac
cat tail > $PKG/usr/bin/tail
cat tr > $PKG/usr/bin/tr
cat tsort > $PKG/usr/bin/tsort
cat unexpand > $PKG/usr/bin/unexpand
cat uniq > $PKG/usr/bin/uniq
cat wc > $PKG/usr/bin/wc
cd ../man
for page in cat.1 cksum.1 comm.1 csplit.1 cut.1 expand.1 fmt.1 \
fold.1 head.1 join.1 md5sum.1 nl.1 od.1 paste.1 pr.1 ptx.1 sort.1 \
split.1 sum.1 tac.1 tail.1 tr.1 tsort.1 unexpand.1 uniq.1 wc.1 ; do
  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
done
cd ../po
for file in *.gmo ; do
  mkdir -p $PKG/usr/share/locale/`basename $file .gmo`/LC_MESSAGES
  cat $file > $PKG/usr/share/locale/`basename $file .gmo`/LC_MESSAGES/textutils.mo
done
cd ../doc
gzip -9c textutils.info > $PKG/usr/info/textutils.info.gz

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
echo "n" | makepkg $TMP/textutils-$VERSION-$ARCH-$BUILD.tgz

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