Blame Scripts/Bash/Cli/Functions/Svg/svg_updateMetadata.sh

641be4
#!/bin/bash
641be4
#
35298f
# svg_updateMetadata.sh -- This function updates metadata values
35298f
# inside scalable vector graphic (SVG) files. First, we ask user to
35298f
# provide the information. If user doesn't provide the information,
35298f
# centos-art.sh script uses autogenerated values as default ---when
35298f
# possible--- taking as reference SVG file path. 
641be4
#
9f5f2e
# Copyright (C) 2009-2011 Alain Reguera Delgado
641be4
# 
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.
641be4
# 
641be4
# This program is distributed in the hope that it will be useful, but
641be4
# WITHOUT ANY WARRANTY; without even the implied warranty of
641be4
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
641be4
# General Public License for more details.
641be4
#
641be4
# You should have received a copy of the GNU General Public License
641be4
# along with this program; if not, write to the Free Software
641be4
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
641be4
# USA.
7cd8e9
# 
641be4
# ----------------------------------------------------------------------
641be4
# $Id$
641be4
# ----------------------------------------------------------------------
641be4
641be4
function svg_updateMetadata {
641be4
35298f
    local NAM=''
35298f
    local URL=''
35298f
    local KEYS=''
03a4b4
    local FILE=''
03a4b4
    local COUNT=0
03a4b4
    local FILES=''
03a4b4
    local INSTANCE=''
03a4b4
    local TEMPLATES=''
35298f
    local -a TITLE
35298f
    local -a VALUE
35298f
    local -a PATTERN
b6a56c
    local -a PATTERN_MSG
b6a56c
    local -a MARKER
b6a56c
    local -a DEFAULT
641be4
b6a56c
    # Define template file name.
7b46a4
    TEMPLATE="${CLI_BASEDIR}/Functions/Svg/Config/tpl_forMetadata.sed"
641be4
b6a56c
    # Check template file existence.
b76c02
    cli_checkFiles $TEMPLATE 'f'
641be4
b6a56c
    # Define titles using Inkscape 0.46 metadata definition as reference.
35298f
    TITLE[0]="`gettext "Title"`"
35298f
    TITLE[1]="`gettext "Date"`"
35298f
    TITLE[2]="`gettext "Creator"`"
35298f
    TITLE[3]="`gettext "Rights"`"
35298f
    TITLE[4]="`gettext "Publisher"`"
35298f
    TITLE[5]="`gettext "Identifier"`"
35298f
    TITLE[6]="`gettext "Source"`"
35298f
    TITLE[7]="`gettext "Relation"`"
35298f
    TITLE[8]="`gettext "Language"`"
35298f
    TITLE[9]="`gettext "Keywords"`"
35298f
    TITLE[10]="`gettext "Coverage"`"
35298f
    TITLE[11]="`gettext "Description"`"
35298f
    TITLE[12]="`gettext "Contributor"`"
35298f
b6a56c
    # Define markers. These values are used inside template.
b6a56c
    MARKER[0]='=TITLE='
b6a56c
    MARKER[1]='=DATE='
b6a56c
    MARKER[2]='=CREATOR='
b6a56c
    MARKER[3]='=RIGHTS='
b6a56c
    MARKER[4]='=PUBLISHER='
b6a56c
    MARKER[5]='=IDENTIFIER='
b6a56c
    MARKER[6]='=SOURCE='
b6a56c
    MARKER[7]='=RELATION='
b6a56c
    MARKER[8]='=LANGUAGE='
b6a56c
    MARKER[9]='=KEYWORDS='
b6a56c
    MARKER[10]='=COVERAGE='
b6a56c
    MARKER[11]='=DESCRIPTION='
b6a56c
    MARKER[12]='=CONTRIBUTOR='
b6a56c
b6a56c
    # Define pattern. These values are used as regular
35298f
    # expression patterns for user's input further verification.
35298f
    PATTERN[0]='^([[:alnum:] _-.]+)?$'
35298f
    PATTERN[1]='^([0-9]{4}-(0[1-9]|1[0-2])-([0-2][1-9]|3[0-1]))?$'
35298f
    PATTERN[2]=${PATTERN[0]}
35298f
    PATTERN[3]=${PATTERN[0]}
35298f
    PATTERN[4]=${PATTERN[0]}
35298f
    PATTERN[5]='^(https://projects.centos.org/svn/artwork/[[:alnum:]/._-]+)?$'
35298f
    PATTERN[6]=${PATTERN[5]}
35298f
    PATTERN[7]=${PATTERN[5]}
35298f
    PATTERN[8]='^([a-z]{2}(_[A-Z]{2})?)?$'
35298f
    PATTERN[9]=${PATTERN[0]}
35298f
    PATTERN[10]=${PATTERN[0]}
35298f
    PATTERN[11]=${PATTERN[0]}
35298f
    PATTERN[12]=${PATTERN[0]}
35298f
b6a56c
    # Define pattern message. These values are used as output
35298f
    # message when user's input doesn't match the related pattern.
35298f
    PATTERN_MSG[0]="`gettext "Try using alphanumeric characters."`"
35298f
    PATTERN_MSG[1]="`gettext "Try using 'YYYY-MM-DD' date format."`"
35298f
    PATTERN_MSG[2]=${PATTERN_MSG[0]}
35298f
    PATTERN_MSG[3]=${PATTERN_MSG[0]}
35298f
    PATTERN_MSG[4]=${PATTERN_MSG[0]}
35298f
    PATTERN_MSG[5]="`gettext "Only locations under https://projects.centos.ort/svn/artwork are supported."`"
35298f
    PATTERN_MSG[6]=${PATTERN_MSG[0]}
35298f
    PATTERN_MSG[7]=${PATTERN_MSG[0]}
35298f
    PATTERN_MSG[8]="`gettext "Try using 'LL' or 'LL_CC' locale format."`"
35298f
    PATTERN_MSG[9]=${PATTERN_MSG[0]}
35298f
    PATTERN_MSG[10]=${PATTERN_MSG[0]}
35298f
    PATTERN_MSG[11]=${PATTERN_MSG[0]}
35298f
    PATTERN_MSG[12]=${PATTERN_MSG[0]}
35298f
b6a56c
    # Define common default values.
35298f
    DEFAULT[1]=$(date +%Y-%m-%d)
35298f
    DEFAULT[2]="The CentOS Project"
35298f
    DEFAULT[3]=${DEFAULT[2]}
35298f
    DEFAULT[4]=${DEFAULT[2]}
35298f
    DEFAULT[8]=$(cli_getCurrentLocale)
35298f
    DEFAULT[10]=${DEFAULT[2]}
35298f
b6a56c
    # Initialize values using user's input.
b6a56c
    cli_printMessage "`gettext "Enter metadata information you want to apply:"`"
35298f
    while [[ $COUNT -ne ${#TITLE[*]} ]];do
35298f
b6a56c
        # Request value.
35298f
        cli_printMessage "${TITLE[$COUNT]}" 'AsRequestLine'
35298f
        read VALUE[$COUNT]
35298f
b6a56c
        # Sanitate values to exclude characters that could
35298f
        # introduce possible markup malformations to final SVG files.
35298f
        until [[ ${VALUE[$COUNT]} =~ ${PATTERN[$COUNT]} ]];do
35298f
            cli_printMessage "${PATTERN_MSG[$COUNT]}"
35298f
            cli_printMessage "${TITLE[$COUNT]}" 'AsRequestLine'
35298f
            read VALUE[$COUNT]
35298f
        done
35298f
b6a56c
        # Set default value to empty values. 
35298f
        if [[ ${VALUE[$COUNT]} == '' ]];then
35298f
            VALUE[$COUNT]=${DEFAULT[$COUNT]}
35298f
        fi
35298f
35298f
        # Increase counter.
35298f
        COUNT=$(($COUNT + 1))
641be4
35298f
    done
641be4
03a4b4
    # Build list of files to process.
d26634
    FILES=$(cli_getFilesList "$ACTIONVAL" "${FLAG_FILTER}.*\.(svgz|svg)")
d26634
d26634
    # Set action preamble.
d26634
    cli_printActionPreamble "${FILES}"
a33931
03a4b4
    # Process list of scalable vector graphics.
35298f
    for FILE in $FILES;do
641be4
35298f
        # Output action message.
35298f
        cli_printMessage $FILE 'AsUpdatingLine'
641be4
35298f
        # Build title from file path.
eed7b0
        NAM=$(basename "$FILE")
641be4
35298f
        # Build url from file path.
35298f
        URL=$(echo $FILE | sed 's!/home/centos!https://projects.centos.org/svn!')
641be4
35298f
        # Build keywords from file path. Do not include filename, it
35298f
        # is already on title.
eed7b0
        KEYS=$(dirname "$FILE" | cut -d/ -f6- | tr '/' '\n')
641be4
35298f
        # Build keywords using SVG standard format. Note that this
b6a56c
        # information is inserted inside template file. The
b6a56c
        # template file is a replacement set of sed commands
641be4
        # so we need to escape the new line of each line using one
641be4
        # backslash (\). As we are doing this inside bash, it is
641be4
        # required to escape the backslash with another backslash so
b6a56c
        # one of them passes literally to template file.
35298f
        KEYS=$(\
35298f
            for KEY in $KEYS;do
35298f
                echo "            <rdf:li>$KEY</rdf:li>\\"
35298f
            done)
641be4
b6a56c
        # Redefine template instance file name.
b6a56c
        INSTANCE=$(cli_getTemporalFile $TEMPLATE)
b6a56c
b6a56c
        # Create template instance.
35298f
        cp $TEMPLATE $INSTANCE
35298f
b6a56c
        # Check template instance. We cannot continue if the template
b6a56c
        # instance couldn't be created.
35298f
        cli_checkFiles $INSTANCE 'f'
35298f
b6a56c
        # Reset counter.
35298f
        COUNT=0
35298f
35298f
        while [[ $COUNT -ne ${#TITLE[*]} ]];do
35298f
b6a56c
            # Redefine file-specific values.
35298f
            if [[ $COUNT -eq 0 ]];then
35298f
                VALUE[$COUNT]=$NAM
35298f
            elif [[ $COUNT -eq 5 ]];then
35298f
                VALUE[$COUNT]=$URL
35298f
            elif [[ $COUNT -eq 6 ]];then
35298f
                VALUE[$COUNT]=$URL
35298f
            elif [[ $COUNT -eq 7 ]];then
35298f
                VALUE[$COUNT]=$URL
35298f
            elif [[ $COUNT -eq 9 ]];then
35298f
                VALUE[$COUNT]=$KEYS
35298f
            fi
35298f
35298f
            # Apply translation marker replacement.
35298f
            if [[ $COUNT -eq 9 ]];then
b6a56c
                sed -i -r "/${MARKER[$COUNT]}/c\\${VALUE[$COUNT]}" $INSTANCE
35298f
            else
b6a56c
                sed -i -r "s!${MARKER[$COUNT]}!${VALUE[$COUNT]}!g" $INSTANCE
35298f
            fi
35298f
b6a56c
            # Increase counter.
35298f
            COUNT=$(($COUNT + 1))
35298f
35298f
        done
35298f
        
b6a56c
        # Sanitate template instance.
641be4
        sed -i -r -e 's/>$/>\\/g' $INSTANCE
641be4
b6a56c
        # Apply template instance to scalable vector graphic
641be4
        # file.
641be4
        sed -i -f $INSTANCE $FILE
641be4
b6a56c
        # Remove template instance.
03a4b4
        if [[ -f $INSTANCE ]];then
03a4b4
            rm $INSTANCE
03a4b4
        fi
641be4
b6a56c
        # Sanitate scalable vector graphic.
b6a56c
        sed -i -r '/^[[:space:]]*$/d' $FILE
b6a56c
03a4b4
    done
641be4
641be4
}