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

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

echo "+============+"
echo "| bison-1.27 |"
echo "+============+"
cd $TMP
tar xzvf $CWD/bison-1.27.tar.gz
cd bison-1.27
mkdir -p $PKG/usr/doc/bison-1.27
cp -a AUTHORS COPYING ChangeLog INSTALL NEWS README REFERENCES \
      $PKG/usr/doc/bison-1.27
chmod 644 $PKG/usr/doc/bison-1.27/*
chown root.root $PKG/usr/doc/bison-1.27/*
./configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cat bison.simple > $PKG/usr/share/bison.simple
cat bison.hairy > $PKG/usr/share/bison.hairy
cat bison > $PKG/usr/bin/bison
mkdir -p $PKG/usr/info
for file in bison.info* ; do
 cat $file | gzip -9c > $PKG/usr/info/$file.gz
done
cat bison.1 | gzip -9c > $PKG/usr/man/man1/bison.1.gz

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

# Warn of zero-length files:
find . -type f -size 0c | while read file ; do
  echo "WARNING: zero length file $file"
done
find . -type f -name '*.gz' -size 20c | while read file ; do
  echo "WARNING: possible empty gzipped file $file"
done

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