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