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