#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-emacspeak
SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

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:
echo cd $PKG
     cd $PKG
echo tar -xzf $CWD/_emacspeak.tar.gz
     tar -xzf $CWD/_emacspeak.tar.gz

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+===============+"
echo "| emacspeak-6.0 |"
echo "+===============+"
echo cd $TMP
     cd $TMP
echo mkdir emacspeak-6.0
     mkdir emacspeak-6.0
# this source archive unpacks itself into the current directory, 
# not a subdirectory
echo cd emacspeak-6.0
     cd emacspeak-6.0
echo tar -xvzf $CWD/emacspeak-6.0.tgz
     tar -xzf $CWD/emacspeak-6.0.tgz
## Apply patches.  Move the "make config" line so we can install into
## a different directory than we're configured for.
echo "patch -s -p0 < $CWD/emacspeak-patch"
     patch -s -p0 < $CWD/emacspeak-patch
# insert code to consult the configuration file
if grep ^source emacspeak.def; then true; else\
  awk '/^emacs/{print "source /etc/emacspeak.conf"}{print}' \
  emacspeak.def >tmp$$; mv tmp$$ emacspeak.def; \
fi
echo make PREFIX=/usr config
     make PREFIX=/usr config
if grep -q ^source emacspeak.def; then true; else
  awk '/^emacs/{print "source /etc/emacspeak.conf"}{print}' \
  emacspeak.def >tmp$$; mv tmp$$ emacspeak.def; 
fi
echo make PREFIX=/usr
     make PREFIX=/usr
echo "cd info; make PREFIX=/usr; cd .."
     cd info; make PREFIX=/usr; cd ..
echo make -k PREFIX=$PKG/usr install
     make -k PREFIX=$PKG/usr install
# we don't want to replace the user's info
# directory while installing the package
# (/install/doinst.sh will create dir if it doesn't exist)
echo rm -f $PKG/usr/info/dir
     rm -f $PKG/usr/info/dir
echo cp README COPYRIGHT info/*.html $PKG/usr/doc/emacspeak
     cp README COPYRIGHT info/*.html $PKG/usr/doc/emacspeak

# Build the package:
echo cd $PKG
     cd $PKG
#echo chown root.root `find . -print`
     chown root.root `find . -print`
echo chown root.bin `find usr/bin -print`
     chown root.bin `find usr/bin -print`
echo tar -cvzf $TMP/emacspeak.tgz .
     tar -czf $TMP/emacspeak.tgz .

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