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

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

echo "+==============+"
echo "| texinfo-3.12 |"
echo "+==============+"
cd $TMP
tar xzvf $CWD/texinfo-3.12.tar.gz
cd texinfo-3.12
mkdir -p $PKG/usr/doc/texinfo-3.12
cp -a ABOUT-NLS AUTHORS COPYING INSTALL INTRODUCTION NEWS \
README TODO $PKG/usr/doc/texinfo-3.12
chmod 644 $PKG/usr/doc/texinfo-3.12/*
chown root.root $PKG/usr/doc/texinfo-3.12/*
./configure --prefix=/usr
make CFLAGS=-O2 LDFLAGS=-s
cat info/ginfo > $PKG/usr/bin/info
cat util/install-info > $PKG/usr/bin/install-info
cat makeinfo/makeinfo > $PKG/usr/bin/makeinfo
cat util/texi2dvi > $PKG/usr/bin/texi2dvi
cat util/texindex > $PKG/usr/bin/texindex
cat $CWD/info.1.gz > $PKG/usr/man/man1/info.1.gz
cd po
for file in *.gmo ; do
 DIR=`basename $file .gmo`
 mkdir -p $PKG/usr/share/locale/$DIR/LC_MESSAGES
 cat $file > $PKG/usr/share/locale/$DIR/LC_MESSAGES/texinfo.mo
done
cd ../doc
for file in info-stnd.info info.info texinfo texinfo-* ; do
  gzip -9c $file > $PKG/usr/info/$file.gz
done
mkdir -p $PKG/usr/share/texmf/tex/texinfo
cp -a texinfo.tex $PKG/usr/share/texmf/tex/texinfo/texinfo.tex
chown root.root $PKG/usr/share/texmf/tex/texinfo/texinfo.tex
mkdir -p $PKG/usr/share/texmf/tex/plain/dvips
cp -a epsf.tex $PKG/usr/share/texmf/tex/plain/dvips/epsf.tex
chown root.root $PKG/usr/share/texmf/tex/plain/dvips/epsf.tex

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