Blame Scripts/Bash/Functions/Commons/cli_getRepoName.sh

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