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

878a2b
#!/bin/bash
878a2b
#
878a2b
# cli_getRepoName.sh -- This function standardize file and directories
878a2b
# name convenction inside the working copy of CentOS Artowrk
878a2b
# Repository. As convenction, regular files are written in lower case
878a2b
# and directories are written in lower case but with the first letter
878a2b
# in upper case.  Use this function to sanitate the name of regular
878a2b
# files and directory components of paths you work with.
878a2b
#
03486a
# Copyright (C) 2009, 2010, 2011, 2012 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
878a2b
                # structure that shouldn't be sanitated  (e.g., /home,
878a2b
                # /home/centos, /home/centos/artwork,
878a2b
                # /home/centos/artwork/turnk, trunk, etc.) So, we keep
878a2b
                # them unchaged for later use.
878a2b
                PREFIXDIR=$(echo $NAME \
878a2b
                    | sed -r "s,^(($(cli_getRepoTLDir)/)?(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
878a2b
                # Rebuild path using sanitated values.
878a2b
                CLEANDIRS="${CLEANDIRS}/$DIR"
878a2b
878a2b
            done
878a2b
878a2b
            # Redefine path using sanitated values.
878a2b
            NAME=$(echo ${CLEANDIRS} | sed -r "s!^/!!")
878a2b
878a2b
            # Add prefix directory information to sanitated 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
}