Blame Scripts/tcar_printPath.sh

Alain Reguera Delgado b6d353
#!/bin/bash
Alain Reguera Delgado b6d353
######################################################################
Alain Reguera Delgado b6d353
#
Alain Reguera Delgado b6d353
#   tcar - The CentOS Artwork Repository automation tool.
Alain Reguera Delgado b6d353
#   Copyright © 2014 The CentOS Artwork SIG
Alain Reguera Delgado b6d353
#
Alain Reguera Delgado b6d353
#   This program is free software; you can redistribute it and/or
Alain Reguera Delgado b6d353
#   modify it under the terms of the GNU General Public License as
Alain Reguera Delgado b6d353
#   published by the Free Software Foundation; either version 2 of the
Alain Reguera Delgado b6d353
#   License, or (at your option) any later version.
Alain Reguera Delgado b6d353
#
Alain Reguera Delgado b6d353
#   This program is distributed in the hope that it will be useful,
Alain Reguera Delgado b6d353
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado b6d353
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado b6d353
#   General Public License for more details.
Alain Reguera Delgado b6d353
#
Alain Reguera Delgado b6d353
#   You should have received a copy of the GNU General Public License
Alain Reguera Delgado b6d353
#   along with this program; if not, write to the Free Software
Alain Reguera Delgado b6d353
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado b6d353
#
Alain Reguera Delgado b6d353
#   Alain Reguera Delgado <al@centos.org.cu>
Alain Reguera Delgado b6d353
#   39 Street No. 4426 Cienfuegos, Cuba.
Alain Reguera Delgado b6d353
#
Alain Reguera Delgado b6d353
######################################################################
Alain Reguera Delgado b6d353
Alain Reguera Delgado 0349b6
# Print non-option arguments passed through the command-line for
Alain Reguera Delgado 0349b6
# further processing.
Alain Reguera Delgado b6d353
function tcar_printPath {
Alain Reguera Delgado b6d353
Alain Reguera Delgado 0349b6
    # Define base directory's default value.
Alain Reguera Delgado 0349b6
    local BASEDIR="${PWD}"
Alain Reguera Delgado b6d353
Alain Reguera Delgado 0349b6
    # Define the function's options.
Alain Reguera Delgado 0349b6
    OPTIND=1
Alain Reguera Delgado 0349b6
    while getopts "b:" OPTION "${@}"; do
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
        case "${OPTION}" in
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
             b )
Alain Reguera Delgado 0349b6
                BASEDIR=${OPTARG}
Alain Reguera Delgado 0349b6
                ;;
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
        esac
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
    done
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
    # Clean up positional parameters to reflect the fact that options
Alain Reguera Delgado 0349b6
    # have been processed already.
Alain Reguera Delgado 0349b6
    shift $(( ${OPTIND} - 1 ))
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
    # Retrieve all locations passed as non-option arguments to this
Alain Reguera Delgado 0349b6
    # function.
Alain Reguera Delgado 0349b6
    local LOCATIONS="${@}"
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
    for LOCATION in ${LOCATIONS};do
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
        # Concatenate both base directory and provided location to
Alain Reguera Delgado 0349b6
        # build the final path.
Alain Reguera Delgado 0349b6
        if [[ ${BASEDIR} =~ '^/' && ! ${LOCATION} =~ '^/' ]];then
Alain Reguera Delgado 0349b6
            LOCATION=${BASEDIR}/${LOCATION}
Alain Reguera Delgado 0349b6
        fi
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
        # Remove consecutive slashes, trailing slashes and any
Alain Reguera Delgado 0349b6
        # dot-slash or double-dot slash constructions from final
Alain Reguera Delgado 0349b6
        # location.
Alain Reguera Delgado 0349b6
        echo "${LOCATION}" \
Alain Reguera Delgado 0349b6
            | sed -r -e 's,/+,/,g' -e 's,/+$,,g' -e 's,\.+/,,g'
Alain Reguera Delgado 0349b6
Alain Reguera Delgado 0349b6
    done
Alain Reguera Delgado b6d353
Alain Reguera Delgado b6d353
}