Blame Scripts/Bash/Functions/Render/render_doIdentityGroupByType.sh

4c79b5
#!/bin/bash
4c79b5
#
ff3ea9
# render_doIdentityGroupByTypes.sh -- This function provides
15b1d2
# post-rendition and last-rendition action to group file inside
ff3ea9
# directories named as their file type.
4c79b5
#
7cd8e9
# Usage:
7cd8e9
# ------
15b1d2
# Post-rendition --> render_doIdentityGroupByTypes "$FILE" "$ACTION"
15b1d2
# Last-rendition --> render_doIdentityGroupByTypes "$ACTION"
4c79b5
#
15b1d2
# Note that post-rendition uses 2 arguments ($FILE and $ACTION) and
15b1d2
# last-rendition just one ($ACTION). This function uses the amount
15b1d2
# of arguments to determine when it is acting as post-rendition and
15b1d2
# when as last-rendition.
ff3ea9
#
ff3ea9
# This function create one directory for each different file type.
ff3ea9
# Later files are moved inside directories respectively.  For example:
ff3ea9
# if the current file is a .png file, it is moved inside a Png/
ff3ea9
# directory; if the current file is a .jpg file, it is stored inside a
ff3ea9
# Jpg/ directory, and so on.
ff3ea9
#
ff3ea9
# For this function to work correctly, you need to specify which file
15b1d2
# type you want to group. This is done in the post-rendition ACTIONS
ff3ea9
# array inside the appropriate `render.conf.sh' pre-configuration
ff3ea9
# script. 
ff3ea9
#
ff3ea9
# For example, the following three lines will create one jpg, ppm,
ff3ea9
# xpm, and tif file for each png file available and groups them all by
ff3ea9
# its file type, inside directories named as their file type (i.e.
ff3ea9
# Png, Jpg, Ppm, Xpm, Tif). Note that in the example, groupByType is
15b1d2
# ivoked as post-rendition action. If you want to invoke it as
15b1d2
# last-rendition action use LAST definition instead of POST.
7cd8e9
# 
7cd8e9
# ACTIONS[0]='BASE:renderImage' 
7cd8e9
# ACTIONS[1]='POST:renderFormats: jpg, ppm, xpm, tif' 
7cd8e9
# ACTIONS[2]='POST:groupByType: png, jpg, ppm, xpm, tif'
4c79b5
#
ff3ea9
# groupByType function is generally used with renderFormats function.
ff3ea9
# Both definitions must match the file type you want to have rendered
ff3ea9
# and grouped. You don't need to specify the png file type in
ff3ea9
# renderFormats' definition, but in groupByType's definition it must
ff3ea9
# be specified.  Otherwise png files will not be grouped inside a png
ff3ea9
# directory.
4c79b5
#
9f5f2e
# 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
ff3ea9
function render_doIdentityGroupByType {
ff3ea9
ff3ea9
    local FILE=''
ff3ea9
    local -a FILES
ff3ea9
    local -a PATTERNS
ff3ea9
    local FORMATS=''
ff3ea9
    local SOURCE=''
ff3ea9
    local TARGET=''
ff3ea9
    local COUNT=0
ff3ea9
ff3ea9
    if [[ $# -eq 1 ]];then
15b1d2
        # Define file types for post-rendition action.
ff3ea9
        FORMATS=$1
ff3ea9
    elif [[ $# -eq 2 ]];then
15b1d2
        # Define file types for last-rendition action.
ff3ea9
        FORMATS=$2
ff3ea9
    else
07c2fe
        cli_printMessage "`gettext "groupByType: Wrong invokation."`" 'AsErrorLine'
ff3ea9
        cli_printMessage $(caller) "AsToKnowMoreLine"
ff3ea9
    fi
ff3ea9
15b1d2
    # Sanitate file types passed from render.conf.sh pre-rendition
ff3ea9
    # configuration script.
44281e
    FORMATS=$(render_getIdentityConfigOption "$FORMATS" '2-')
ff3ea9
15b1d2
    # Check file types passed from render.conf.sh pre-rendition
ff3ea9
    # configuration script.
ff3ea9
    if [[ "$FORMATS" == "" ]];then
07c2fe
        cli_printMessage "`gettext "There is no file type information to process."`" 'AsErrorLine'
ff3ea9
        cli_printMessage $(caller) "AsToKnowMoreLine"
ff3ea9
    fi
ff3ea9
ff3ea9
    if [[ $# -eq 1 ]];then
ff3ea9
ff3ea9
        # Define pattern for file extensions.
ff3ea9
        PATTERNS[0]=$(echo "$FORMATS" | sed 's! !|!g')
ff3ea9
ff3ea9
        # Define pattern for directories.
f94e5c
        PATTERNS[1]=$(echo $(for i in $FORMATS; do cli_getRepoName "$i" 'd'; done) | sed 's! !|!g')
ff3ea9
ff3ea9
        # Define pattern for path. The path pattern combines both file
ff3ea9
        # extension and directories patterns. This pattern is what we
ff3ea9
        # use to match rendered file.
ff3ea9
        PATTERNS[2]="^.*[^(${PATTERNS[1]})]/[[:alpha:]_-]+\.(${PATTERNS[0]})$"
ff3ea9
ff3ea9
        # Define list of files to process when acting as
15b1d2
        # last-rendition action. There may be many different files to
ff3ea9
        # process here, so we need to build a list with them all
ff3ea9
        # (without duplications).
8219f0
        for FILE in $(find $ACTIONVAL -regextype posix-egrep -type f -regex ${PATTERNS[2]} \
ff3ea9
            | sed -r 's!\.[[:alpha:]]{1,4}$!!' | sort | uniq \
558b58
            | egrep $FLAG_FILTER);do
ff3ea9
            FILES[$COUNT]="$FILE"
ff3ea9
            COUNT=$(($COUNT + 1))
ff3ea9
        done
4c79b5
ff3ea9
    elif [[ $# -eq 2 ]];then
4c79b5
ff3ea9
        # Define list of files to process when action as
15b1d2
        # post-rendition action. There is just one value to process
ff3ea9
        # here, the one being currently rendered.
ff3ea9
        FILES[0]="$1"
ff3ea9
        
ff3ea9
    fi
4c79b5
ff3ea9
    # Start processing list of files.
ff3ea9
    for FILE in "${FILES[@]}";do
4c79b5
ff3ea9
        for FORMAT in $FORMATS;do
4c79b5
f94e5c
            # Redifine source file we want to move.
ff3ea9
            SOURCE=${FILE}.${FORMAT}
f94e5c
f94e5c
            # Define target directory where source file will be moved
f94e5c
            # into.
f94e5c
            TARGET=$(dirname "$FILE")/$(cli_getRepoName "$FORMAT" 'd')
4c79b5
ff3ea9
            # Check existence of source file.
b76c02
            cli_checkFiles $SOURCE 'f'
4c79b5
ff3ea9
            # Check existence of target directory.
f94e5c
            if [[ ! -d $TARGET ]];then
f94e5c
                mkdir -p $TARGET
f94e5c
            fi
4c79b5
ff3ea9
            # Redifine file path to add file and its type.
f94e5c
            TARGET=${TARGET}/$(cli_getRepoName "$FILE" 'f').${FORMAT}
4c79b5
ff3ea9
            # Move file into its final location.
ff3ea9
            cli_printMessage "$TARGET" 'AsMovedToLine'
ff3ea9
            mv ${SOURCE} ${TARGET}
4c79b5
4c79b5
      done
4c79b5
ff3ea9
    done
4c79b5
4c79b5
}