#!/bin/sh
#
# generate an Info directory, given a directory of Info files and a
# list of entries.  The output will be suitable for a dir.info file,
# in the order given in the skeleton file

INFODIR=$1
if [ $# = 2 ] ; then
  SKELETON=$2
else
  SKELETON=/dev/null
fi

skip=

if [ $# -gt 2 ] ; then
  echo usage: $0 info-directory [ skeleton-file ] 1>&2
  exit 1
else
  true
fi

if [ ! -d ${INFODIR} ] ; then
  echo "$0: first argument must specify a directory"
  exit 1
fi

infofiles=`(cd ${INFODIR}; ls *.info | sort | sed "s/dir\.info//")`
template=`cat ${SKELETON}`

### output the dir.info header
echo "-*- Text -*-"
echo "This file was generated automatically by the gen-info-dir script."
echo "This version was generated on `date`"
echo "by `whoami`@`hostname` for `(cd ${INFODIR}; pwd)`"

cat << moobler

This is the file .../info/dir, which contains the topmost node of the
Info hierarchy.  The first time you invoke Info you start off
looking at that node, which is (dir)Top.

File: dir	Node: Top	This is the top of the INFO tree
  This (the Directory node) gives a menu of major topics. 
  Typing "d" returns here, "q" exits, "?" lists all INFO commands, "h" 
  gives a primer for first-timers, "mTexinfo<Return>" visits Texinfo topic,
  etc.
  --- PLEASE ADD DOCUMENTATION TO THIS TREE. (See INFO topic first.) ---

* Menu: The list of major topics begins on the next line.

moobler


### go through the list of files in the template.  If an info file
### exists, grab the ENTRY information from it.  If there is no entry
### info, then create a minimal dir entry, otherwise use the given info.
###
### Then remove that file from the list of existing files.  If any
### additional files remain (ones that don't have a template entry), 
### then generate entries for those in the same way, putting the info for 
### those at the very end....

for file in ${template} ; do
  if [ "${file}" = "--" ] ; then
    skip=1
  else
    if [ -f ${INFODIR}/${file}.info ] ; then
      entry=`sed -e '1,/START-INFO-DIR-ENTRY/d' -e '/END-INFO-DIR-ENTRY/,$d' ${INFODIR}/${file}.info`
      if [ ! -z "${skip}" ] ; then
	echo
        skip=
      fi

      if [ ! -z "${entry}" ] ; then
	echo "${entry}"
      else
	echo "* ${file}: (${file})."
      fi

      infofiles=`echo ${infofiles} | sed -e "s/${file}\.info//"`
    fi
  fi 
done

if [ -z "${infofiles}" ] ; then
  exit 0
else
  echo
fi

for file in ${infofiles}; do
  entry=`sed -e '1,/START-INFO-DIR-ENTRY/d' -e '/END-INFO-DIR-ENTRY/,$d' ${INFODIR}/${file}`

  if [ ! -z "${entry}" ] ; then
    echo "${entry}"
  else
    echo "* ${file}: (${file})."
  fi
done

