Home | History | Annotate | Line # | Download | only in util
fix-info-dir revision 1.1.1.1
      1  1.1  christos #!/bin/sh
      2  1.1  christos #fix-info-dir (GNU texinfo)
      3  1.1  christos VERSION=1.1
      4  1.1  christos #Copyright (C) 1998, 2003 Free Software Foundation, Inc.
      5  1.1  christos #fix-info-dir comes with NO WARRANTY, to the extent permitted by law.
      6  1.1  christos #You may redistribute copies of fix-info-dir
      7  1.1  christos #under the terms of the GNU General Public License.
      8  1.1  christos #For more information about these matters, see the files named COPYING."
      9  1.1  christos #fix-info-dir was derived from update-info and gen-dir-node
     10  1.1  christos # The skeleton file contains info topic names in the
     11  1.1  christos # order they should appear in the output.  There are three special
     12  1.1  christos # lines that alter the behavior: a line consisting of just "--" causes
     13  1.1  christos # the next line to be echoed verbatim to the output.  A line
     14  1.1  christos # containing just "%%" causes all the remaining filenames (wildcards
     15  1.1  christos # allowed) in the rest of the file to be ignored.  A line containing
     16  1.1  christos # just "!!" exits the script when reached (unless preceded by a line
     17  1.1  christos # containing just "--").
     18  1.1  christos #Author: Richard L. Hawes, rhawes (at] dmapub.dma.org.
     19  1.1  christos 
     20  1.1  christos # ###SECTION 1### Constants
     21  1.1  christos set -h 2>/dev/null
     22  1.1  christos # ENVIRONMENT
     23  1.1  christos if test -z "$TMPDIR"; then
     24  1.1  christos 	TMPDIR="/usr/tmp"
     25  1.1  christos fi
     26  1.1  christos if test -z "$LINENO"; then
     27  1.1  christos 	LINENO="0"
     28  1.1  christos fi
     29  1.1  christos 
     30  1.1  christos MENU_BEGIN='^\*\([ 	]\)\{1,\}Menu:'
     31  1.1  christos MENU_ITEM='^\* ([^ 	]).*:([ 	])+\('
     32  1.1  christos MENU_FILTER1='s/^\*\([ 	]\)\{1,\}/* /'
     33  1.1  christos MENU_FILTER2='s/\([ 	]\)\{1,\}$//g'
     34  1.1  christos 
     35  1.1  christos TMP_FILE1="${TMPDIR}/fx${$}.info"
     36  1.1  christos TMP_FILE2="${TMPDIR}/fy${$}.info"
     37  1.1  christos TMP_FILE_LIST="$TMP_FILE1 $TMP_FILE2"
     38  1.1  christos 
     39  1.1  christos TRY_HELP_MSG="Try --help for more information"
     40  1.1  christos 
     41  1.1  christos # ###SECTION 100### main program
     42  1.1  christos #variables set by options
     43  1.1  christos CREATE_NODE=""
     44  1.1  christos DEBUG=":"
     45  1.1  christos MODE=""
     46  1.1  christos #
     47  1.1  christos Total="0"
     48  1.1  christos Changed=""
     49  1.1  christos 
     50  1.1  christos while test "$*"; do
     51  1.1  christos 	case "$1" in
     52  1.1  christos 		-c|--create)    CREATE_NODE="y";;
     53  1.1  christos 		--debug)	set -eux; DEBUG="set>&2";;
     54  1.1  christos 		-d|--delete)	MODE="Detect_Invalid";;
     55  1.1  christos 		+d);;
     56  1.1  christos 		--version)
     57  1.1  christos cat<<VersionEOF
     58  1.1  christos fix-info-dir (GNU Texinfo) $VERSION
     59  1.1  christos Copyright (C) 1998 Free Software Foundation, Inc.
     60  1.1  christos fix-info-dir comes with NO WARRANTY, to the extent permitted by law.
     61  1.1  christos You may redistribute copies of fix-info-dir
     62  1.1  christos under the terms of the GNU General Public License.
     63  1.1  christos For more information about these matters, see the files named COPYING.
     64  1.1  christos Author: Richard L. Hawes
     65  1.1  christos VersionEOF
     66  1.1  christos 		exit;;
     67  1.1  christos 
     68  1.1  christos 		--help)
     69  1.1  christos cat<<HelpEndOfFile
     70  1.1  christos Usage:	fix-info-dir  [OPTION]... [INFO_DIR/[DIR_FILE]] [SKELETON]
     71  1.1  christos 
     72  1.1  christos It detects and inserts missing menu items into the info dir file.
     73  1.1  christos The info dir must be the current directory.
     74  1.1  christos 
     75  1.1  christos Options:
     76  1.1  christos -c,	--create	create a new info node
     77  1.1  christos -d,	--delete	delete invalid menu items (ignore missing menu items)
     78  1.1  christos 	--debug		print debug information to standard error path
     79  1.1  christos 	--help		print this help message and exit
     80  1.1  christos 	--version	print current version and exit
     81  1.1  christos Backup of the info node has a '.old' suffix added.  This is a shell script.
     82  1.1  christos Environment Variables: TMPDIR
     83  1.1  christos Email bug reports to bug-texinfo@gnu.org.
     84  1.1  christos HelpEndOfFile
     85  1.1  christos 		exit;;
     86  1.1  christos 
     87  1.1  christos 		[-+]*)	echo "$0:$LINENO: \"$1\" is not a valid option">&2
     88  1.1  christos 			echo "$TRY_HELP_MSG">&2
     89  1.1  christos 			exit 2;;
     90  1.1  christos 		*) break;;
     91  1.1  christos 	esac
     92  1.1  christos 	shift
     93  1.1  christos done
     94  1.1  christos 
     95  1.1  christos ORIGINAL_DIR=`pwd`
     96  1.1  christos 
     97  1.1  christos if test "$#" -gt "0"; then
     98  1.1  christos 	INFO_DIR="$1"
     99  1.1  christos 	shift
    100  1.1  christos else
    101  1.1  christos 	INFO_DIR=$DEFAULT_INFO_DIR
    102  1.1  christos fi
    103  1.1  christos 
    104  1.1  christos if test ! -d "${INFO_DIR}"; then
    105  1.1  christos 	DIR_FILE=`basename ${INFO_DIR}`;
    106  1.1  christos 	INFO_DIR=`dirname ${INFO_DIR}`;
    107  1.1  christos else
    108  1.1  christos 	DIR_FILE="dir"
    109  1.1  christos fi
    110  1.1  christos 
    111  1.1  christos cd "$INFO_DIR"||exit
    112  1.1  christos 
    113  1.1  christos 
    114  1.1  christos if test "$CREATE_NODE"; then
    115  1.1  christos 	if test "$#" -gt "0"; then
    116  1.1  christos 		if test `expr $1 : /` = '1'; then
    117  1.1  christos 			SKELETON="$1"
    118  1.1  christos 		else
    119  1.1  christos 			SKELETON="$ORIGINAL_DIR/$1"
    120  1.1  christos 		fi
    121  1.1  christos 		if test ! -r "$SKELETON" && test -f "$SKELETON"; then
    122  1.1  christos 			echo "$0:$LINENO: $SKELETON is not readable">&2
    123  1.1  christos 			exit 2
    124  1.1  christos 		fi
    125  1.1  christos 		shift
    126  1.1  christos 	else
    127  1.1  christos 		SKELETON=/dev/null
    128  1.1  christos 
    129  1.1  christos 	fi
    130  1.1  christos else
    131  1.1  christos 	if test ! -f "$DIR_FILE"; then
    132  1.1  christos 		echo "$0:$LINENO: $DIR_FILE is irregular or nonexistant">&2
    133  1.1  christos 		exit 2
    134  1.1  christos 	elif test ! -r "$DIR_FILE"; then
    135  1.1  christos 		echo "$0:$LINENO: $DIR_FILE is not readable">&2
    136  1.1  christos 		exit 2
    137  1.1  christos 	elif test ! -w "$DIR_FILE"; then
    138  1.1  christos 		echo "$0:$LINENO: $DIR_FILE is not writeable">&2
    139  1.1  christos 		exit 2
    140  1.1  christos 	fi
    141  1.1  christos fi
    142  1.1  christos 
    143  1.1  christos if test "$#" -gt "0"; then
    144  1.1  christos 	echo "$0:$LINENO: Too many parameters">&2
    145  1.1  christos 	echo "$TRY_HELP_MSG">&2
    146  1.1  christos 	exit 2
    147  1.1  christos fi
    148  1.1  christos 
    149  1.1  christos if test -f "$DIR_FILE"; then
    150  1.1  christos 	cp "$DIR_FILE" "$DIR_FILE.old"
    151  1.1  christos 	echo "Backed up $DIR_FILE to $DIR_FILE.old."
    152  1.1  christos fi
    153  1.1  christos 
    154  1.1  christos if test "$CREATE_NODE"; then
    155  1.1  christos 	if test "$MODE"; then
    156  1.1  christos 		echo "$0:$LINENO: ERROR: Illogical option combination: -d -c">&2
    157  1.1  christos 		echo "$TRY_HELP_MSG">&2
    158  1.1  christos 		exit 2
    159  1.1  christos 	fi
    160  1.1  christos 	echo "Creating new Info Node: `pwd`/$DIR_FILE"
    161  1.1  christos 	Changed="y"
    162  1.1  christos 
    163  1.1  christos {
    164  1.1  christos 
    165  1.1  christos 	### output the dir header
    166  1.1  christos 	echo "-*- Text -*-"
    167  1.1  christos 	echo "This file was generated automatically by $0."
    168  1.1  christos 	echo "This version was generated on `date`"
    169  1.1  christos 	echo "by `whoami`@`hostname` for `pwd`"
    170  1.1  christos 
    171  1.1  christos 	cat<<DIR_FILE_END_OF_FILE
    172  1.1  christos This is the file .../info/$DIR_FILE, which contains the topmost node of the
    173  1.1  christos Info hierarchy.  The first time you invoke Info you start off
    174  1.1  christos looking at that node, which is ($DIR_FILE)Top.
    175  1.1  christos 
    176  1.1  christos 
    177  1.1  christos File: $DIR_FILE       Node: Top       This is the top of the INFO tree
    178  1.1  christos 
    179  1.1  christos   This (the Directory node) gives a menu of major topics.
    180  1.1  christos   Typing "q" exits, "?" lists all Info commands, "d" returns here,
    181  1.1  christos   "h" gives a primer for first-timers,
    182  1.1  christos   "mEmacs<Return>" visits the Emacs topic, etc.
    183  1.1  christos 
    184  1.1  christos   In Emacs, you can click mouse button 2 on a menu item or cross reference
    185  1.1  christos   to select it.
    186  1.1  christos 
    187  1.1  christos * Menu: The list of major topics begins on the next line.
    188  1.1  christos 
    189  1.1  christos DIR_FILE_END_OF_FILE
    190  1.1  christos 
    191  1.1  christos ### go through the list of files in the skeleton.  If an info file
    192  1.1  christos ### exists, grab the ENTRY information from it.  If an entry exists
    193  1.1  christos ### use it, otherwise create a minimal $DIR_FILE entry.
    194  1.1  christos 
    195  1.1  christos 	# Read one line from the file.  This is so that we can echo lines with
    196  1.1  christos 	# whitespace and quoted characters in them.
    197  1.1  christos 	while read fileline; do
    198  1.1  christos 		# flag fancy features
    199  1.1  christos 		if test ! -z "$echoline"; then        # echo line
    200  1.1  christos 			echo "$fileline"
    201  1.1  christos 			echoline=""
    202  1.1  christos 			continue
    203  1.1  christos 		elif test "${fileline}" = "--"; then
    204  1.1  christos 			# echo the next line
    205  1.1  christos 			echoline="1"
    206  1.1  christos 			continue
    207  1.1  christos 		elif test "${fileline}" = "%%"; then
    208  1.1  christos 			# skip remaining files listed in skeleton file
    209  1.1  christos 			skip="1"
    210  1.1  christos 			continue
    211  1.1  christos 		elif test "${fileline}" = "!!"; then
    212  1.1  christos 			# quit now
    213  1.1  christos 			break
    214  1.1  christos 		fi
    215  1.1  christos 
    216  1.1  christos 		# handle files if they exist
    217  1.1  christos 		for file in $fileline""; do
    218  1.1  christos 			fname=
    219  1.1  christos 			if test -z "$file"; then
    220  1.1  christos 				break
    221  1.1  christos 			fi
    222  1.1  christos 			# Find the file to operate upon.
    223  1.1  christos 			if test -r "$file"; then
    224  1.1  christos 				fname="$file"
    225  1.1  christos 			elif test -r "${file}.info"; then
    226  1.1  christos 				fname="${file}.info"
    227  1.1  christos 			elif test -r "${file}.gz"; then
    228  1.1  christos 				fname="${file}.gz"
    229  1.1  christos 			elif test -r "${file}.info.gz"; then
    230  1.1  christos 				fname="${file}.info.gz"
    231  1.1  christos 			else
    232  1.1  christos 				echo "$0:$LINENO: can't find info file for ${file}?">&2
    233  1.1  christos 				continue
    234  1.1  christos 			fi
    235  1.1  christos 
    236  1.1  christos 			# if we found something and aren't skipping, do the entry
    237  1.1  christos 			if test "$skip"; then
    238  1.1  christos 				continue
    239  1.1  christos 			fi
    240  1.1  christos 
    241  1.1  christos 			infoname=`echo $file|sed -e 's/.info$//'`
    242  1.1  christos 			entry=`zcat -f $fname|\
    243  1.1  christos 			sed -e '1,/START-INFO-DIR-ENTRY/d'\
    244  1.1  christos 			-e '/END-INFO-DIR-ENTRY/,$d'`
    245  1.1  christos 			if [ ! -z "${entry}" ]; then
    246  1.1  christos 				echo "${entry}"
    247  1.1  christos 			else
    248  1.1  christos 				echo "* ${infoname}: (${infoname})."
    249  1.1  christos 			fi
    250  1.1  christos 			Total=`expr "$Total" + "1"`
    251  1.1  christos 		done
    252  1.1  christos 	done
    253  1.1  christos }>$DIR_FILE<$SKELETON
    254  1.1  christos fi
    255  1.1  christos 
    256  1.1  christos trap ' eval "$DEBUG"; rm -f $TMP_FILE_LIST; exit ' 0
    257  1.1  christos trap ' rm -f $TMP_FILE_LIST
    258  1.1  christos 	exit ' 1
    259  1.1  christos trap ' rm -f $TMP_FILE_LIST
    260  1.1  christos 	echo "$0:$LINENO: received INT signal.">&2
    261  1.1  christos 	exit ' 2
    262  1.1  christos trap ' rm -f $TMP_FILE_LIST
    263  1.1  christos 	echo "$0:$LINENO: received QUIT signal.">&2
    264  1.1  christos 	exit ' 3
    265  1.1  christos 
    266  1.1  christos sed -e "1,/$MENU_BEGIN/d" -e "$MENU_FILTER1" -e "$MENU_FILTER2"<$DIR_FILE\
    267  1.1  christos |sed -n -e '/\* /{
    268  1.1  christos s/).*$//g
    269  1.1  christos s/\.gz$//
    270  1.1  christos s/\.info$//
    271  1.1  christos s/^.*(//p
    272  1.1  christos }'|sort -u>$TMP_FILE1
    273  1.1  christos ls -F|sed -e '/\/$/d' -e '/[-.][0-9]/d'\
    274  1.1  christos 	-e "/^$DIR_FILE\$/d" -e "/^$DIR_FILE.old\$/d"\
    275  1.1  christos 	-e 's/[*@]$//' -e 's/\.gz$//' -e 's/\.info$//'|sort>$TMP_FILE2
    276  1.1  christos 
    277  1.1  christos if test -z "$MODE"; then
    278  1.1  christos 	#Detect Missing
    279  1.1  christos 	DONE_MSG="total menu item(s) were inserted into `pwd`/$DIR_FILE"
    280  1.1  christos 	for Info_Name in `comm -13 $TMP_FILE1 $TMP_FILE2`; do
    281  1.1  christos 		if test -r "$Info_Name"; then
    282  1.1  christos 			Info_File="$Info_Name"
    283  1.1  christos 		elif test -r "${Info_Name}.info"; then
    284  1.1  christos 			Info_File="${Info_Name}.info"
    285  1.1  christos 		elif test -r "${Info_Name}.gz"; then
    286  1.1  christos 			Info_File="${Info_Name}.gz"
    287  1.1  christos 		elif test -r "${Info_Name}.info.gz"; then
    288  1.1  christos 			Info_File="${Info_Name}.info.gz"
    289  1.1  christos 		else
    290  1.1  christos 			echo "$0:$LINENO: can't find info file for ${Info_Name}?">&2
    291  1.1  christos 			continue
    292  1.1  christos 		fi
    293  1.1  christos 		Changed="y"
    294  1.1  christos 		if install-info $Info_File $DIR_FILE; then
    295  1.1  christos 			Total=`expr "$Total" + "1"`
    296  1.1  christos 		fi
    297  1.1  christos 	done
    298  1.1  christos else
    299  1.1  christos 	# Detect Invalid
    300  1.1  christos 	DONE_MSG="total invalid menu item(s) were removed from `pwd`/$DIR_FILE"
    301  1.1  christos 	for Info_Name in `comm -23 $TMP_FILE1 $TMP_FILE2`; do
    302  1.1  christos 		Changed="y"
    303  1.1  christos 		if install-info --remove $Info_Name $DIR_FILE; then
    304  1.1  christos 			Total=`expr "$Total" + "1"`
    305  1.1  christos 		fi
    306  1.1  christos 	done
    307  1.1  christos fi
    308  1.1  christos 
    309  1.1  christos # print summary
    310  1.1  christos if test "$Changed"; then
    311  1.1  christos 	echo "$Total $DONE_MSG"
    312  1.1  christos else
    313  1.1  christos 	echo "Nothing to do"
    314  1.1  christos fi
    315  1.1  christos rm -f $TMP_FILE_LIST
    316  1.1  christos eval "$DEBUG"
    317  1.1  christos exit 0
    318