1#!/bin/sh
2# $MirOS: src/gnu/usr.bin/texinfo/util/gen-dir-node,v 1.3 2005/06/13 20:46:50 tg Exp $
3# $Id: gen-dir-node,v 1.3 2004/04/11 17:56:47 karl Exp $
4# Generate the top-level Info node, given a directory of Info files
5# and (optionally) a skeleton file.  The output will be suitable for a
6# top-level dir file.  The skeleton file contains info topic names in the
7# order they should appear in the output.  There are three special
8# lines that alter the behavior: a line consisting of just "--" causes
9# the next line to be echoed verbatim to the output.  A line
10# containing just "%%" causes all the remaining filenames (wildcards
11# allowed) in the rest of the file to be ignored.  A line containing
12# just "!!" exits the script when reached (unless preceded by a line
13# containing just "--").  Once the script reaches the end of the
14# skeleton file, it goes through the remaining files in the directory
15# in order, putting their entries at the end.  The script will use the
16# ENTRY information in each info file if it exists.  Otherwise it will
17# make a minimal entry.
18
19# sent by Jeffrey Osier <jeffrey@cygnus.com>, who thinks it came from
20# zoo@winternet.com (david d `zoo' zuhn)
21
22# modified 7 April 1995 by Joe Harrington <jh@tecate.gsfc.nasa.gov> to
23# take special flags
24
25INFODIR=$1
26if [ $# = 2 ] ; then
27  SKELETON=$2
28else
29  SKELETON=/dev/null
30fi
31
32skip=
33
34if [ $# -gt 2 ] ; then
35  echo usage: $0 info-directory [ skeleton-file ] 1>&2
36  exit 1
37elif [ -z "${INFODIR}" ] ; then
38  INFODIR="%%DEFAULT_INFO_DIR%%"
39else
40  true
41fi
42
43if [ ! -d ${INFODIR} ] ; then
44  echo "$0: first argument must specify a directory"
45  exit 1
46fi
47
48### output the dir header
49echo "-*- Text -*-"
50echo "This file was generated automatically by $0."
51echo "This version was generated on `date`"
52echo "by `whoami`@`hostname` for `(cd ${INFODIR}; pwd)`"
53
54cat << moobler
55\$MirOS: src/gnu/usr.bin/texinfo/util/gen-dir-node,v 1.3 2005/06/13 20:46:50 tg Exp $
56This is the file .../info/dir, which contains the topmost node of the
57Info hierarchy.  The first time you invoke Info you start off
58looking at that node, which is (dir)Top.
59
60File: dir	Node: Top	This is the top of the INFO tree
61
62  This (the Directory node) gives a menu of major topics. 
63  Typing "q" exits, "?" lists all Info commands, "d" returns here,
64  "h" gives a primer for first-timers,
65  "mEmacs<Return>" visits the Emacs topic, etc.
66
67  In Emacs, you can click mouse button 2 on a menu item or cross reference
68  to select it.
69
70* Menu: The list of major topics begins on the next line.
71
72moobler
73
74### go through the list of files in the skeleton.  If an info file
75### exists, grab the ENTRY information from it.  If an entry exists
76### use it, otherwise create a minimal dir entry.
77###
78### Then remove that file from the list of existing files.  If any
79### additional files remain (ones that don't have a skeleton entry),
80### then generate entries for those in the same way, putting the info for
81### those at the end....
82
83infofiles=$( (cd ${INFODIR}; /bin/ls | grep -a -v '\-[0-9]*$' | egrep -a -v '^dir$|^dir\.info$|^dir\.orig$'))
84
85# echoing gets clobbered by backquotes; we do it the hard way...
86lines=`wc $SKELETON | awk '{print $1}'`
87line=1
88while [ $lines -ge $line ] ; do
89  # Read one line from the file.  This is so that we can echo lines with
90  # whitespace and quoted characters in them.
91  fileline=`awk NR==$line $SKELETON`
92
93  # flag fancy features
94  if [ ! -z "$echoline" ] ; then	# echo line
95    echo "$fileline"
96    fileline=
97    echoline=
98  elif [ "${fileline}" = "--" ] ; then	# should we echo the next line?
99    echoline=1
100  elif [ "${fileline}" = "%%" ] ; then	# eliminate remaining files from dir?
101    skip=1
102  elif [ "${fileline}" = "!!" ] ; then	# quit now
103    exit 0
104  fi
105
106  # handle files if they exist
107  for file in $fileline"" ; do	# expand wildcards ("" handles blank lines)
108
109    fname=
110
111    if [ -z "$echoline" ] && [ ! -z "$file" ] ; then
112      # Find the file to operate upon.  Check both possible names.
113      infoname=`echo $file | sed 's/\.info$//'`
114      noext=
115      ext=
116      if [ -f ${INFODIR}/$infoname ] ; then
117        noext=$infoname
118      fi
119      if [ -f ${INFODIR}/${infoname}.info ] ; then
120        ext=${infoname}.info
121      fi
122
123      # If it exists with both names take what was said in the file.
124      if [ ! -z "$ext" ] && [ ! -z "$noext" ]; then
125        fname=$file
126        warn="### Warning: $ext and $noext both exist!  Using ${file}. ###"
127      elif [ ! -z "${noext}${ext}" ]; then
128        # just take the name if it exists only once
129        fname=${noext}${ext}
130      fi
131
132      # if we found something and aren't skipping, do the entry
133      if [ ! -z "$fname" ] ; then
134        if [ -z "$skip" ] ; then
135
136          if [ ! -z "$warn" ] ; then	# issue any warning
137	    echo $warn
138	    warn=
139          fi
140
141          entry=`sed -e '1,/START-INFO-DIR-ENTRY/d' \
142		     -e '/END-INFO-DIR-ENTRY/,$d' ${INFODIR}/$fname`
143          if [ ! -z "${entry}" ] ; then
144            echo "${entry}"
145          else
146            echo "* ${infoname}: (${infoname})."
147          fi
148        fi
149
150        # remove the name from the directory listing
151	infofiles=`echo "" ${infofiles} "" | sed -e "s/ ${fname} / /" -e "s/  / /g"`
152
153      fi
154
155    fi
156
157  done
158
159  line=`expr $line + 1`
160done
161
162if [ -z "${infofiles}" ] ; then
163  exit 0
164elif [ $lines -gt 0 ]; then
165  echo
166fi
167
168# Sort remaining files by INFO-DIR-SECTION.
169prevsect=
170filesectdata=$( (cd ${INFODIR}; fgrep -a INFO-DIR-SECTION /dev/null ${infofiles} | \
171	      fgrep -a -v 'INFO-DIR-SECTION Miscellaneous' | \
172	      sort -t: -k2 -k1 | tr ' ' '_'))
173for sectdata in ${filesectdata}; do
174  file=`echo ${sectdata} | cut -d: -f1`
175  section=`sed -n -e 's/^INFO-DIR-SECTION //p' ${INFODIR}/${file}`
176  infofiles=`echo "" ${infofiles} "" | sed -e "s/ ${file} / /" -e "s/  / /g"`
177
178  if [ "${prevsect}" != "${section}" ] ; then
179    if [ ! -z "${prevsect}" ] ; then
180      echo ""
181    fi
182    echo "${section}"
183    prevsect="${section}"
184  fi
185
186  infoname=`echo $file | sed 's/\.info$//'`
187  entry=`sed -e '1,/START-INFO-DIR-ENTRY/d' \
188	-e '/END-INFO-DIR-ENTRY/,$d' ${INFODIR}/${file}`
189  if [ ! -z "${entry}" ] ; then
190    echo "${entry}"
191  elif [ ! -d "${INFODIR}/${file}" ] ; then
192    echo "* ${infoname}: (${infoname})."
193  fi
194done
195
196# Process miscellaneous files.
197for file in ${infofiles}; do
198  if [ ! -z "${prevsect}" ] ; then
199    echo ""
200    echo "Miscellaneous"
201    prevsect=""
202  fi
203
204  infoname=`echo $file | sed 's/\.info$//'`
205  entry=`sed -e '1,/START-INFO-DIR-ENTRY/d' \
206	-e '/END-INFO-DIR-ENTRY/,$d' ${INFODIR}/${file}`
207
208  if [ ! -z "${entry}" ] ; then
209    echo "${entry}"
210  elif [ ! -d "${INFODIR}/${file}" ] ; then
211    echo "* ${infoname}: (${infoname})."
212  fi
213done
214