#!/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-4.0 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/texinfo-4.0.tar.gz
cd texinfo-4.0
./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
cd doc
for file in info-stnd.info* info.info texinfo texinfo-* ; do
  gzip -9c $file > $PKG/usr/info/$file.gz
done
for file in info.1 install-info.1 makeinfo.1 texi2dvi.1 texindex.1 ; do
  gzip -9c $file > $PKG/usr/man/man1/$file.gz
done
for file in info.5 texinfo.5 ; do
  gzip -9c $file > $PKG/usr/man/man5/$file.gz
done
# teTeX carries it's own copies, so we won't much around in there:
#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
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 ../
mkdir -p $PKG/usr/doc/texinfo-4.0
cp -a ABOUT-NLS AUTHORS COPYING ChangeLog INSTALL INTRODUCTION NEWS \
   README TODO $PKG/usr/doc/texinfo-4.0
chmod 644 $PKG/usr/doc/texinfo-4.0/*
chown root.root $PKG/usr/doc/texinfo-4.0/*

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