#!/bin/sh
# update-info -- update dir file from all extant info pages.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, you can either send email to this
# program's maintainer or write to: The Free Software Foundation,
# Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
# 
# Author: rhawes@dmapub.dma.org.  Please report bugs to him.
# 
# run this program to install update-info
# ###SECTION 0### install script
# These constants set the version numbers for both files:
PROGRAM_VERSION="1.4"
PACKAGE_VERSION="4.0"

# ENVIRONMENT
if test -z "$TMPDIR"; then
	TMPDIR="/usr/tmp"
fi

TMP_SED="$TMPDIR/uss$$.info"
TMP_F_ADD_SECTION="$TMPDIR/ufa$$.info"
TMP_F_DELETE_INVALID="$TMPDIR/ufd$$.info"
TMP_F_INSERT_MISSING="$TMPDIR/ufi$$.info"
TMP_FILES="$TMP_SED $TMP_F_ADD_SECTION $TMP_F_DELETE_INVALID\
	$TMP_F_INSERT_MISSING"

trap 'rm -f $TMP_FILES' 0

# file boundaries
UPDATE_INFO="/^# _file: 'update-info'_/"
UPDATE_INFO_F="/^# _file: 'update-info.f'_/"

# @F_ADD_SECTION@
echo 'Item_Num=`expr "$Item_Num" + "1"`
echo "$1">>"$TMP_SECTIONS"
if test "$Item_Status"; then
	Item_Status=`echo "${Item_Status}
X"`
else
	Item_Status="X"
fi '>$TMP_F_ADD_SECTION

# @F_INSERT_MISSING@
echo 'if test -z "$Create_Node"; then
	rm -f ${Info_Node}.old
	cp $Info_Node ${Info_Node}.old
	echo "$BACKUP_MSG"
fi
echo "/$MENU_BEGIN/
+,$ d
r $TMP_MENU
w
q"|ed -s $Info_Node>/dev/null'>$TMP_F_INSERT_MISSING

# @F_DELETE_INVALID@
echo '
rm -f ${Info_Node}.old
cp $Info_Node ${Info_Node}.old
echo "$BACKUP_MSG"
echo "/$MENU_BEGIN/
+,$ d
w
q"|ed -s $Info_Node>/dev/null
sed -f "$TMP_SED" "$TMP_MENU">>"$Info_Node"'>$TMP_F_DELETE_INVALID

