#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-zsh
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/_zsh.tar.gz

echo "+===========+"
echo "| zsh-3.0.6 |"
echo "+===========+"
cd $TMP
tar xyvf $CWD/zsh-3.0.6.tar.bz2
cd zsh-3.0.6
./configure --prefix=/usr
make
strip Src/zsh
cat Src/zsh > $PKG/bin/zsh
cd Doc
for file in zsh*.1 ; do
  gzip -9c $file > $PKG/usr/man/man1/$file.gz
done
mkdir -p $INFO
for file in zsh.info* ; do
  gzip -9c $file > $INFO/$file.gz
done
cd ../Etc
mkdir -p $PKG/usr/doc/zsh-3.0.6
cp -a * $PKG/usr/doc/zsh-3.0.6
rm -f $PKG/usr/doc/zsh-3.0.6/Makefile*
chown -R root.root $PKG/usr/doc/zsh-3.0.6

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