Blame Scripts/Functions/cli_getRepoName.sh

4c79b5
#!/bin/bash
4c79b5
#
eb238e
# cli_getRepoName.sh -- This function standardize file and directories
eb238e
# name convenction inside the working copy of CentOS Artowrk
eb238e
# Repository. As convenction, regular files are written in lower case
4c79b5
# and directories are written in lower case but with the first letter
eb238e
# in upper case.  Use this function to sanitate the name of regular
b1bcf2
# files and directory components of paths you work with.
4c79b5
#
3b0984
# Copyright (C) 2009, 2010, 2011 The CentOS Artwork SIG
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
dcd347
# the Free Software Foundation; either version 2 of the License, or (at
dcd347
# your option) any later version.
fa95b1
#
74a058
# This program is distributed in the hope that it will be useful, but
74a058
# 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
dcd347
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7ac5a5
#
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
4c79b5
function cli_getRepoName {
4c79b5
eb238e
    # Define the name we want to apply verifications to.
550c05
    local NAME="$1"
b1bcf2
550c05
    # Avoid using options as it were names. When name value is empty
550c05
    # but an option is provided, the option becomes the first
550c05
    # positional argument and is evaluated as it were a name which is
550c05
    # something we need to prevent from happening.
550c05
    if [[ $NAME =~ '^-' ]];then
550c05
        return
550c05
    fi
b1bcf2
550c05
    # Look for options passed through positional parameters.
550c05
    case "$2" in
b1bcf2
550c05
        -f|--basename )
b1bcf2
550c05
            # Reduce the path passed to use just the non-directory
550c05
            # part of it (i.e., the last component in the path; _not_
550c05
            # the last "real" directory in the path).
550c05
            NAME=$(basename $NAME)
b1bcf2
550c05
            # Clean value.
550c05
            NAME=$(echo $NAME \
550c05
                | tr -s ' ' '_' \
550c05
                | tr '[:upper:]' '[:lower:]')
550c05
            ;;
b1bcf2
550c05
        -d|--dirname )
b1bcf2
550c05
            local DIR=''
550c05
            local DIRS=''
550c05
            local CLEANDIRS=''
550c05
            local PREFIXDIR=''
b1bcf2
550c05
            # In order to sanitate each directory in a path, it is
550c05
            # required to break off the path string so each component
550c05
            # can be worked out individually and later combine them
550c05
            # back to create a clean path string.
eb238e
                
550c05
            # Reduce path information passed to use the directory part
550c05
            # of it only.  Of course, this is applied if there is a
550c05
            # directory part in the path.  Assuming there is no
550c05
            # directory part but a non-empty value in the path, use
550c05
            # that value as directory part and clean it up.
550c05
            if [[ $NAME =~ '.+/.+' ]];then
550c05
550c05
                # When path information is reduced, we need to
550c05
                # consider that absolute paths contain some
550c05
                # directories outside the working copy directory
550c05
                # structure that shouldn't be sanitated  (e.g., /home,
550c05
                # /home/centos, /home/centos/artwork,
550c05
                # /home/centos/artwork/turnk, trunk, etc.) So, we keep
550c05
                # them unchaged for later use.
550c05
                PREFIXDIR=$(echo $NAME \
550c05
                    | sed -r "s,^(($(cli_getRepoTLDir)/)?(trunk|branches|tags)/).+$,\1,")
550c05
550c05
                # ... and remove them from the path information we do
550c05
                # want to sanitate.
550c05
                DIRS=$(dirname "$NAME" \
550c05
                    | sed -r "s!^${PREFIXDIR}!!" \
550c05
                    | tr '/' ' ')
550c05
550c05
            else
eb238e
                
550c05
                # At this point, there is not directory part in the
550c05
                # information passed, so use the value passed as
550c05
                # directory part as such. 
550c05
                DIRS=$NAME
eb238e
550c05
            fi
eb238e
550c05
            for DIR in $DIRS;do
eb238e
550c05
                # Sanitate directory component.
550c05
                if [[ $DIR =~ '^[a-z]' ]];then
550c05
                    DIR=$(echo ${DIR} \
550c05
                        | tr -s ' ' '_' \
550c05
                        | tr '[:upper:]' '[:lower:]' \
550c05
                        | sed -r 's/^([[:alpha:]])/\u\1/')
550c05
                fi
eb238e
550c05
                # Rebuild path using sanitated values.
550c05
                CLEANDIRS="${CLEANDIRS}/$DIR"
eb238e
550c05
            done
2ad5f6
550c05
            # Redefine path using sanitated values.
550c05
            NAME=$(echo ${CLEANDIRS} | sed -r "s!^/!!")
2ad5f6
550c05
            # Add prefix directory information to sanitated path
550c05
            # information.
550c05
            if [[ "$PREFIXDIR" != '' ]];then
550c05
                NAME=${PREFIXDIR}${NAME}
550c05
            fi
550c05
        ;;
2ad5f6
550c05
    esac
b1bcf2
eb238e
    # Print out the clean path string.
b1bcf2
    echo $NAME
4c79b5
4c79b5
}