xref: /openbsd/gnu/usr.bin/texinfo/util/fix-info-dir (revision a1acfa9b)
11cc83814Sespie#!/bin/sh
21cc83814Sespie#fix-info-dir (GNU texinfo)
31cc83814SespieVERSION=1.1
4*a1acfa9bSespie#Copyright (C) 1998, 2003 Free Software Foundation, Inc.
51cc83814Sespie#fix-info-dir comes with NO WARRANTY, to the extent permitted by law.
61cc83814Sespie#You may redistribute copies of fix-info-dir
71cc83814Sespie#under the terms of the GNU General Public License.
81cc83814Sespie#For more information about these matters, see the files named COPYING."
91cc83814Sespie#fix-info-dir was derived from update-info and gen-dir-node
101cc83814Sespie# The skeleton file contains info topic names in the
111cc83814Sespie# order they should appear in the output.  There are three special
121cc83814Sespie# lines that alter the behavior: a line consisting of just "--" causes
131cc83814Sespie# the next line to be echoed verbatim to the output.  A line
141cc83814Sespie# containing just "%%" causes all the remaining filenames (wildcards
151cc83814Sespie# allowed) in the rest of the file to be ignored.  A line containing
161cc83814Sespie# just "!!" exits the script when reached (unless preceded by a line
171cc83814Sespie# containing just "--").
181cc83814Sespie#Author: Richard L. Hawes, rhawes@dmapub.dma.org.
191cc83814Sespie
201cc83814Sespie# ###SECTION 1### Constants
211cc83814Sespieset -h 2>/dev/null
221cc83814Sespie# ENVIRONMENT
231cc83814Sespieif test -z "$TMPDIR"; then
241cc83814Sespie	TMPDIR="/usr/tmp"
251cc83814Sespiefi
261cc83814Sespieif test -z "$LINENO"; then
271cc83814Sespie	LINENO="0"
281cc83814Sespiefi
291cc83814Sespie
301cc83814SespieMENU_BEGIN='^\*\([ 	]\)\{1,\}Menu:'
311cc83814SespieMENU_ITEM='^\* ([^ 	]).*:([ 	])+\('
321cc83814SespieMENU_FILTER1='s/^\*\([ 	]\)\{1,\}/* /'
331cc83814SespieMENU_FILTER2='s/\([ 	]\)\{1,\}$//g'
341cc83814Sespie
351cc83814SespieTMP_FILE1="${TMPDIR}/fx${$}.info"
361cc83814SespieTMP_FILE2="${TMPDIR}/fy${$}.info"
371cc83814SespieTMP_FILE_LIST="$TMP_FILE1 $TMP_FILE2"
381cc83814Sespie
391cc83814SespieTRY_HELP_MSG="Try --help for more information"
401cc83814Sespie
411cc83814Sespie# ###SECTION 100### main program
421cc83814Sespie#variables set by options
431cc83814SespieCREATE_NODE=""
441cc83814SespieDEBUG=":"
451cc83814SespieMODE=""
461cc83814Sespie#
471cc83814SespieTotal="0"
481cc83814SespieChanged=""
491cc83814Sespie
501cc83814Sespiewhile test "$*"; do
511cc83814Sespie	case "$1" in
521cc83814Sespie		-c|--create)    CREATE_NODE="y";;
531cc83814Sespie		--debug)	set -eux; DEBUG="set>&2";;
541cc83814Sespie		-d|--delete)	MODE="Detect_Invalid";;
551cc83814Sespie		+d);;
561cc83814Sespie		--version)
571cc83814Sespiecat<<VersionEOF
581cc83814Sespiefix-info-dir (GNU Texinfo) $VERSION
591cc83814SespieCopyright (C) 1998 Free Software Foundation, Inc.
601cc83814Sespiefix-info-dir comes with NO WARRANTY, to the extent permitted by law.
611cc83814SespieYou may redistribute copies of fix-info-dir
621cc83814Sespieunder the terms of the GNU General Public License.
631cc83814SespieFor more information about these matters, see the files named COPYING.
641cc83814SespieAuthor: Richard L. Hawes
651cc83814SespieVersionEOF
661cc83814Sespie		exit;;
671cc83814Sespie
681cc83814Sespie		--help)
691cc83814Sespiecat<<HelpEndOfFile
701cc83814SespieUsage:	fix-info-dir  [OPTION]... [INFO_DIR/[DIR_FILE]] [SKELETON]
711cc83814Sespie
721cc83814SespieIt detects and inserts missing menu items into the info dir file.
731cc83814SespieThe info dir must be the current directory.
741cc83814Sespie
751cc83814SespieOptions:
761cc83814Sespie-c,	--create	create a new info node
771cc83814Sespie-d,	--delete	delete invalid menu items (ignore missing menu items)
781cc83814Sespie	--debug		print debug information to standard error path
791cc83814Sespie	--help		print this help message and exit
801cc83814Sespie	--version	print current version and exit
811cc83814SespieBackup of the info node has a '.old' suffix added.  This is a shell script.
821cc83814SespieEnvironment Variables: TMPDIR
831cc83814SespieEmail bug reports to bug-texinfo@gnu.org.
841cc83814SespieHelpEndOfFile
851cc83814Sespie		exit;;
861cc83814Sespie
871cc83814Sespie		[-+]*)	echo "$0:$LINENO: \"$1\" is not a valid option">&2
881cc83814Sespie			echo "$TRY_HELP_MSG">&2
891cc83814Sespie			exit 2;;
901cc83814Sespie		*) break;;
911cc83814Sespie	esac
921cc83814Sespie	shift
931cc83814Sespiedone
941cc83814Sespie
951cc83814SespieORIGINAL_DIR=`pwd`
961cc83814Sespie
971cc83814Sespieif test "$#" -gt "0"; then
981cc83814Sespie	INFO_DIR="$1"
991cc83814Sespie	shift
1001cc83814Sespieelse
1011cc83814Sespie	INFO_DIR=$DEFAULT_INFO_DIR
1021cc83814Sespiefi
1031cc83814Sespie
1041cc83814Sespieif test ! -d "${INFO_DIR}"; then
1051cc83814Sespie	DIR_FILE=`basename ${INFO_DIR}`;
1061cc83814Sespie	INFO_DIR=`dirname ${INFO_DIR}`;
1071cc83814Sespieelse
1081cc83814Sespie	DIR_FILE="dir"
1091cc83814Sespiefi
1101cc83814Sespie
1111cc83814Sespiecd "$INFO_DIR"||exit
1121cc83814Sespie
1131cc83814Sespie
1141cc83814Sespieif test "$CREATE_NODE"; then
1151cc83814Sespie	if test "$#" -gt "0"; then
1161cc83814Sespie		if test `expr $1 : /` = '1'; then
1171cc83814Sespie			SKELETON="$1"
1181cc83814Sespie		else
1191cc83814Sespie			SKELETON="$ORIGINAL_DIR/$1"
1201cc83814Sespie		fi
121*a1acfa9bSespie		if test ! -r "$SKELETON" && test -f "$SKELETON"; then
1221cc83814Sespie			echo "$0:$LINENO: $SKELETON is not readable">&2
1231cc83814Sespie			exit 2
1241cc83814Sespie		fi
1251cc83814Sespie		shift
1261cc83814Sespie	else
1271cc83814Sespie		SKELETON=/dev/null
1281cc83814Sespie
1291cc83814Sespie	fi
1301cc83814Sespieelse
1311cc83814Sespie	if test ! -f "$DIR_FILE"; then
1321cc83814Sespie		echo "$0:$LINENO: $DIR_FILE is irregular or nonexistant">&2
1331cc83814Sespie		exit 2
1341cc83814Sespie	elif test ! -r "$DIR_FILE"; then
1351cc83814Sespie		echo "$0:$LINENO: $DIR_FILE is not readable">&2
1361cc83814Sespie		exit 2
1371cc83814Sespie	elif test ! -w "$DIR_FILE"; then
1381cc83814Sespie		echo "$0:$LINENO: $DIR_FILE is not writeable">&2
1391cc83814Sespie		exit 2
1401cc83814Sespie	fi
1411cc83814Sespiefi
1421cc83814Sespie
1431cc83814Sespieif test "$#" -gt "0"; then
1441cc83814Sespie	echo "$0:$LINENO: Too many parameters">&2
1451cc83814Sespie	echo "$TRY_HELP_MSG">&2
1461cc83814Sespie	exit 2
1471cc83814Sespiefi
1481cc83814Sespie
1491cc83814Sespieif test -f "$DIR_FILE"; then
1501cc83814Sespie	cp "$DIR_FILE" "$DIR_FILE.old"
1511cc83814Sespie	echo "Backed up $DIR_FILE to $DIR_FILE.old."
1521cc83814Sespiefi
1531cc83814Sespie
1541cc83814Sespieif test "$CREATE_NODE"; then
1551cc83814Sespie	if test "$MODE"; then
1561cc83814Sespie		echo "$0:$LINENO: ERROR: Illogical option combination: -d -c">&2
1571cc83814Sespie		echo "$TRY_HELP_MSG">&2
1581cc83814Sespie		exit 2
1591cc83814Sespie	fi
1601cc83814Sespie	echo "Creating new Info Node: `pwd`/$DIR_FILE"
1611cc83814Sespie	Changed="y"
1621cc83814Sespie
1631cc83814Sespie{
1641cc83814Sespie
1651cc83814Sespie	### output the dir header
1661cc83814Sespie	echo "-*- Text -*-"
1671cc83814Sespie	echo "This file was generated automatically by $0."
1681cc83814Sespie	echo "This version was generated on `date`"
1691cc83814Sespie	echo "by `whoami`@`hostname` for `pwd`"
1701cc83814Sespie
1711cc83814Sespie	cat<<DIR_FILE_END_OF_FILE
1721cc83814SespieThis is the file .../info/$DIR_FILE, which contains the topmost node of the
1731cc83814SespieInfo hierarchy.  The first time you invoke Info you start off
1741cc83814Sespielooking at that node, which is ($DIR_FILE)Top.
1751cc83814Sespie
1761cc83814Sespie
1771cc83814SespieFile: $DIR_FILE       Node: Top       This is the top of the INFO tree
1781cc83814Sespie
1791cc83814Sespie  This (the Directory node) gives a menu of major topics.
1801cc83814Sespie  Typing "q" exits, "?" lists all Info commands, "d" returns here,
1811cc83814Sespie  "h" gives a primer for first-timers,
1821cc83814Sespie  "mEmacs<Return>" visits the Emacs topic, etc.
1831cc83814Sespie
1841cc83814Sespie  In Emacs, you can click mouse button 2 on a menu item or cross reference
1851cc83814Sespie  to select it.
1861cc83814Sespie
1871cc83814Sespie* Menu: The list of major topics begins on the next line.
1881cc83814Sespie
1891cc83814SespieDIR_FILE_END_OF_FILE
1901cc83814Sespie
1911cc83814Sespie### go through the list of files in the skeleton.  If an info file
1921cc83814Sespie### exists, grab the ENTRY information from it.  If an entry exists
1931cc83814Sespie### use it, otherwise create a minimal $DIR_FILE entry.
1941cc83814Sespie
1951cc83814Sespie	# Read one line from the file.  This is so that we can echo lines with
1961cc83814Sespie	# whitespace and quoted characters in them.
1971cc83814Sespie	while read fileline; do
1981cc83814Sespie		# flag fancy features
1991cc83814Sespie		if test ! -z "$echoline"; then        # echo line
2001cc83814Sespie			echo "$fileline"
2011cc83814Sespie			echoline=""
2021cc83814Sespie			continue
2031cc83814Sespie		elif test "${fileline}" = "--"; then
2041cc83814Sespie			# echo the next line
2051cc83814Sespie			echoline="1"
2061cc83814Sespie			continue
2071cc83814Sespie		elif test "${fileline}" = "%%"; then
2081cc83814Sespie			# skip remaining files listed in skeleton file
2091cc83814Sespie			skip="1"
2101cc83814Sespie			continue
2111cc83814Sespie		elif test "${fileline}" = "!!"; then
2121cc83814Sespie			# quit now
2131cc83814Sespie			break
2141cc83814Sespie		fi
2151cc83814Sespie
2161cc83814Sespie		# handle files if they exist
2171cc83814Sespie		for file in $fileline""; do
2181cc83814Sespie			fname=
2191cc83814Sespie			if test -z "$file"; then
2201cc83814Sespie				break
2211cc83814Sespie			fi
2221cc83814Sespie			# Find the file to operate upon.
2231cc83814Sespie			if test -r "$file"; then
2241cc83814Sespie				fname="$file"
2251cc83814Sespie			elif test -r "${file}.info"; then
2261cc83814Sespie				fname="${file}.info"
2271cc83814Sespie			elif test -r "${file}.gz"; then
2281cc83814Sespie				fname="${file}.gz"
2291cc83814Sespie			elif test -r "${file}.info.gz"; then
2301cc83814Sespie				fname="${file}.info.gz"
2311cc83814Sespie			else
2321cc83814Sespie				echo "$0:$LINENO: can't find info file for ${file}?">&2
2331cc83814Sespie				continue
2341cc83814Sespie			fi
2351cc83814Sespie
2361cc83814Sespie			# if we found something and aren't skipping, do the entry
2371cc83814Sespie			if test "$skip"; then
2381cc83814Sespie				continue
2391cc83814Sespie			fi
2401cc83814Sespie
2411cc83814Sespie			infoname=`echo $file|sed -e 's/.info$//'`
2421cc83814Sespie			entry=`zcat -f $fname|\
2431cc83814Sespie			sed -e '1,/START-INFO-DIR-ENTRY/d'\
2441cc83814Sespie			-e '/END-INFO-DIR-ENTRY/,$d'`
2451cc83814Sespie			if [ ! -z "${entry}" ]; then
2461cc83814Sespie				echo "${entry}"
2471cc83814Sespie			else
2481cc83814Sespie				echo "* ${infoname}: (${infoname})."
2491cc83814Sespie			fi
2501cc83814Sespie			Total=`expr "$Total" + "1"`
2511cc83814Sespie		done
2521cc83814Sespie	done
2531cc83814Sespie}>$DIR_FILE<$SKELETON
2541cc83814Sespiefi
2551cc83814Sespie
2561cc83814Sespietrap ' eval "$DEBUG"; rm -f $TMP_FILE_LIST; exit ' 0
2571cc83814Sespietrap ' rm -f $TMP_FILE_LIST
2581cc83814Sespie	exit ' 1
2591cc83814Sespietrap ' rm -f $TMP_FILE_LIST
2601cc83814Sespie	echo "$0:$LINENO: received INT signal.">&2
2611cc83814Sespie	exit ' 2
2621cc83814Sespietrap ' rm -f $TMP_FILE_LIST
2631cc83814Sespie	echo "$0:$LINENO: received QUIT signal.">&2
2641cc83814Sespie	exit ' 3
2651cc83814Sespie
2661cc83814Sespiesed -e "1,/$MENU_BEGIN/d" -e "$MENU_FILTER1" -e "$MENU_FILTER2"<$DIR_FILE\
2671cc83814Sespie|sed -n -e '/\* /{
2681cc83814Sespies/).*$//g
2691cc83814Sespies/\.gz$//
2701cc83814Sespies/\.info$//
2711cc83814Sespies/^.*(//p
2721cc83814Sespie}'|sort -u>$TMP_FILE1
2731cc83814Sespiels -F|sed -e '/\/$/d' -e '/[-.][0-9]/d'\
2741cc83814Sespie	-e "/^$DIR_FILE\$/d" -e "/^$DIR_FILE.old\$/d"\
2751cc83814Sespie	-e 's/[*@]$//' -e 's/\.gz$//' -e 's/\.info$//'|sort>$TMP_FILE2
2761cc83814Sespie
2771cc83814Sespieif test -z "$MODE"; then
2781cc83814Sespie	#Detect Missing
2791cc83814Sespie	DONE_MSG="total menu item(s) were inserted into `pwd`/$DIR_FILE"
2801cc83814Sespie	for Info_Name in `comm -13 $TMP_FILE1 $TMP_FILE2`; do
2811cc83814Sespie		if test -r "$Info_Name"; then
2821cc83814Sespie			Info_File="$Info_Name"
2831cc83814Sespie		elif test -r "${Info_Name}.info"; then
2841cc83814Sespie			Info_File="${Info_Name}.info"
2851cc83814Sespie		elif test -r "${Info_Name}.gz"; then
2861cc83814Sespie			Info_File="${Info_Name}.gz"
2871cc83814Sespie		elif test -r "${Info_Name}.info.gz"; then
2881cc83814Sespie			Info_File="${Info_Name}.info.gz"
2891cc83814Sespie		else
2901cc83814Sespie			echo "$0:$LINENO: can't find info file for ${Info_Name}?">&2
2911cc83814Sespie			continue
2921cc83814Sespie		fi
2931cc83814Sespie		Changed="y"
2941cc83814Sespie		if install-info $Info_File $DIR_FILE; then
2951cc83814Sespie			Total=`expr "$Total" + "1"`
2961cc83814Sespie		fi
2971cc83814Sespie	done
2981cc83814Sespieelse
2991cc83814Sespie	# Detect Invalid
3001cc83814Sespie	DONE_MSG="total invalid menu item(s) were removed from `pwd`/$DIR_FILE"
3011cc83814Sespie	for Info_Name in `comm -23 $TMP_FILE1 $TMP_FILE2`; do
3021cc83814Sespie		Changed="y"
3031cc83814Sespie		if install-info --remove $Info_Name $DIR_FILE; then
3041cc83814Sespie			Total=`expr "$Total" + "1"`
3051cc83814Sespie		fi
3061cc83814Sespie	done
3071cc83814Sespiefi
3081cc83814Sespie
3091cc83814Sespie# print summary
3101cc83814Sespieif test "$Changed"; then
3111cc83814Sespie	echo "$Total $DONE_MSG"
3121cc83814Sespieelse
3131cc83814Sespie	echo "Nothing to do"
3141cc83814Sespiefi
3151cc83814Sespierm -f $TMP_FILE_LIST
3161cc83814Sespieeval "$DEBUG"
3171cc83814Sespieexit 0
318