Blame Scripts/Functions/Help/help_updateMenu.sh

4c79b5
#!/bin/bash
4c79b5
#
0211a4
# help_updateMenu.sh -- This function updates menu lines inside
4c79b5
# texinfo chapters.  If this function is called with the
4c79b5
# 'remove-entry' string as first argument, then the menu line related
4c79b5
# to the entry being processed is removed. If this function is called
4c79b5
# with the 'update-entry' string as first argument, then the menu line
4c79b5
# related to the entry being processed is added to the menu. If no
4c79b5
# argument is passed to this function, the 'update-entry' action is
4c79b5
# assumed.
4c79b5
#
9f5f2e
# Copyright (C) 2009-2011 Alain Reguera Delgado
fa95b1
#
fa95b1
# This program is free software; you can redistribute it and/or modify
fa95b1
# it under the terms of the GNU General Public License as published by
dcd347
# the Free Software Foundation; either version 2 of the License, or (at
dcd347
# your option) any later version.
fa95b1
#
74a058
# This program is distributed in the hope that it will be useful, but
74a058
# WITHOUT ANY WARRANTY; without even the implied warranty of
4c79b5
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c79b5
# General Public License for more details.
4c79b5
#
4c79b5
# You should have received a copy of the GNU General Public License
4c79b5
# along with this program; if not, write to the Free Software
dcd347
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
0211a4
function help_updateMenu {
4c79b5
4c79b5
    # Specify which action to do inside chapter's menu.
4c79b5
    local ACTION="$1"
4c79b5
4da029
    # Build the menu node related to the entry being processed
4c79b5
    # currently.
a3b457
    local MENUNODE=$(echo "$ENTRY" | cut -d / -f8- | tr '/' ' ' \
4da029
        | sed 's!\.texi$!!')
4c79b5
4c79b5
    # Give format to menu line using texinfo style.
4da029
    local MENULINE="* ${MANUAL_CHAPTER_NAME} $MENUNODE::" 
4c79b5
4c79b5
    # Define chapter's menu. Remove `@menu', `@end menu', and empty lines
4c79b5
    # from output.
7f7f8f
    local MENU=$(cat $MANUAL_CHAPTER_DIR/chapter-menu.texi \
4c79b5
        | egrep -v '^[[:space:]]*$' | egrep -v '^@(end )?menu') 
4c79b5
4c79b5
    # Re-defined chapter's menu based on action.
4c79b5
    case $ACTION in
4c79b5
        'remove-entry' )
4c79b5
            # Remove menu line from chapter's menu.
4c79b5
            MENU=$(echo "$MENU"  | egrep -v "$MENULINE")
4c79b5
            ;;
4c79b5
        'update-entry' | * )
4c79b5
            # Add menu line to chapter's menu. This is the default
0211a4
            # behaivour if no argument is passed to help_updateMenu
4c79b5
            # function.
4c79b5
            MENU="$MENU
4c79b5
            $MENULINE"
4c79b5
            ;;
4c79b5
    esac
4c79b5
4c79b5
    # Organize menu alphabetically, remove empty and duplicated lines.
4c79b5
    # At this point, empty line may occur the first time the menu is
4c79b5
    # created, don't let them to scape.
4c79b5
    MENU=$(echo "$MENU" | egrep -v '^[[:space:]]*$' | sort | uniq )
4c79b5
4c79b5
    # Rebuild chapter's menu structure adding '@menu' and '@end menu'
4c79b5
    # lines back in menu.
4c79b5
    MENU="@menu
4c79b5
    $MENU
4c79b5
    @end menu"
4c79b5
4c79b5
    # Remove opening spaces/tabs from final menu structure.
4c79b5
    MENU=$(echo "$MENU" | sed -r 's!^[[:space:]]+!!g')
4c79b5
4c79b5
    # Dump final menu structure back into chapter's menu file.
7f7f8f
    echo "$MENU" > $MANUAL_CHAPTER_DIR/chapter-menu.texi
4c79b5
4c79b5
}