cat<<Sed_Script_EOF>$TMP_SED
s/@UPDATE_INFO_VERSION@/$PROGRAM_VERSION/g
s/@TEXINFO_VERSION@/$PACKAGE_VERSION/g
s/@SET_ITEM@/Item_Status=\`echo "\$Item_Status"|sed -e "\${1}s%^.*%\${2}%"\`/
/@F_ADD_SECTION@/r $TMP_F_ADD_SECTION
/@F_ADD_SECTION@/d
/@F_DELETE_INVALID@/r $TMP_F_DELETE_INVALID
/@F_DELETE_INVALID@/d
/@F_INSERT_MISSING@/r $TMP_F_INSERT_MISSING
/@F_INSERT_MISSING@/d
Sed_Script_EOF

sed -e "1,${UPDATE_INFO}d" -e "$UPDATE_INFO_F,\$d" -f $TMP_SED $0>update-info
sed -e "1,${UPDATE_INFO_F}d" -f $TMP_SED $0>update-info.f
chmod +x update-info update-info.f
echo "installed update-info, and update-info.f into `pwd`"
rm -f $TMP_FILES
exit

# _file: 'update-info'_
#!/bin/sh
#update-info (GNU texinfo @TEXINFO_VERSION@) @UPDATE_INFO_VERSION@
#Copyright (C) 1997 Free Software Foundation, Inc.
#update-info comes with NO WARRANTY, to the extent permitted by law.
#You may redistribute copies of update-info
#under the terms of the GNU General Public License.
#For more information about these matters, see the files named COPYING."
#Author: Richard L. Hawes

# ###SECTION 1### Constants
set -h 2>/dev/null
# ENVIRONMENT
if test -z "$TMPDIR"; then
	TMPDIR="/usr/tmp"
fi
if test -z "$LINES"; then
	LINES=24
fi
if test -z "$COLUMNS"; then
	COLUMNS=80
fi
if test -z "$EDITOR"; then
	EDITOR=vi
fi
if test -z "$LINENO"; then
	LINENO="0"
fi
# constants redefined by update-info.f
PROMPT1="(y=yes, Y=yes to all, n=no, N=No to all):"
FUNCTIONS=""
#
ARGUMENTS="$*"
DISPLAY_NUM=`expr "$LINES" - 4`
CONTROL_D="{Ctrl-D}"
DIR_SECTION="^INFO-DIR-SECTION"
ENTRY_END="^END-INFO-DIR-ENTRY"
ENTRY_START="^START-INFO-DIR-ENTRY"
MENU_BEGIN='^\*\([ 	]\)\{1,\}Menu:'
MENU_ITEM='^\* ([^ 	]).*:([ 	])+\('
SECTION_TITLE="^[A-Za-z0-9]"
MENU_FILTER1='s/^\*\([ 	]\)\{1,\}/* /'
MENU_FILTER2='s/\([ 	]\)\{1,\}$//g'
TMP_ITEM="${TMPDIR}/ui${$}.info"
TMP_LIST="${TMPDIR}/ul${$}.info"
TMP_MENU="${TMPDIR}/um${$}.info"
TMP_SECTIONS="${TMPDIR}/us${$}.info" # used only in Detect_Missing
TMP_SED="$TMP_SECTIONS" # used only in Detect_Invalid routines
TMP_FILE1="${TMPDIR}/ux${$}.info"
TMP_FILE2="${TMPDIR}/uy${$}.info"
TMP_COUNT="$TMP_FILE2"
TMP_FILE_LIST="$TMP_LIST $TMP_MENU $TMP_SECTIONS $TMP_FILE1 $TMP_FILE2\
	$TMP_ITEM"
TRY_HELP_MSG="Try --help for more information"
if zcat --version 2>/dev/null>/dev/null; then
	CAT_COMMAND="zcat -f"
else
	echo "$0:$LINENO: GNU zcat not found">&2
	CAT_COMMAND="cat"
fi

# ###SECTION 100### main program
#variables set by options
Create_Node=""
Debug=":"
Interactive=""
Load_Functions="y"
Mode=""
#
Inserts="0"
Inserts_Total="0"
Invalid="0"
Invalid_Total="0"
Changed=""

while test "$*"
	do
	case "$1" in
		-c)             Create_Node="y";;
		-ci|-ic)        Create_Node="y"; Interactive="y";;
		-cif|-cfi|-ifc|-icf|-fci|-fic) Create_Node="y"
				Interactive="y"; Load_Functions="";;
		--debug)	set -eux; Debug="set>&2";;
		-d|--delete)	Mode="Detect_Invalid";;
		-f)		Load_Functions="";;
		-i|--interactive) Interactive="y";;
		-fi|-if)	Load_Functions=""; Interactive="y";;
		-id|-di)	Mode="Detect_Invalid"; Interactive="y";;
		+i|+d|+f);;
		--version)
cat<<VersionEOF
update-info (GNU texinfo @TEXINFO_VERSION@) @UPDATE_INFO_VERSION@
Copyright (C) 1997 Free Software Foundation, Inc.
update-info comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of update-info
under the terms of the GNU General Public License.
For more information about these matters, see the files named COPYING.
Author: Richard L. Hawes
VersionEOF
		exit;;

		--help)
cat<<HelpEndOfFile
Usage:	update-info  [OPTION]... INFO_PATH/INFO_DIR_FILE

It detects and inserts missing menu items into the info node file.

Options:
-c			create a new info node
	--debug		print debug information to standard error path
-d,	--delete	delete invalid menu items (ignore missing menu items)
-f			do not load functions (file update-info.f)
	--help		print this help message and exit
-i,	--interactive	interactive mode prompts before inserting or removing
			menu items
	--version	print current version and exit
Backup of the info node has a '.old' suffix added.  This is a shell script.
Files: update-info.f -- contains functions (optional).
Environment Variables: COLUMNS, EDITOR, LINES, TMPDIR
Email bug reports to bug-texinfo@gnu.org.
HelpEndOfFile
		exit;;

		[-+]*)	echo "$0:$LINENO: \"$1\" is not a valid option">&2
			echo "$TRY_HELP_MSG">&2
			exit 2;;
		*) break;;
	esac
	shift
	done

if test "$#" -lt "1"; then
	echo "$0:$LINENO: Too few parameters">&2
	echo "$TRY_HELP_MSG">&2
	exit 2
elif test "$#" -gt "1"; then
	echo "$0:$LINENO: Too many parameters">&2
	echo "$TRY_HELP_MSG">&2
	exit 2
fi
Info_Path="$1"
Info_Node=`basename "$Info_Path"`
if echo "$Info_Node"|grep ".*dir$">/dev/null; then
	:
else
	echo "$0:$LINENO: $Info_Node is not a valid info node name">&2
	exit 2
fi
Info_Pathname=`dirname "$Info_Path"`
cd "$Info_Pathname"||exit

BACKUP_MSG="Backed up $Info_Node to ${Info_Node}.old."
HANGUP_MSG="Hang up on \"update-info $ARGUMENTS\""
INSERT_MSG="menu item(s) were inserted (not counting duplicates)."
INSERT_MSG2="total menu item(s) were inserted into `pwd`/$Info_Node"
DELETE_MSG="invalid menu item(s) were removed (not counting duplicates)."
DELETE_MSG2="total invalid menu item(s) were removed from `pwd`/$Info_Node"

if test "$Create_Node"; then
	if test "$Mode"; then
		echo "$0:$LINENO: ERROR: Illogical option combination: -d -c">&2
		echo "$TRY_HELP_MSG">&2
		exit 2
	fi
	if test -f "$Info_Node"; then
		rm -f ${Info_Node}.old
		mv "$Info_Node" "${Info_Node}.old"
		echo "$BACKUP_MSG"
	fi
	echo "Creating new Info Node: `pwd`/$Info_Node"
cat>$Info_Node<<NodeEndOfFile||exit

This is the file .../info/dir, which contains the topmost node of the
Info hierarchy.  The first time you invoke Info you start off
looking at that node, which is (dir)Top.

File: dir	Node: Top	This is the top of the INFO tree
  This (the Directory node) gives a menu of major topics.
  Typing "d" returns here, "q" exits, "?" lists all INFO commands, "h"
  gives a primer for first-timers, "mTexinfo<Return>" visits Texinfo topic,
  etc.
  Or click mouse button 2 on a menu item or cross reference to select it.
  --- PLEASE ADD DOCUMENTATION TO THIS TREE. (See INFO topic first.) ---

* Menu: The list of major topics begins on the next line.

NodeEndOfFile
else
	if test ! -f "$Info_Node"; then
		echo "$0:$LINENO: $Info_Node is irregular or nonexistant">&2
		exit 2
	elif test ! -r "$Info_Node"; then
		echo "$0:$LINENO: $Info_Node is not readable">&2
		exit 2
	elif test ! -w "$Info_Node"; then
		echo "$0:$LINENO: $Info_Node is not writeable">&2
		exit 2
	fi
fi

if test "$Load_Functions" -a "$Interactive" -a -z "$Mode"; then
	if FUNCTIONS_VERSION=`( update-info.f )`; then
		if test `echo "$FUNCTIONS_VERSION"\
		|cut -d' ' -f5` = "@UPDATE_INFO_VERSION@"; then
			echo "Loading functions..."
			. update-info.f
		else
			echo "$0:$LINENO: wrong version of update-info.f">&2
			echo "(functions were not loaded)">&2
		fi
	else
		echo "(functions were not loaded)">&2
	fi
fi


trap ' eval "$Debug"; rm -f $TMP_FILE_LIST; exit ' 0
if test "$Interactive"; then
	if test ! -t "1"; then
		echo "$0:$LINENO: Cannot run in interactive mode  "\
		"standard out is redirected">&2
		exit 2
	fi
	trap ' ' 2 3
else
	trap ' rm -f $TMP_FILE_LIST
	echo "$0:$LINENO: received INT signal.  All edits are canceled.">&2
	exit ' 2
	trap ' rm -f $TMP_FILE_LIST
	echo "$0:$LINENO: received QUIT signal.  All edits are canceled.">&2
	exit ' 3
fi
if test -z "$Mode"; then
	trap '
	if test "$Changed"; then
		{
		echo $HANGUP_MSG
		@F_INSERT_MISSING@
		Inserts_Total=`wc -c<"$TMP_COUNT"`
		echo $Inserts_Total $INSERT_MSG2
		}|mail "$LOGNAME"
	fi
	rm -f $TMP_FILE_LIST
	exit ' 1
else
	trap '
	if test "$Changed"; then
		{
		echo $HANGUP_MSG
		@F_DELETE_INVALID@
		Invalid_Total=`wc -l<"$TMP_SED"`
		echo $Invalid_Total $DELETE_MSG2
		}|mail $LOGNAME
	fi
	rm -f $TMP_FILE_LIST
	exit ' 1
fi

sed -e "1,/$MENU_BEGIN/d" -e "$MENU_FILTER1" -e "$MENU_FILTER2"<$Info_Node\
|tee $TMP_MENU\
|sed -n -e '/\* /{
s/).*$//g
s/\.gz$//
s/\.info$//
s/^.*(//p
}'|sort -u>$TMP_FILE1
ls -F|sed -e '/\/$/d' -e '/[-.][0-9]/d'\
	-e '/:$/d' -e '/^$/d' -e "/^${Info_Node}~\$/d"\
	-e "/^${Info_Node}\$/d" -e "/^${Info_Node}.old\$/d"\
	-e 's/\.gz$//' -e 's/\.info$//'|sort>$TMP_FILE2

if test -z "$Mode"; then
	#Detect Missing
	comm -13 $TMP_FILE1 $TMP_FILE2>$TMP_LIST
	cat</dev/null>$TMP_COUNT
	#get sections, initialize variables
	sed -n -e "/$SECTION_TITLE/p" "$TMP_MENU">"$TMP_SECTIONS"
	Item_Num=`wc -l<"$TMP_SECTIONS"|tr -d ' '`
	Item_Status=`echo\
	|awk "BEGIN{for(i=1;i<=${Item_Num};i++)printf(\"_\n\")}"`
	Item_Dir="$Item_Num"
	for Info_Name in `cat $TMP_LIST`
		do
		if test -r "$Info_Name"; then
			Info_File="$Info_Name"
		elif test -r "${Info_Name}.info"; then
			Info_File="${Info_Name}.info"
		elif test -r "${Info_Name}.gz"; then
			Info_File="${Info_Name}.gz"
		elif test -r "${Info_Name}.info.gz"; then
			Info_File="${Info_Name}.info.gz"
		else
			echo "$0:$LINENO: can't find info file for ${Info_Name}?">&2
		fi
		#generate menu item
		echo|tr -d '\012'>$TMP_FILE1
		eval $CAT_COMMAND "$Info_File"\
		|sed -n -e "/$DIR_SECTION/w $TMP_FILE1"\
		-e "/$ENTRY_START/,/$ENTRY_END/{
		$MENU_FILTER1
		p
		}"|awk "BEGIN{Mode=0}
		/^$/{if(Mode==1)exit}
		/^([ 	])+([^ 	])+/{if(Mode==1)print}
		/^[^ 	]/{if(Mode==1)exit}
		/${MENU_ITEM}${Info_Name}\)\./{if(Mode==0){Mode++
				print}
			else
				exit}">"$TMP_ITEM"
		if test ! -s "$TMP_ITEM"; then
			echo "* $Info_Name: ($Info_Name).">"$TMP_ITEM"
		fi
		Item_Status=`echo "$Item_Status"|sed -e '1,$s/^./_/'`
		if test -s "$TMP_FILE1"; then
			Item_Section=`sed -e "s/$DIR_SECTION[ 	]*//"\
			<$TMP_FILE1`
		else
			Item_Section=`echo "Miscellaneous"`
		fi
		Size=`echo "$Item_Section"|wc -l|tr -d ' '`
		# initialize variables, check for new sections
		Num1=1
		while test "$Num1" -le "$Size"
			do
			Item=`echo "$Item_Section"|sed -n -e "${Num1}p"`
			if Num=`grep -in "^$Item$" "$TMP_SECTIONS"`; then
				Num=`echo "$Num"|sed -e 's/:.*$//g'`
				##F#Set_Item
				set "$Num" "X"
				@SET_ITEM@
			else
				set "$Item"
				@F_ADD_SECTION@
			fi
			Num1=`expr "$Num1" + "1"`
			done
		if test "$Interactive"; then
			echo "$Item_Section"
			cat "$TMP_ITEM"
			echo "add menu item for $Info_File? "
			while true
				do
				echo "$PROMPT1"|tr -d '\012'
				read Answer
				case $Answer in
					y) break;;
					e)
					if test "$FUNCTIONS"; then
						Select_Sections
						break
					else
						echo "Can't edit.  "\
						"Functions are not loaded.">&2
					fi;;
					Y) Interactive=""; break;;
					n) continue 2;;
					N) break 2;;
					*) echo "\"$Answer\" "\
					"is an invalid response">&2;;
					esac
				done
		fi
		if echo "$Item_Status"|grep '^X'>/dev/null; then
			# edit $TMP_MENU

			Changed="y"
			(
			trap ' ' 1 2 3
			Tmp_Var=`echo "$Item_Status"|tr -d '\012'`
			Key=`awk -F':' ' FNR==1{ print $1}' $TMP_ITEM`
			# add new sections to 'dir' file
			if test "$Item_Num" -gt "$Item_Dir"; then
				if test "$Item_Dir" -ne "0"; then
					sed -e "1,${Item_Dir}d" -e 'i\

' "$TMP_SECTIONS">>"$TMP_MENU"
				else
					sed -e 'i\

' "$TMP_SECTIONS">>"$TMP_MENU"
				fi
			fi
			# awk determines the insertion points for each section
			awk -F":" "function Insert(Line){
			if(Mode==2){
				Mode=1;if(substr(\"$Tmp_Var\",Item++,1)==\"X\")
					print Line
				}
			}
			BEGIN{Mode=1;Item=1}
			/$SECTION_TITLE/{Insert(FNR-1);if(Mode>=1)Mode=2}
			/${MENU_ITEM}.*\)\./{if(\$1>Item_Name)Insert(FNR-1)}
			/^$/{Insert(FNR-1)}
			END{Insert(FNR)}" Item_Name="$Key" "$TMP_MENU"\
			|sort -nr|sed -e "s%\$% r $TMP_ITEM%"|sed -e '$a\
w
' -e '$a\
q
'|ed -s "$TMP_MENU"
			echo "$Item_Status"|tr -cd "X">>$TMP_COUNT
			)

			Inserts=`expr "$Inserts" + "1"`
			echo "$Info_File installed into section(s):"\
			|tr -d '\012'
			echo "$Item_Status"|awk '/X/{printf(" %d", FNR)}'
			echo
			Item_Dir="$Item_Num"
		else
			echo "$Info_File not installed (no section selected)"
		fi
		done
	# print summary
	trap ' ' 1 2 3
	if test "$Changed"; then
		@F_INSERT_MISSING@
		Inserts_Total=`wc -c<"$TMP_COUNT"|tr -d " "`
		if test "$Inserts" -ne "$Inserts_Total"; then
			echo "$Inserts $INSERT_MSG"
		fi
		echo "$Inserts_Total $INSERT_MSG2"
	fi
else
	# Detect Invalid
	cat</dev/null>"$TMP_SED"
	comm -23 $TMP_FILE1 $TMP_FILE2>$TMP_LIST
	for Info_Name in `cat $TMP_LIST`
		do
		if test "$Interactive"; then
			# display invalid menu item(s)
			awk "BEGIN{Mode=1}
			/^([ 	])+([^ 	])+/{if(Mode==2)print}
			/^$/{if(Mode==2)Mode=1}
			/$SECTION_TITLE/{Section=\$0}
			/^[^ 	]/{if(Mode==2)Mode=1}
			/${MENU_ITEM}${Info_Name}\)\./{if(Mode==1){Mode++
				print Section
				print}}" $TMP_MENU
			echo "delete menu item for $Info_Name? "
			while true
				do
				echo\
				"(y=yes, n=no, Y=yes to all, N=No to all):"\
				|tr -d '\012'
				read Answer
				case "$Answer" in
					y) break;;
					Y) Interactive=""; break;;
					n) continue 2;;
					N) break 2;;
					*) echo "\"$Answer\" "\
					"is an invalid reponse">&2;;
					esac
				done
		fi
		# remove menu item from $TMP_MENU
		Invalid=`expr "$Invalid" + "1"`
		Changed="y"
		(
		trap ' ' 1 2 3
		echo\
		"invalid menu item for $Info_Name removed from section(s):"\
		|tr -d '\012'
		awk "function Delete(Last){
		printf(\"%d,%dd\n\",First,Last-1)>>\"$TMP_SED\"}
		BEGIN{Mode=1;Section=0}
		/^$/{if(Mode==2){Delete(FNR);Mode=1}}
		/$SECTION_TITLE/{Section++}
		/^[^ 	]/{if(Mode==2){Delete(FNR);Mode=1}}
		/${MENU_ITEM}${Info_Name}\)\./{if(Mode==1){
			First=FNR;printf(\" %d\",Section);Mode=2}}
		END{if(Mode==2)Delete(FNR+1)}" $TMP_MENU
		echo
		)
		done

	# display a summary
	trap ' ' 1 2 3
	if test "$Changed"; then
		Invalid_Total=`wc -l<"$TMP_SED"|tr -d ' '`
		@F_DELETE_INVALID@
		if test "$Invalid" -ne "$Invalid_Total"; then
			echo "$Invalid $DELETE_MSG"
		fi
		echo "$Invalid_Total $DELETE_MSG2"
	fi
fi
if test -z "$Changed"; then
	echo "Nothing to do"
fi
rm -f $TMP_FILE_LIST
eval "$Debug"
exit 0
# _file: 'update-info.f'_
#update-info.f (GNU texinfo @TEXINFO_VERSION@) @UPDATE_INFO_VERSION@
#Copyright (C) 1997 Free Software Foundation, Inc.
#update-info comes with NO WARRANTY, to the extent permitted by law.
#You may redistribute copies of update-info
#under the terms of the GNU General Public License.
#For more information about these matters, see the files named COPYING."
#Author: Richard L. Hawes

# update-info.f functions for update-info

# ###SECTION 1### functions used to insert missing menu items

Set_Item (){
# set item status
@SET_ITEM@
}

Add_Section (){
# add a section
@F_ADD_SECTION@
}

# ###SECTION 2### functions for menu selection of sections

Print (){
# print a line without a linefeed
echo "$*"|tr -d '\012'
}

Get_Answer (){
# get an answer to question
_gs_Valid="$1"
_gs_Prompt="$2"
set -f
Answer=""
while test -z "$Answer"
	do
	Print "$_gs_Prompt"
	if read Answer; then
		:
	else
		Answer="$CONTROL_D"
		echo
	fi
	if expr "$Answer" : "[$_gs_Valid]$">/dev/null; then
		:
	else
		Print "\"$Answer\" is not a valid response! --">&2
		Answer=""
	fi
	done
set +f
}

Do_Previous (){
# go to previous screen
if test "$Previous"; then
	Next="$Top_Item"
	Top_Item="$Previous"
	if Previous=`expr "$Top_Item" - "$DISPLAY_NUM"`; then
		if test "$Previous" -le "0"; then
			Previous=""
		fi
	else
		Previous=""
	fi
elif test "$Next"; then
	Last_Page
else
	Print "There is no previous page. ">&2
fi
}

Do_Next (){
# process go to next
if test "$Next"; then
	Previous="$Top_Item"
	Top_Item="$Next"
	Set_Next
elif test "$Previous"; then
	Top_Item="1"
	Previous=""
	Set_Next
else
	Print "There is no next page. ">&2
fi
}

Do_Add_Section (){
# process add section command
echo
echo "Please enter the name of the new section:"
if read Answer; then
	Answer=`echo "$Answer"\
	|sed -e 's/^\([ 	]\)\{1,\}//g' -e "$MENU_FILTER2"`
	if test "$Answer"; then
		Add_Section "$Answer"
		Last_Page
		clear
	fi
else
	Answer=""
fi
if test -z "$Answer"; then
	clear
	Print "no section added. "
fi
}

Do_Edit (){
# process edit item command
if test -t "2"; then
	_de_Done=""
	cp "$TMP_ITEM" "$TMP_FILE1"
	while test -z "$_de_Done"
		do
		eval $EDITOR "$TMP_FILE1"
		clear
		_de_Done="t"
		if sed -n -e "$MENU_FILTER1" -e '1p' "$TMP_FILE1"\
		|egrep "${MENU_ITEM}${Info_Name}\)\.">/dev/null; then
			:
		else
			sed -n -e '1p' "$TMP_FILE1"
			echo "Pattern mismatch: `echo\
			"/${MENU_ITEM}${Info_Name}\)\./"\
			|tr -d "\011"`">&2
			echo
			_de_Done=""
		fi
		if sed -n -e '2,$p' "$TMP_FILE1"|grep '^[^ 	]'>&2; then
			echo "These lines must have leading spaces">&2
			echo
			_de_Done=""
		fi
		if test -z "$_de_Done"; then
			Get_Answer "yn" "Invalid entry, cancel edits? (y or n):"
			if test "y" = "$Answer"; then
				clear
				Print "Canceling edits -- invalid entry ">&2
				_de_Done="t"
			fi
		else
			sed -e "$MENU_FILTER1" -e "$MENU_FILTER2" -e '/^$/d'\
			<"$TMP_FILE1">"$TMP_ITEM"
		fi
		done
else
	Print "editor cannot run with error path redirected "
fi
}

Do_Number (){
# process number
_dn_Num="$1"
if test "$_dn_Num" -ge 1 -a "$_dn_Num" -le "$Item_Num"; then
	if test `echo "$Item_Status"|sed -n -e "${_dn_Num}p"` = "_"; then
		Set_Item "$_dn_Num" "X"
	else
		Set_Item "$_dn_Num" "_"
	fi
else
	Print "\"$_dn_Num\" is an invalid section number. ">&2
fi
}

Do_Help (){
# process menu help
echo
echo "\
Enter the following commands seperated
by spaces and terminated by<ENTER>:
# : (section number) toggle section
a : add a new section
e : edit item -- changes will not be
  accepted if you change the
  '(info_file_name).' or delete
  the key parts: '*' 'Menu Name' ':'
h : get this help screen
n : next page
p : previous page
q : quit and do not put into menu
s : save and put into menu"
Print "Press enter to continue:"
read junk
clear
}

Set_Next (){
# determine value of Next
Next=`expr "$DISPLAY_NUM" + "$Top_Item"`
if test "$Next" -gt "$Item_Num"; then
	Next=""
fi
}

Last_Page (){
# go to last page of menu
Top_Item=`echo|awk "BEGIN{printf(\"%d\",
int((${Item_Num}-1)/${DISPLAY_NUM})*${DISPLAY_NUM}+1)}"`
if test "$Top_Item" -gt "$DISPLAY_NUM"; then
	Previous=`expr "$Top_Item" '-' "$DISPLAY_NUM"`
