Blame Scripts/Bash/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
#
641be4
# Copyright (C) 2009-2010 Alain Reguera Delgado
641be4
# 
641be4
# This program is free software; you can redistribute it and/or modify
641be4
# it under the terms of the GNU General Public License as published by
641be4
# the Free Software Foundation; either version 2 of the License, or
641be4
# (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.
641be4
#
641be4
# ----------------------------------------------------------------------
641be4
# $Id$
641be4
# ----------------------------------------------------------------------
641be4
641be4
function svg_updateMetadata {
641be4
641be4
    local TEMPLATES=''
641be4
    local INSTANCE=''
35298f
    local COUNT=0
35298f
    local NAM=''
35298f
    local URL=''
35298f
    local KEYS=''
35298f
    local -a TITLE
35298f
    local -a VALUE
35298f
    local -a PATTERN
641be4
35298f
    # Define metadata template file name.
35298f
    TEMPLATE="/home/centos/artwork/trunk/Scripts/Bash/Functions/Svg/Config/tpl_forMetadata.sed"
641be4
641be4
    # Check metadata template file existence.
b76c02
    cli_checkFiles $TEMPLATE 'f'
641be4
35298f
    # Define file name to metadata template instance.
93705a
    INSTANCE=$(cli_getTemporalFile $TEMPLATE)
641be4
35298f
    # Define metadata titles using Inkscape 0.46 metadata definition
35298f
    # 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
35298f
    # Define metadata 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
35298f
    # Define metadata 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
35298f
    # Define metadata 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
35298f
    # Initialize metadata values using user's input.
35298f
    cli_printMessage "`gettext "Enter metadata you want to apply:"`"
35298f
    while [[ $COUNT -ne ${#TITLE[*]} ]];do
35298f
35298f
        # Request metadata value.
35298f
        cli_printMessage "${TITLE[$COUNT]}" 'AsRequestLine'
35298f
        read VALUE[$COUNT]
35298f
35298f
        # Sanitate metadata 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
35298f
        # Set default value to metadata 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
35298f
    for FILE in $FILES;do
641be4
35298f
        # Output action message.
35298f
        cli_printMessage $FILE 'AsUpdatingLine'
641be4
35298f
        # Build title from file path.
35298f
        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.
35298f
        KEYS=$(dirname $FILE | cut -d/ -f6- | tr '/' '\n')
641be4
35298f
        # Build keywords using SVG standard format. Note that this
641be4
        # information is inserted inside metadata template file. The
641be4
        # metadata 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
35298f
        # one of them passes literally to metadata template file.
35298f
        KEYS=$(\
35298f
            for KEY in $KEYS;do
35298f
                echo "            <rdf:li>$KEY</rdf:li>\\"
35298f
            done)
641be4
641be4
        # Create metadata template instance.
35298f
        cp $TEMPLATE $INSTANCE
35298f
35298f
        # Check metadata template instance. We cannot continue if
35298f
        # template instance couldn't be created.
35298f
        cli_checkFiles $INSTANCE 'f'
35298f
35298f
        # Reset metadata counter.
35298f
        COUNT=0
35298f
35298f
        while [[ $COUNT -ne ${#TITLE[*]} ]];do
35298f
35298f
            # Redefine file-specific metadata 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
            # Define translation marker pattern.
35298f
            PATTERN[$COUNT]=$(echo ${TITLE[$COUNT]} | tr '[[:lower:]]' '[[:upper:]]')
35298f
35298f
            # Apply translation marker replacement.
35298f
            if [[ $COUNT -eq 9 ]];then
35298f
                sed -i -r "/=${PATTERN[$COUNT]}=/c\\${VALUE[$COUNT]}" $INSTANCE
35298f
            else
35298f
                sed -i -r "s!=${PATTERN[$COUNT]}=!${VALUE[$COUNT]}!g" $INSTANCE
35298f
            fi
35298f
35298f
            # Increase metadata counter.
35298f
            COUNT=$(($COUNT + 1))
35298f
35298f
        done
35298f
        
35298f
        # Sanitate metadata template instance.
641be4
        sed -i -r -e 's/>$/>\\/g' $INSTANCE
641be4
641be4
        # Apply metadata template instance to scalable vector graphic
641be4
        # file.
641be4
        sed -i -f $INSTANCE $FILE
641be4
35298f
        # Sanitate scalable vector graphic.
35298f
        sed -i -r '/^[[:space:]]*$/d' $FILE
35298f
641be4
        # Remove metadata template instance.
35298f
        cli_checkFiles "${INSTANCE}" 'f'
b76c02
        rm $INSTANCE
641be4
641be4
    done \
641be4
        | awk -f /home/centos/artwork/trunk/Scripts/Bash/Styles/output_forTwoColumns.awk
641be4
641be4
    # Check repository changes and ask you to commit them up to
641be4
    # central repository.
641be4
    cli_commitRepoChanges
641be4
641be4
}