Blame Scripts/Functions/Help/help.sh

4c79b5
#!/bin/bash
4c79b5
#
0211a4
# help.sh -- This function provides documentation features to
4c79b5
# centos-art.sh script. Here we initialize documentation variables and
0211a4
# call help_getArguments functions.
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
fa95b1
# the Free Software Foundation; either version 2 of the License, or
fa95b1
# (at your option) any later version.
fa95b1
#
4c79b5
# This program is distributed in the hope that it will be useful, but
4c79b5
# 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
4c79b5
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
4c79b5
# USA.
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
    
0211a4
function help {
2bd980
c145a8
    local ACTIONNAM=''
c145a8
    local ACTIONVAL=''
5ec287
    local -a ACTIONVALS
c145a8
5285d6
    # Define manuals base directory. This is the place where
5285d6
    # documentation manuals base directory structures are stored and
5285d6
    # organized in.
8bcc5c
    MANUAL_BASEDIR="${HOME}/artwork/trunk/Identity/Manual"
5285d6
1f6dd8
    # Define file name for documentation manual. This is the file used
1f6dd8
    # to initiate the structure of documentation manual.
69150d
    MANUAL_NAME=repository
1f6dd8
1f6dd8
    # Define base name for documentation manual files (without
1f6dd8
    # extension). This is the main file name used to build texinfo
1f6dd8
    # related files (.info, .pdf, .xml, etc.).
1f6dd8
    MANUAL_BASEFILE="${MANUAL_BASEDIR}/${MANUAL_NAME}"
1f6dd8
5ec287
    # Interpret option arguments passed through command-line.
5ec287
    help_getOptions
c145a8
c145a8
    # Redefine positional parameters using ARGUMENTS. At this point,
c145a8
    # option arguments have been removed from ARGUMENTS variable and
c145a8
    # only non-option arguments remain in it. 
c145a8
    eval set -- "$ARGUMENTS"
c145a8
5ec287
    # Store remaining arguments into an array. This way it is possible
5ec287
    # to find out which is the last argument in the list.  When
5ec287
    # copying files inside the repository, the last argument in the
5ec287
    # list is considered the target location and all other arguments
5ec287
    # are considered source the locations. Similary, when renaming
5ec287
    # files, only two arguments can be passed.
5ec287
    for ACTIONVAL in "$@";do
5ec287
        ACTIONVALS[((++${#ACTIONVALS[*]}))]="$ACTIONVAL"
5ec287
    done
5ec287
5ec287
    # Enforce conditions against remaining non-option arguments before
5ec287
    # processing them.
5ec287
    if [[ ${ACTIONNAM} == ${FUNCNAM}_copyEntry ]];then
5ec287
5ec287
        # When a documentation entry is copied, we need to determine
5ec287
        # what location to use as target.  To solve this, the last
5ec287
        # argument passed to centos-art script is taken as target
5ec287
        # location. All other arguments previously defined are
5ec287
        # considered source locations. Notice that more than one
5ec287
        # source location can be specified, but just one target
5ec287
        # location can exist.
5ec287
        local -a ENTRY_SRC
5ec287
        local ENTRY_DST=''
5ec287
        local COUNT=0
5ec287
5ec287
        # Define list of source locations using remaining arguments.
5ec287
        while [[ ${COUNT} -lt $((${#ACTIONVALS[*]} - 1)) ]];do
5ec287
            ENTRY_SRC[((++${#ENTRY_SRC[*]}))]=${ACTIONVALS[$COUNT]}
5ec287
            COUNT=$(($COUNT + 1))
5ec287
        done
5ec287
5ec287
        # Define target location.
5ec287
        ENTRY_DST=$(cli_checkRepoDirTarget "${ACTIONVALS[((${#ACTIONVALS[*]} - 1))]}")
5ec287
5ec287
        # Define target documentation entry.
5ec287
        ENTRY_DST=$(help_getEntry "$ENTRY_DST")
5ec287
5ec287
        # Redefine arguments to store source locations only.
5ec287
        ARGUMENTS=${ENTRY_SRC[@]}
5ec287
5ec287
    elif [[ ${ACTIONNAM} == ${FUNCNAM}_renameEntry ]];then
5ec287
5ec287
        # When documentation entry is renamed, we need to restrict the
5ec287
        # number of arguments to two arguments only.  More than two
5ec287
        # arguments are useless since the renaming action works with
5ec287
        # two arguments only. This is, the source location and the
5ec287
        # target location.
5ec287
        local ENTRY_SRC=''
5ec287
        local ENTRY_DST=''
5ec287
5ec287
        # Verify number of arguments passed to centos-art.sh script.
5ec287
        if [[ ${#ACTIONVALS[*]} -gt 2 ]];then
5ec287
            cli_printMessage "`gettext "Only two arguments are accepted."`" 'AsErrorLine'
5ec287
            cli_printMessage "${FUNCDIRNAME}" 'AsToKnowMoreLine'
5ec287
        elif [[ ${#ACTIONVALS[*]} -lt 2 ]];then
5ec287
            cli_printMessage "`gettext "Two arguments are required."`" 'AsErrorLine'
5ec287
            cli_printMessage "${FUNCDIRNAME}" 'AsToKnowMoreLine'
5ec287
        fi
5ec287
5ec287
        # Define source location. 
5ec287
        ENTRY_SRC=${ACTIONVALS[0]}
5ec287
5ec287
        # Define target location.
5ec287
        ENTRY_DST=$(cli_checkRepoDirTarget "${ACTIONVALS[1]}")
5ec287
5ec287
        # Define target documentation entry.
5ec287
        ENTRY_DST=$(help_getEntry "${ACTIONVALS[1]}")
5ec287
5ec287
        # Redefine arguments to store source locations only.
5ec287
        ARGUMENTS=$ENTRY_SRC
5ec287
5ec287
    else
5ec287
5ec287
        # Redefine arguments to store all arguments.
5ec287
        ARGUMENTS=$@
5ec287
5ec287
    fi
5ec287
6cda26
    # Define action name. It does matter what option be passed to
6cda26
    # centos-art, there are many different actions to perform based on
6cda26
    # the option passed (e.g., `--edit', `--read', `--search', etc.).
0211a4
    # In that sake, we defined action name inside help_getArguments,
6cda26
    # at the moment of interpreting options.
6cda26
6cda26
    # Define action value. As convenction, we use non-option arguments
6cda26
    # to define the action value (ACTIONVAL) variable.
5ec287
    for ACTIONVAL in $ARGUMENTS;do
c145a8
c145a8
        # Check action value passed through the command-line using
c145a8
        # source directory definition as reference.
c145a8
        cli_checkRepoDirSource
c145a8
c145a8
        # Define documentation entry.
0211a4
        ENTRY=$(help_getEntry)
c145a8
c145a8
        # Define documentation entry directory. This is the directory
c145a8
        # where the entry file is stored.
c145a8
        ENTRY_DIR=$(dirname ${ENTRY} | sed -r 's!\.texi$!!')
c145a8
c145a8
        # Define documentation entry file (without extension).
c145a8
        ENTRY_FILE=$(basename ${ENTRY} | sed -r 's!\.texi$!!')
c145a8
c145a8
        # Define directory to store documentation entries.  At this
c145a8
        # point, we need to take a desition about documentation
c145a8
        # design, in order to answer the question: How do we assign
c145a8
        # chapters, sections and subsections automatically, based on
c145a8
        # the repository structure?  and also, how such design could
c145a8
        # be adapted to changes in the repository structure?
c145a8
        #
c145a8
        # One solution would be: represent the repository's directory
0b20e8
        # structure as sections inside a chapter named `Directories'
0b20e8
        # or something similar. Subsections and subsubsections will
0b20e8
        # not have their own files, they all will be written inside
0b20e8
        # the same section file that represents the repository
0b20e8
        # documentation entry.
5ec287
        MANUAL_CHAPTER_DIR=$(echo $ENTRY | cut -d / -f-8)
c145a8
c145a8
        # Define chapter name for the documentation entry we are
c145a8
        # working with.
c145a8
        MANUAL_CHAPTER_NAME=$(basename "$MANUAL_CHAPTER_DIR")
c145a8
c145a8
        # Set action preable.
c145a8
        cli_printActionPreamble "${MANUAL_BASEFILE}.texi" '' ''
c145a8
c145a8
        # Syncronize changes between repository and working copy. At
c145a8
        # this point, changes in the repository are merged in the
c145a8
        # working copy and changes in the working copy committed up to
c145a8
        # repository.
0b20e8
        cli_syncroRepoChanges ${MANUAL_CHAPTER_DIR}
c145a8
c145a8
        # Execute action name.
c145a8
        if [[ $ACTIONNAM =~ "^${FUNCNAM}_[A-Za-z]+$" ]];then
c145a8
            eval $ACTIONNAM
c145a8
        else
c145a8
            cli_printMessage "`gettext "A valid action is required."`" 'AsErrorLine'
eee226
            cli_printMessage "${FUNCDIRNAM}" 'AsToKnowMoreLine'
c145a8
        fi
638bf8
c145a8
        # Commit changes from working copy to central repository only.
c145a8
        # At this point, changes in the repository are not merged in
c145a8
        # the working copy, but chages in the working copy do are
c145a8
        # committed up to repository.
0b20e8
        cli_commitRepoChanges ${MANUAL_CHAPTER_DIR}
4c79b5
c145a8
    done
4c79b5
}