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

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

echo "+=============+"
echo "| bash-1.14.7 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/bash-1.14.7.tar.gz
cd bash-1.14.7
zcat $CWD/bash-1.14.7.diff.gz | patch -p1 -E
make CFLAGS=-O2 LDFLAGS=-s
cat bash > $PKG/bin/bash1
mkdir -p $PKG/usr/doc/bash-1.14.7
cp -a NEWS README RELEASE COPYING $PKG/usr/doc/bash-1.14.7
cd documentation
cp -a article.txt $PKG/usr/doc/bash-1.14.7
chown root.root $PKG/usr/doc/bash-1.14.7/*
chmod 644 $PKG/usr/doc/bash-1.14.7/*
cat bash.1 | gzip -9c > $PKG/usr/man/man1/bash1.1.gz
cat builtins.1 | gzip -9c > $PKG/usr/man/man1/builtins1.1.gz
#for page in bash.1 builtins.1 ; do
#  cat $page | gzip -9c > $PKG/usr/man/man1/$page.gz
#done
#cat readline.3 | gzip -9c > $PKG/usr/man/man3/readline.3.gz
mkdir -p $PKG/usr/info
cat features.info | gzip -9c > $PKG/usr/info/features.info.gz

# Build the package:
cd $PKG
tar czvf $TMP/bash1.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/bash-1.14.7
  rm -rf $PKG
fi
