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

echo "+================+"
echo "| readline-2.2.1 |"
echo "+================+"
cd $TMP
tar xzvf $CWD/readline-2.2.1.tar.gz
cd readline-2.2.1
zcat $CWD/readline-2.2.1-shared.patch.gz | patch -p1
./configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s static shared
make install install-shared prefix=$PKG/usr
rm $PKG/usr/info/dir
gzip -9 $PKG/usr/man/man3/*
gzip -9 $PKG/usr/info/*
chmod 755 $PKG/usr/lib/lib*.so.*
( cd $PKG/usr/lib ; ldconfig . )
( cd $PKG/usr/lib ; rm -f libhistory.so libreadline.so )
( cd $PKG/usr/lib ; ln -sf libhistory.so.3 libhistory.so )
( cd $PKG/usr/lib ; ln -sf libreadline.so.3 libreadline.so )
mkdir -p $PKG/usr/doc/readline-2.2.1
cp -a CHANGELOG CHANGES COPYING INSTALL MANIFEST README examples \
  $PKG/usr/doc/readline-2.2.1
chown -R root.root $PKG/usr/doc/readline-2.2.1
chmod -R 644 $PKG/usr/doc/readline-2.2.1/*
chmod 755 $PKG/usr/doc/readline-2.2.1/examples

# Build the package:
cd $PKG
# Since we make symlinks, we better process this like so:
makepkg $TMP/readline.tgz
#tar czvf $TMP/readline.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/readline-2.2.1
  rm -rf $PKG
fi