#!/bin/bash
#
# render_doIdentityGroupByTypes.sh -- This function provides
# post-rendering and last-rendering action to group file inside
# directories named as their file type.
#
# Usage:
# ------
# Post-rendering --> render_doIdentityGroupByTypes "$FILE" "$ACTION"
# Last-rendering --> render_doIdentityGroupByTypes "$ACTION"
#
# Note that post-rendering uses 2 arguments ($FILE and $ACTION) and
# last-rendering just one ($ACTION). This function uses the amount
# of arguments to determine when it is acting as post-rendering and
# when as last-rendering.
#
# This function create one directory for each different file type.
# Later files are moved inside directories respectively. For example:
# if the current file is a .png file, it is moved inside a Png/
# directory; if the current file is a .jpg file, it is stored inside a
# Jpg/ directory, and so on.
#
# For this function to work correctly, you need to specify which file
# type you want to group. This is done in the post-rendering ACTIONS
# array inside the appropriate `render.conf.sh' pre-configuration
# script.
#
# For example, the following three lines will create one jpg, ppm,
# xpm, and tif file for each png file available and groups them all by
# its file type, inside directories named as their file type (i.e.
# Png, Jpg, Ppm, Xpm, Tif). Note that in the example, groupByType is
# ivoked as post-rendering action. If you want to invoke it as
# last-rendering action use LAST definition instead of POST.
#
# ACTIONS[0]='BASE:renderImage'
# ACTIONS[1]='POST:renderFormats: jpg, ppm, xpm, tif'
# ACTIONS[2]='POST:groupByType: png, jpg, ppm, xpm, tif'
#
# groupByType function is generally used with renderFormats function.
# Both definitions must match the file type you want to have rendered
# and grouped. You don't need to specify the png file type in
# renderFormats' definition, but in groupByType's definition it must
# be specified. Otherwise png files will not be grouped inside a png
# directory.
#
# Copyright (C) 2009-2010 Alain Reguera Delgado
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
#
# ----------------------------------------------------------------------
# $Id: render_doIdentityImageGroupBy.sh 56 2010-09-17 11:07:26Z al $
# ----------------------------------------------------------------------
function render_doIdentityGroupByType {
local FILE=''
local -a FILES
local -a PATTERNS
local FORMATS=''
local SOURCE=''
local TARGET=''
local COUNT=0
if [[ $# -eq 1 ]];then
# Define file types for post-rendering action.
FORMATS=$1
elif [[ $# -eq 2 ]];then
# Define file types for last-rendering action.
FORMATS=$2
else
cli_printMessage "`gettext "groupByType: Wrong invokation."`"
cli_printMessage $(caller) "AsToKnowMoreLine"
fi
# Sanitize file types passed from render.conf.sh pre-rendering
# configuration script.
FORMATS=$(echo "${FORMATS}" \
| cut -d: -f2- \
| sed -r 's!^ *!!g' \
| sed -r 's!( |:|,|;) *! !g' \
| sed -r 's! *$!!g')
# Check file types passed from render.conf.sh pre-rendering
# configuration script.
if [[ "$FORMATS" == "" ]];then
cli_printMessage "`gettext "groupByType: There is no file type information to process."`"
cli_printMessage $(caller) "AsToKnowMoreLine"
fi
if [[ $# -eq 1 ]];then
# Define pattern for file extensions.
PATTERNS[0]=$(echo "$FORMATS" | sed 's! !|!g')
# Define pattern for directories.
PATTERNS[1]=$(echo $(for i in $FORMATS; do cli_getRepoName 'd' $i; done) | sed 's! !|!g')
# Define pattern for path. The path pattern combines both file
# extension and directories patterns. This pattern is what we
# use to match rendered file.
PATTERNS[2]="^.*[^(${PATTERNS[1]})]/[[:alpha:]_-]+\.(${PATTERNS[0]})$"
# Define list of files to process when acting as
# last-rendering action. There may be many different files to
# process here, so we need to build a list with them all
# (without duplications).
for FILE in $(find $OPTIONVAL -regextype posix-egrep -type f -regex ${PATTERNS[2]} \
| sed -r 's!\.[[:alpha:]]{1,4}$!!' | sort | uniq \
| egrep $REGEX);do
FILES[$COUNT]="$FILE"
COUNT=$(($COUNT + 1))
done
elif [[ $# -eq 2 ]];then
# Define list of files to process when action as
# post-rendering action. There is just one value to process
# here, the one being currently rendered.
FILES[0]="$1"
fi
# Start processing list of files.
for FILE in "${FILES[@]}";do
for FORMAT in $FORMATS;do
# Redifine file path to add file type directory
SOURCE=${FILE}.${FORMAT}
TARGET=$(dirname $FILE)/$(cli_getRepoName 'd' "$FORMAT")
# Check existence of source file.
cli_checkFiles $SOURCE 'f' '' '--quiet'
if [[ $? -ne 0 ]];then
continue
fi
# Check existence of target directory.
cli_checkFiles $TARGET 'd' '' '--quiet'
if [[ $? -ne 0 ]];then
mkdir $TARGET
fi
# Redifine file path to add file and its type.
TARGET=${TARGET}/$(basename $FILE).${FORMAT}
# Move file into its final location.
cli_printMessage "$TARGET" 'AsMovedToLine'
mv ${SOURCE} ${TARGET}
done
done
}