#!/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.28 |"
echo "+============+"
cd $TMP
tar xzvf $CWD/bison-1.28.tar.gz
cd bison-1.28
mkdir -p $PKG/usr/doc/bison-1.28
cp -a ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL NEWS README REFERENCES doc/FAQ \
      $PKG/usr/doc/bison-1.28
chmod 644 $PKG/usr/doc/bison-1.28/*
chown root.root $PKG/usr/doc/bison-1.28/*
./configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cd src
cat bison.simple > $PKG/usr/share/bison.simple
cat bison.hairy > $PKG/usr/share/bison.hairy
cat bison > $PKG/usr/bin/bison
cd ../doc
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
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/bison.mo
done

# 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.28
  rm -rf $PKG
fi