Blame Automation/centos-art.sh-mods/Cli/cli_getRepoName.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# cli_getRepoName.sh -- This function standardizes files and
Alain Reguera Delgado 8f60cb
# directories name convection inside the working copy of CentOS
Alain Reguera Delgado 8f60cb
# Artowrk Repository. As convection, regular files are written in
Alain Reguera Delgado 8f60cb
# lower-case and directories are written capitalized.  Use this
Alain Reguera Delgado 8f60cb
# function to sanitate the name of regular files and directories on
Alain Reguera Delgado 8f60cb
# paths you work with.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Copyright (C) 2009-2013 The CentOS Project
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 8f60cb
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 8f60cb
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 8f60cb
# your option) any later version.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 8f60cb
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 8f60cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 8f60cb
# General Public License for more details.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 8f60cb
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 8f60cb
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# ----------------------------------------------------------------------
Alain Reguera Delgado 8f60cb
# $Id$
Alain Reguera Delgado 8f60cb
# ----------------------------------------------------------------------
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
function cli_getRepoName {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define the name we want to apply verifications to.
Alain Reguera Delgado 8f60cb
    local NAME="$1"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Avoid using options as it were names. When name value is empty
Alain Reguera Delgado 8f60cb
    # but an option is provided, the option becomes the first
Alain Reguera Delgado 8f60cb
    # positional argument and is evaluated as it were a name which is
Alain Reguera Delgado 8f60cb
    # something we need to prevent from happening.
Alain Reguera Delgado 8f60cb
    if [[ $NAME =~ '^-' ]];then
Alain Reguera Delgado 8f60cb
        return
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Look for options passed through positional parameters.
Alain Reguera Delgado 8f60cb
    case "$2" in
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        -f|--basename )
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Reduce the path passed to use just the non-directory
Alain Reguera Delgado 8f60cb
            # part of it (i.e., the last component in the path; _not_
Alain Reguera Delgado 8f60cb
            # the last "real" directory in the path).
Alain Reguera Delgado 8f60cb
            NAME=$(basename $NAME)
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Clean value.
Alain Reguera Delgado 8f60cb
            NAME=$(echo $NAME \
Alain Reguera Delgado 8f60cb
                | tr -s ' ' '_' \
Alain Reguera Delgado 8f60cb
                | tr '[:upper:]' '[:lower:]')
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        -d|--dirname )
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            local DIR=''
Alain Reguera Delgado 8f60cb
            local DIRS=''
Alain Reguera Delgado 8f60cb
            local CLEANDIRS=''
Alain Reguera Delgado 8f60cb
            local PREFIXDIR=''
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # In order to sanitate each directory in a path, it is
Alain Reguera Delgado 8f60cb
            # required to break off the path string so each component
Alain Reguera Delgado 8f60cb
            # can be worked out individually and later combine them
Alain Reguera Delgado 8f60cb
            # back to create a clean path string.
Alain Reguera Delgado 8f60cb
                
Alain Reguera Delgado 8f60cb
            # Reduce path information passed to use the directory part
Alain Reguera Delgado 8f60cb
            # of it only.  Of course, this is applied if there is a
Alain Reguera Delgado 8f60cb
            # directory part in the path.  Assuming there is no
Alain Reguera Delgado 8f60cb
            # directory part but a non-empty value in the path, use
Alain Reguera Delgado 8f60cb
            # that value as directory part and clean it up.
Alain Reguera Delgado 8f60cb
            if [[ $NAME =~ '.+/.+' ]];then
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                # When path information is reduced, we need to
Alain Reguera Delgado 8f60cb
                # consider that absolute paths contain some
Alain Reguera Delgado 8f60cb
                # directories outside the working copy directory
Alain Reguera Delgado 8f60cb
                # structure that shouldn't be sanitized (e.g., /home,
Alain Reguera Delgado 8f60cb
                # /home/centos, /home/centos/artwork,
Alain Reguera Delgado 8f60cb
                # /home/centos/artwork/turnk, trunk, etc.) So, we keep
Alain Reguera Delgado 8f60cb
                # them unchanged for later use.
Alain Reguera Delgado 8f60cb
                PREFIXDIR=$(echo $NAME \
Alain Reguera Delgado 8f60cb
                    | sed -r "s,^((${TCAR_WORKDIR}/)?(trunk|branches|tags)/)?.+$,\1,")
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                # ... and remove them from the path information we do
Alain Reguera Delgado 8f60cb
                # want to sanitate.
Alain Reguera Delgado 8f60cb
                DIRS=$(dirname "$NAME" \
Alain Reguera Delgado 8f60cb
                    | sed -r "s!^${PREFIXDIR}!!" \
Alain Reguera Delgado 8f60cb
                    | tr '/' ' ')
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            else
Alain Reguera Delgado 8f60cb
                
Alain Reguera Delgado 8f60cb
                # At this point, there is not directory part in the
Alain Reguera Delgado 8f60cb
                # information passed, so use the value passed as
Alain Reguera Delgado 8f60cb
                # directory part as such. 
Alain Reguera Delgado 8f60cb
                DIRS=$NAME
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            for DIR in $DIRS;do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                # Sanitate directory component.
Alain Reguera Delgado 8f60cb
                if [[ $DIR =~ '^[a-z]' ]];then
Alain Reguera Delgado 8f60cb
                    DIR=$(echo ${DIR} \
Alain Reguera Delgado 8f60cb
                        | tr -s ' ' '_' \
Alain Reguera Delgado 8f60cb
                        | tr '[:upper:]' '[:lower:]' \
Alain Reguera Delgado 8f60cb
                        | sed -r 's/^([[:alpha:]])/\u\1/')
Alain Reguera Delgado 8f60cb
                fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                # Rebuild path using sanitized values.
Alain Reguera Delgado 8f60cb
                CLEANDIRS="${CLEANDIRS}/$DIR"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Redefine path using sanitized values.
Alain Reguera Delgado 8f60cb
            NAME=$(echo ${CLEANDIRS} | sed -r "s!^/!!")
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Add prefix directory information to sanitate path
Alain Reguera Delgado 8f60cb
            # information.
Alain Reguera Delgado 8f60cb
            if [[ "$PREFIXDIR" != '' ]];then
Alain Reguera Delgado 8f60cb
                NAME=${PREFIXDIR}${NAME}
Alain Reguera Delgado 8f60cb
            fi
Alain Reguera Delgado 8f60cb
        ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    esac
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Print out the clean path string.
Alain Reguera Delgado 8f60cb
    echo $NAME
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}