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

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

echo "+=============+"
echo "| bzip2-1.0.0 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/bzip2-1.0.0.tar.gz
cd bzip2-1.0.0
make -f Makefile-libbz2_so
make
mkdir $PKG/usr/include
cp -a bzlib.h $PKG/usr/include
chown root.root $PKG/usr/include/bzlib.h
chmod 644 $PKG/usr/include/bzlib.h
mkdir -p $PKG/usr/lib
cp -a libbz2.a $PKG/usr/lib/libbz2.a
cp -a libbz2.so.1.0.0 $PKG/lib/libbz2.so.1.0.0
chown root.root $PKG/usr/lib/libbz2.a $PKG/lib/libbz2.so.1.0.0
chmod 644 $PKG/usr/lib/libbz2.a
chmod 755 $PKG/lib/libbz2.so.1.0.0
strip bzip2-shared bzip2recover
cat bzip2-shared > $PKG/bin/bzip2
cat bzip2recover > $PKG/bin/bzip2recover
cat bzip2.1 | gzip -9c > $PKG/usr/man/man1/bzip2.1.gz
echo '.so man1/bzip2.1' | gzip -9c > $PKG/usr/man/man1/bzip2recover.1.gz
mkdir -p $PKG/usr/doc/bzip2-1.0.0
cp -a CHANGES LICENSE README README.COMPILATION.PROBLEMS Y2K_INFO bzip2.txt *.html $PKG/usr/doc/bzip2-1.0.0
chown root.root $PKG/usr/doc/bzip2-1.0.0/*
chmod 644 $PKG/usr/doc/bzip2-1.0.0/*

# Build the package:
cd $PKG
tar czvf $TMP/bzip2.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
done

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