Blame tcar-scripts/tcar_getRepoName.sh

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