Blame Scripts/Functions/Help/Backends/Docbook/docbook_getEntities.sh

fbd795
#!/bin/bash
fbd795
#
fbd795
# docbook_getEntities.sh -- This function explores docbook's directory
fbd795
# structure, takes file paths and transforms them into their related
fbd795
# entity parts.
fbd795
#
fbd795
# Copyright (C) 2009, 2010, 2011 The CentOS Artwork SIG
fbd795
#
fbd795
# This program is free software; you can redistribute it and/or modify
fbd795
# it under the terms of the GNU General Public License as published by
fbd795
# the Free Software Foundation; either version 2 of the License, or (at
fbd795
# your option) any later version.
fbd795
#
fbd795
# This program is distributed in the hope that it will be useful, but
fbd795
# WITHOUT ANY WARRANTY; without even the implied warranty of
fbd795
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fbd795
# General Public License for more details.
fbd795
#
fbd795
# You should have received a copy of the GNU General Public License
fbd795
# along with this program; if not, write to the Free Software
fbd795
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
fbd795
#
fbd795
# ----------------------------------------------------------------------
fbd795
# $Id$
fbd795
# ----------------------------------------------------------------------
fbd795
fbd795
function docbook_getEntities {
fbd795
fbd795
    # Define short options.
fbd795
    local ARGSS=''
fbd795
fbd795
    # Define long options.
fbd795
    local ARGSL='name,path,pattern:'
fbd795
fbd795
    # Initialize arguments with an empty value and set it as local
fbd795
    # variable to this function scope.
fbd795
    local ARGUMENTS=''
fbd795
fbd795
    # Initialize entity related variable as local to avoid conflicts
fbd795
    # outside this function scope.
fbd795
    local ENTITY_NAMES=''
fbd795
    local ENTITY_PATHS=''
fbd795
fbd795
    # Initialize name flag (`--name'). This flag specifies whether to
fbd795
    # retrive the name part of the entity or not.
fbd795
    local FLAG_NAME='false'
fbd795
fbd795
    # Initialize path flag (`--path'). This flag specifies whether to
fbd795
    # retrive the name part of the entity or not.
fbd795
    local FLAG_PATH='false'
fbd795
fbd795
    # Initialize pattern flag (`--pattern'). This flag specifies the
fbd795
    # regular expression pattern applied to docbook list of files.  By
fbd795
    # default no reduction is applied to docbook list of files (i.e.,
fbd795
    # all docbook files are printed out).
fbd795
    local FLAG_PATTERN=''
fbd795
fbd795
    # Redefine ARGUMENTS variable using current positional parameters. 
fbd795
    cli_parseArgumentsReDef "$@"
fbd795
fbd795
    # Redefine ARGUMENTS variable using getopt output.
fbd795
    cli_parseArguments
fbd795
fbd795
    # Redefine positional parameters using ARGUMENTS variable.
fbd795
    eval set -- "$ARGUMENTS"
fbd795
fbd795
    # Look for options passed through positional parameters.
fbd795
    while true; do
fbd795
fbd795
        case "$1" in
fbd795
fbd795
            --name )
fbd795
                FLAG_NAME='true'
fbd795
                shift 1
fbd795
                ;;
fbd795
fbd795
            --path )
fbd795
                FLAG_PATH='true'
fbd795
                shift 1
fbd795
                ;;
fbd795
fbd795
            --pattern )
fbd795
                FLAG_PATTERN="$2"
fbd795
                shift 2
fbd795
                ;;
fbd795
fbd795
            -- )
fbd795
                shift 1
fbd795
                break
fbd795
fbd795
        esac
fbd795
fbd795
    done
fbd795
fbd795
    # Build list of all docbook files inside docbook directory
fbd795
    # structure.
fbd795
    local FILE=''
fbd795
    local FILES=$(cli_getFilesList ${@} --pattern="${FLAG_PATTERN}.*\.${FLAG_BACKEND}" --type='f')
fbd795
fbd795
    # Loop through all docbook files under docbook directory structure
fbd795
    # to retrive both entity names and entity relative paths.
fbd795
    for FILE in $FILES;do
fbd795
fbd795
        # Build entity's name.
fbd795
        ENTITY_NAME=$(echo $FILE | cut -d/ -f 9- \
fbd795
            | sed -r 's!/!-!'g | tr '[:upper:]' '[:lower:]' \
fbd795
            | sed "s/\.${FLAG_BACKEND}$//")
fbd795
fbd795
        # Build entity's relative path.
fbd795
        ENTITY_PATH=$(echo $FILE | sed -r "s!${MANUAL_BASEDIR}/Entities/!!")
fbd795
fbd795
        # Output entity information as specified by options. When no
fbd795
        # option is specified the entity full definition is output.
fbd795
        if [[ $FLAG_NAME == 'true' ]];then
fbd795
            echo "&${ENTITY_NAME};"
fbd795
        elif [[ $FLAG_PATH == 'true' ]];then
fbd795
            echo ${ENTITY_PATH}
fbd795
        else
fbd795
            echo ${ENTITY_NAME} ${ENTITY_PATH} \
fbd795
                | gawk '{ printf "\t\n", $1, $2}'
fbd795
        fi
fbd795
fbd795
    done
fbd795
fbd795
}