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 short options.
eb238e
    local ARGSS='f,d'
b1bcf2
eb238e
    # Define long options.
eb238e
    local ARGSL='basename,dirname'
b1bcf2
eb238e
    # Initialize arguments with an empty value and set it as local
eb238e
    # variable to this function scope.
eb238e
    local ARGUMENTS=''
b1bcf2
eb238e
    # Redefine ARGUMENTS variable using current positional parameters. 
793c4c
    cli_parseArgumentsReDef "$@"
eb238e
eb238e
    # Redefine ARGUMENTS variable using getopt output.
793c4c
    cli_parseArguments
eb238e
eb238e
    # Redefine positional parameters using ARGUMENTS variable.
eb238e
    eval set -- "$ARGUMENTS"
b1bcf2
eb238e
    # Define the name we want to apply verifications to.
eb238e
    local NAME=$(echo $@ | sed -r 's!^.*--[[:space:]](.+)$!\1!')
b1bcf2
eb238e
    # Look for options passed through positional parameters.
eb238e
    while true;do
b1bcf2
eb238e
        case "$1" in
b1bcf2
eb238e
            -f|--basename )
b1bcf2
eb238e
                # Reduce the path passed to use just the non-directory
eb238e
                # part of it (i.e., the last component in the path;
eb238e
                # _not_ the last "real" directory in the path).
eb238e
                NAME=$(basename $NAME)
b1bcf2
eb238e
                # Clean value.
eb238e
                NAME=$(echo $NAME \
eb238e
                    | tr -s ' ' '_' \
eb238e
                    | tr '[:upper:]' '[:lower:]')
b1bcf2
eb238e
                shift 1
eb238e
                ;;
b1bcf2
eb238e
            -d|--dirname )
b1bcf2
eb238e
                local DIR=''
eb238e
                local DIRS=''
eb238e
                local CLEANDIRS=''
eb238e
                local PREFIXDIR=''
b1bcf2
eb238e
                # In order to sanitate each directory in a path, it is
eb238e
                # required to break off the path string so each
eb238e
                # component can be worked out individually and later
eb238e
                # combine them back to create a clean path string.
eb238e
                
eb238e
                # Reduce path information passed to use the directory
eb238e
                # part of it only.  Of course, this is applied if
eb238e
                # there is a directory part in the path.  Assuming
eb238e
                # there is no directory part but a non-empty value in
eb238e
                # the path, use that value as directory part and clean
eb238e
                # it up.
eb238e
                if [[ $NAME =~ '.+/.+' ]];then
eb238e
eb238e
                    # When path information is reduced, we need to
eb238e
                    # consider that absolute paths contain some
eb238e
                    # directories outside the working copy directory
eb238e
                    # structure that shouldn't be sanitated  (e.g.,
eb238e
                    # /home, /home/centos, /home/centos/artwork,
eb238e
                    # /home/centos/artwork/turnk, trunk, etc.) 
eb238e
                    # So, we keep them unchaged for later use.
eb238e
                    PREFIXDIR=$(echo $NAME \
eb238e
                        | sed -r "s,^(($(cli_getRepoTLDir)/)?(trunk|branches|tags)/).+$,\1,")
eb238e
eb238e
                    # ... and remove them from the path information we
eb238e
                    # do want to sanitate.
eb238e
                    DIRS=$(dirname "$NAME" \
eb238e
                        | sed -r "s!^${PREFIXDIR}!!" \
eb238e
                        | tr '/' ' ')
eb238e
eb238e
                else
eb238e
                
eb238e
                    # At this point, there is not directory part in
eb238e
                    # the information passed, so use the value passed
eb238e
                    # as directory part as such. 
eb238e
                    DIRS=$NAME
eb238e
eb238e
                fi
eb238e
eb238e
                for DIR in $DIRS;do
eb238e
eb238e
                    # Sanitate directory component.
eb238e
                    if [[ $DIR =~ '^[a-z]' ]];then
eb238e
                        DIR=$(echo ${DIR} \
eb238e
                            | tr -s ' ' '_' \
eb238e
                            | tr '[:upper:]' '[:lower:]' \
eb238e
                            | sed -r 's/^([[:alpha:]])/\u\1/')
eb238e
                    fi
eb238e
eb238e
                    # Rebuild path using sanitated values.
eb238e
                    CLEANDIRS="${CLEANDIRS}/$DIR"
eb238e
eb238e
                done
eb238e
eb238e
                # Redefine path using sanitated values.
eb238e
                NAME=$(echo ${CLEANDIRS} | sed -r "s!^/!!")
eb238e
eb238e
                # Add prefix directory information to sanitated path
eb238e
                # information.
eb238e
                if [[ "$PREFIXDIR" != '' ]];then
eb238e
                    NAME=${PREFIXDIR}${NAME}
eb238e
                fi
2ad5f6
eb238e
                shift 1
eb238e
                ;;
2ad5f6
eb238e
            -- )
eb238e
                shift 1
eb238e
                break
eb238e
                ;;
eb238e
            
eb238e
        esac
2ad5f6
eb238e
    done
b1bcf2
eb238e
    # Print out the clean path string.
b1bcf2
    echo $NAME
4c79b5
4c79b5
}