Blame tcar-scripts/tcar_getRepoName.sh

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