Blame Scripts/Functions/cli_getRepoName.sh

4c79b5
#!/bin/bash
4c79b5
#
4c79b5
# cli_getRepoName.sh -- This function sets naming convenction. Inside
4c79b5
# CentOS Artowrk Repository, regular files are written in lower case
4c79b5
# and directories are written in lower case but with the first letter
ae8a10
# in upper case. Use this function to sanitate the name of regular
b1bcf2
# files and directory components of paths you work with.
4c79b5
#
f8b9b4
# Copyright (C) 2009-2011 Alain Reguera Delgado
4c79b5
# 
7cd8e9
# This program is free software; you can redistribute it and/or
7cd8e9
# modify it under the terms of the GNU General Public License as
7cd8e9
# published by the Free Software Foundation; either version 2 of the
7cd8e9
# License, or (at your option) any later version.
4c79b5
# 
4c79b5
# This program is distributed in the hope that it will be useful, but
4c79b5
# 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
4c79b5
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
4c79b5
# USA.
4c79b5
# 
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
4c79b5
function cli_getRepoName {
4c79b5
b1bcf2
    local NAME="$1"
b1bcf2
    local TYPE="$2"
b1bcf2
    local DIRS=''
b1bcf2
    local DIR=''
b1bcf2
    local CLEANDIRS=''
b1bcf2
    local PREFIXDIR=''
4c79b5
b1bcf2
    case $TYPE in
4c79b5
2ad5f6
        'f' | 'basename' )
4c79b5
b1bcf2
            # Reduce the path passed to use just the non-directory
b1bcf2
            # part of it (i.e., the last component in the path; _not_
b1bcf2
            # the last "real" directory in the path).
b1bcf2
            NAME=$(basename $NAME)
4c79b5
b1bcf2
            # Clean value.
b1bcf2
            NAME=$(echo $NAME \
b1bcf2
                | tr -s ' ' '_' \
b1bcf2
                | tr '[:upper:]' '[:lower:]')
b1bcf2
            ;;
4c79b5
2ad5f6
        'd' | 'dirname' )
b1bcf2
b1bcf2
            # Reduce path information passed to use just the directory
b1bcf2
            # part of it.  Of course, this is applied only if there is
b1bcf2
            # a directory part in the path. However, if there is no
b1bcf2
            # directory part but there is a non-empty value in the
b1bcf2
            # path, assume that value as directory part and clean it
b1bcf2
            # up.
2ad5f6
            if [[ $NAME =~ '.+/.+' ]];then
b1bcf2
b1bcf2
                # When path information is reduced, we need to take
b1bcf2
                # into account that absolute path may be provided.
b1bcf2
                # Absolute paths include directory structures outside
b1bcf2
                # the repository directory structure we don't want to
b1bcf2
                # sanitate (e.g., /home/, /home/centos/,
b1bcf2
                # /home/centos/artwork, /home/centos/artwork/turnk/,
b1bcf2
                # trunk/, etc.). In these cases, it is required that
b1bcf2
                # those path component remain untouched. So, in the
b1bcf2
                # sake of keeping path components, outside repository
b1bcf2
                # directory structure untouched, we use the PREFIXDIR
b1bcf2
                # variable to temporarly store the prefix directory
b1bcf2
                # structure we don't want to sanitate.
b1bcf2
                PREFIXDIR=$(echo $NAME \
b1bcf2
                    | sed -r "s,^((${HOME}/artwork/)?(trunk|branches|tags)/).+$,\1,")
b1bcf2
b1bcf2
                # ... and remove it from the path information we do
b1bcf2
                # want to sanitate.
b1bcf2
                DIRS=$(dirname "$NAME" \
b1bcf2
                    | sed -r "s!^${PREFIXDIR}!!" \
b1bcf2
                    | tr '/' ' ')
b1bcf2
b1bcf2
            else
b1bcf2
                
b1bcf2
                # At this point, there is not directory part in the
b1bcf2
                # information passed, so use the value passed as
b1bcf2
                # directory part as such. 
b1bcf2
                DIRS=$NAME
b1bcf2
b1bcf2
            fi
b1bcf2
b1bcf2
            for DIR in $DIRS;do
b1bcf2
978462
                # Sanitate directory component.
2ad5f6
                if [[ $DIR =~ '^[a-z]' ]];then
978462
                    DIR=$(echo ${DIR} \
978462
                        | tr -s ' ' '_' \
978462
                        | tr '[:upper:]' '[:lower:]' \
978462
                        | sed -r 's/^([[:alpha:]])/\u\1/')
978462
                fi
b1bcf2
b1bcf2
                # Rebuild path using sanitated values.
b1bcf2
                CLEANDIRS="${CLEANDIRS}/$DIR"
b1bcf2
b1bcf2
            done
b1bcf2
b1bcf2
            # Redefine path using sanitated values.
b1bcf2
            NAME=$(echo ${CLEANDIRS} | sed -r "s!^/!!")
b1bcf2
b1bcf2
            # Add prefix directory information to sanitated path
b1bcf2
            # information.
b1bcf2
            if [[ "$PREFIXDIR" != '' ]];then
b1bcf2
                NAME=${PREFIXDIR}${NAME}
b1bcf2
            fi
b1bcf2
            ;;
b1bcf2
2ad5f6
        'fd' | 'basename-to-dirname' )
b1bcf2
b1bcf2
            # Retrive non-directory part.
b1bcf2
            NAME=$(cli_getRepoName $NAME 'f')
b1bcf2
b1bcf2
            # Retrive cleaned directory part from non-directory part.
b1bcf2
            NAME=$(cli_getRepoName $NAME 'd')
b1bcf2
            ;;
b1bcf2
2ad5f6
        'df' | 'dirname-to-basename' )
b1bcf2
b1bcf2
            # Retrive cleaned directory part from non-directory part.
b1bcf2
            NAME=$(cli_getRepoName $NAME 'd')
b1bcf2
b1bcf2
            # Retrive non-directory part.
b1bcf2
            NAME=$(cli_getRepoName $NAME 'f')
2ad5f6
            ;;
b1bcf2
    
2ad5f6
        'dfd' | 'dirname-to-basename-to-dirname' )
2ad5f6
2ad5f6
            # Retrive cleaned directory part from non-directory part.
2ad5f6
            NAME=$(cli_getRepoName $NAME 'd')
2ad5f6
2ad5f6
            # Retrive non-directory part.
2ad5f6
            NAME=$(cli_getRepoName $NAME 'f')
2ad5f6
2ad5f6
            # Retrive cleaned directory part from non-directory part.
2ad5f6
            NAME=$(cli_getRepoName $NAME 'd')
2ad5f6
            ;;
b1bcf2
    esac
b1bcf2
b1bcf2
    # Output clean path information.
b1bcf2
    echo $NAME
4c79b5
4c79b5
}