Blame Automation/Scripts/tcar_expandTMarkers.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado eb3010
#   tcar_expandTMarkers.sh -- This function standardizes construction
Alain Reguera Delgado 615395
#   of translation markers and their related expansion. As convention,
Alain Reguera Delgado 615395
#   translation markers must be set inside source files (e.g.,
Alain Reguera Delgado 615395
#   Docbook, Svg, etc.) and expanded inside temporal instances used to
Alain Reguera Delgado 615395
#   produce final contents.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
#   Written by: 
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 615395
#     Key fingerprint = D67D 0F82 4CBD 90BC 6421  DF28 7CCE 757C 17CA 3951
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Copyright (C) 2009-2013 The CentOS Project
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 8f60cb
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 8f60cb
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 8f60cb
# your option) any later version.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 8f60cb
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 8f60cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 8f60cb
# General Public License for more details.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 8f60cb
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 8f60cb
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado eb3010
function tcar_expandTMarkers {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize variables.
Alain Reguera Delgado 8f60cb
    local -a SRC
Alain Reguera Delgado 8f60cb
    local -a DST
Alain Reguera Delgado 8f60cb
    local COUNT=0
Alain Reguera Delgado 8f60cb
    local COUNTSRC=0
Alain Reguera Delgado 8f60cb
    local COUNTDST=0
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define source location on which sed replacements take place.
Alain Reguera Delgado 615395
    local LOCATION="${1}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify that source location does exist.
Alain Reguera Delgado eb3010
    tcar_checkFiles -e ${LOCATION}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define copyright translation markers.
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=COPYRIGHT_YEAR(_LAST)?='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_printCopyrightInfo --year)"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=COPYRIGHT_YEAR(S)?_LIST='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_printCopyrightInfo --years-list)"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=COPYRIGHT_HOLDER='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_printCopyrightInfo --holder)"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=COPYRIGHT_HOLDER_PREDICATE='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_printCopyrightInfo --holder-predicate)"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define license translation markers.
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=LICENSE='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_printCopyrightInfo --license)"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=LICENSE_URL='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_printCopyrightInfo --license-url)"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define theme translation markers.
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=THEME='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_getPathComponent ${OUTPUT} --motif)"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=THEMENAME='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_getPathComponent ${OUTPUT} --motif-name)"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=THEMERELEASE='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_getPathComponent ${OUTPUT} --motif-release)"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define release-specific translation markers.
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=RELEASE='
Alain Reguera Delgado 615395
    DST[((++${#DST[*]}))]="${FLAG_RELEASEVER}"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=MAJOR_RELEASE='
Alain Reguera Delgado 615395
    DST[((++${#DST[*]}))]="$(echo ${FLAG_RELEASEVER} | cut -d'.' -f1)"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=MINOR_RELEASE='
Alain Reguera Delgado 615395
    DST[((++${#DST[*]}))]="$(echo ${FLAG_RELEASEVER} | cut -d'.' -f2)"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define architectures translation markers.
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=ARCH='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_getPathComponent ${FLAG_BASEARCH} --architecture)"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define url translation markers.
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--home' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_WIKI='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--wiki' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_LISTS='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--lists' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_FORUMS='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--forums' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_MIRRORS='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--mirrors' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_DOCS='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--docs' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_PROJECTS='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--projects' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_BUGS='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--bugs' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_SVN='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--svn' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_TRAC='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--trac' '--with-locale')
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=URL_PLANET='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]=$(tcar_printUrl '--planet' '--with-locale')
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define emails translation markers.
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=MAIL_DOCS='
Alain Reguera Delgado eb3010
    DST[((++${#DST[*]}))]="$(tcar_printMailingList --docs)"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define locale translation markers.
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=LOCALE='
Alain Reguera Delgado ddf7d2
    DST[((++${#DST[*]}))]="${TCAR_SCRIPT_LANG_LC}"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=LOCALE_LL='
Alain Reguera Delgado ddf7d2
    DST[((++${#DST[*]}))]="${TCAR_SCRIPT_LANG_LL}"
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=LOCALE_CC='
Alain Reguera Delgado ddf7d2
    DST[((++${#DST[*]}))]="${TCAR_SCRIPT_LANG_CC}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define domain translation markers for domains.
Alain Reguera Delgado 8f60cb
    SRC[((++${#SRC[*]}))]='=DOMAIN_LL='
Alain Reguera Delgado ddf7d2
    if [[ ! ${TCAR_SCRIPT_LANG_LL} =~ '^en' ]];then
Alain Reguera Delgado ddf7d2
        DST[((++${#DST[*]}))]="${TCAR_SCRIPT_LANG_LL}"
Alain Reguera Delgado 8f60cb
    else
Alain Reguera Delgado 8f60cb
        DST[((++${#DST[*]}))]=""
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define repository translation markers.
Alain Reguera Delgado ddf7d2
    SRC[((++${#SRC[*]}))]='=(REPO_TLDIR|REPO_HOME|TCAR_BASEDIR)='
Alain Reguera Delgado ddf7d2
    DST[((++${#DST[*]}))]="${TCAR_BASEDIR}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Do replacement of nested translation markers.
Alain Reguera Delgado 615395
    while [[ ${COUNTDST} -lt ${#DST[@]} ]];do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Verify existence of translation markers. If there is no
Alain Reguera Delgado 8f60cb
        # translation marker on replacement, continue with the next
Alain Reguera Delgado 8f60cb
        # one in the list.
Alain Reguera Delgado 615395
        if [[ ! ${DST[${COUNTDST}]} =~ '=[A-Z_]+=' ]];then
Alain Reguera Delgado 8f60cb
            # Increment destination counter.
Alain Reguera Delgado 615395
            COUNTDST=$((${COUNTDST} + 1))
Alain Reguera Delgado 8f60cb
            # The current replacement value doesn't have translation
Alain Reguera Delgado 8f60cb
            # marker inside, so skip it and evaluate the next
Alain Reguera Delgado 8f60cb
            # replacement value in the list.
Alain Reguera Delgado 8f60cb
            continue
Alain Reguera Delgado 8f60cb
        fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
        while [[ ${COUNTSRC} -lt ${#SRC[*]} ]];do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Update replacements.
Alain Reguera Delgado 615395
            DST[${COUNTDST}]=$(echo ${DST[${COUNTDST}]} \
Alain Reguera Delgado 615395
                | sed -r "s!${SRC[${COUNTSRC}]}!${DST[${COUNTSRC}]}!g")
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Increment source counter.
Alain Reguera Delgado 615395
            COUNTSRC=$((${COUNTSRC} + 1))
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Reset source counter
Alain Reguera Delgado 8f60cb
        COUNTSRC=0
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Increment destination counter.
Alain Reguera Delgado 615395
        COUNTDST=$((${COUNTDST} + 1))
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Apply replacements for translation markers.
Alain Reguera Delgado 8f60cb
    while [[ ${COUNT} -lt ${#SRC[*]} ]];do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Use sed to replace translation markers inside the design
Alain Reguera Delgado 8f60cb
        # model instance.
Alain Reguera Delgado 615395
        sed -r -i "s!${SRC[${COUNT}]}!${DST[${COUNT}]}!g" ${LOCATION}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Increment counter.
Alain Reguera Delgado 615395
        COUNT=$((${COUNT} + 1))
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Remove escaped character from translation markers. This is one
Alain Reguera Delgado 8f60cb
    # of the reasons why translation marker should be expanded in
Alain Reguera Delgado 8f60cb
    # source files instances not the source files themselves.
Alain Reguera Delgado 8f60cb
    # Escaping translation markers provides a way of talking about
Alain Reguera Delgado 8f60cb
    # them without expanding them.
Alain Reguera Delgado 8f60cb
    sed -r -i 's/(=)\\([A-Z_]+=)/\1\2/g' ${LOCATION}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Unset specific translation markers and specific replacement
Alain Reguera Delgado 8f60cb
    # variables in order to clean them up. Otherwise, undesired values
Alain Reguera Delgado 8f60cb
    # may remain from one file to another.
Alain Reguera Delgado 8f60cb
    unset SRC
Alain Reguera Delgado 8f60cb
    unset DST
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}