else
	Previous=""
fi
Set_Next
}

Select_Sections (){
# prompt user for which sections
set -f
Top_Item="1"
Previous=""
Set_Next
clear
echo "Default sections are selected."
Done=""
while test -z "$Done"
	do
	awk "FNR==1{printf(\"%s\n\", substr(\$0,1,${COLUMNS}))}" $TMP_ITEM
	Tmp_Var=`echo "$Item_Status"|tr -d '\012'`
	awk "BEGIN{Max=$Top_Item+$DISPLAY_NUM}
	FNR>=$Top_Item{if(FNR>=Max)exit
	printf(\"%2d:%s %s\n\",FNR,substr(\"$Tmp_Var\",FNR,1),
	substr(\$0,1,${COLUMNS}-5))}" "$TMP_SECTIONS"
	echo "Enter 1-${Item_Num}, add, edit, help,"
	if test "$Previous" -o "$Next"; then
		Print "next, previous, "
	fi
	Print "quit, save :"
	read Command_List||Command_List="$CONTROL_D"
	Command_List=`echo "$Command_List"\
	|tr '\011' ' '|tr ' ' '\012'|sed -e "/^$/d"`
	clear
	if test -z "$Command_List"; then
		Help="y"
	else
		Help=""
	fi
	while test "$Command_List"
		do
		Command=`echo "$Command_List"|sed -n -e '1p'`
		Command_List=`echo "$Command_List"|sed -e '1d'`
		case "$Command" in
			[0-9]|[0-9][0-9]) Do_Number "$Command";;
			n*)	Do_Next;;
			p*)	Do_Previous;;
			a*)	Do_Add_Section;;
			e*)	Do_Edit; break;;
			h*)	Help="y";;
			s*)	Done="s"; break;;
			q*)	Done="q"
			Item_Status=`echo "$Item_Status"|sed -e '1,$s/^./_/'`
			break;;
			*) Print "$Command is not a valid command. ">&2
			Help="y";;
		esac
		done
	if test "$Help"; then
		Do_Help
	fi
	echo
	done
set +f
# if new sections added, remove unused ones
if test "$Item_Num" -gt "$Item_Dir"; then
	Tmp_Var=`echo "$Item_Status"|awk "FNR>$Item_Dir{
	if(\\$0==\"_\")printf(\"%d\n\", FNR)}"`
	if test "$Tmp_Var"; then
		Tmp_Var1=`echo "$Tmp_Var"|sed -e 's/$/d/'`
		sed -e "$Tmp_Var1" "$TMP_SECTIONS">$TMP_FILE1
		cp $TMP_FILE1 "$TMP_SECTIONS"
		Item_Status=`echo "$Item_Status"|sed -e "$Tmp_Var1"`
		Tmp_Var=""
		Tmp_Var1=""
		Item_Num=`wc -l<"$TMP_SECTIONS"|tr -d ' '`
	fi
fi
}

# ###SECTION 100### Constants that redefine
PROMPT1="(y=yes, e=edit, Y=yes to all, n=no, N=No to all):"
FUNCTIONS="y"
#
echo "update-info.f (GNU texinfo @TEXINFO_VERSION@) @UPDATE_INFO_VERSION@"
