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

4c79b5
#!/bin/bash
4c79b5
#
4c79b5
# groupByFormats.sh -- This function provides post-rendering action
4c79b5
# used to group images by format.  This function create a directory
4c79b5
# for for the image format and move the image file inside it.  For
4c79b5
# example: if the current file is a png file, it is moved inside a png
4c79b5
# directory; if the current file is a jpg file, it is stored inside a
4c79b5
# jpg directory, and so on.
4c79b5
#
4c79b5
# For this function to work correctly, you need to specify which formats you
4c79b5
# want to group. This is done in the post-rendering ACTIONS array inside the
4c79b5
# appropriate render.conf.sh script. 
4c79b5
#
4c79b5
# For example, the following two lines will create a jpg, ppm, xpm, and tif
4c79b5
# file for each png file and will group them all by its format, inside
4c79b5
# directories named as the file format they contain inside (i.e. png, jpg,
4c79b5
# ppm, xpm, tif). 
4c79b5
#     
4c79b5
#     ACTIONS[0]="renderFormats: jpg, ppm, xpm, tif"
4c79b5
#     ACTIONS[1]="groupByFormat: png, jpg, ppm, xpm, tif"
4c79b5
#
4c79b5
# groupByFormat function is generally used with renderFormats function. Both
4c79b5
# definitions should match the type of formats you want to have rendered and
4c79b5
# grouped. You don't need to specify the png format in renderFormats'
4c79b5
# definition, but in groupByFormats's definition it should be specified.
4c79b5
# Otherwise png files will not be grouped inside a png directory.
4c79b5
#
4c79b5
# Copyright (C) 2009-2010 Alain Reguera Delgado
4c79b5
# 
4c79b5
# This program is free software; you can redistribute it and/or modify
4c79b5
# it under the terms of the GNU General Public License as published by
4c79b5
# the Free Software Foundation; either version 2 of the License, or
4c79b5
# (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
# ----------------------------------------------------------------------
4c79b5
# $Id: render_doIdentityImageGroupBy.sh 56 2010-09-17 11:07:26Z al $
4c79b5
# ----------------------------------------------------------------------
4c79b5
4c79b5
function groupByFormat {
4c79b5
4c79b5
   # Get absolute path of PNG image file.
4c79b5
   local FILE="$1"
4c79b5
4c79b5
   # Get image formats.
4c79b5
   local FORMATS=$(echo "$2" | cut -d: -f2-)
4c79b5
4c79b5
   # Sanitize image formats.
4c79b5
   FORMATS=$(echo "${FORMATS}" \
4c79b5
      | sed -r 's!^ *!!g' \
4c79b5
      | sed -r 's!( |:|,|;) *! !g' \
4c79b5
      | sed -r 's! *$!!g')
4c79b5
4c79b5
   # Check image formats.
4c79b5
   if [ "$FORMATS" != "" ];then
4c79b5
4c79b5
      # Loop through image formats and do group by format.
4c79b5
      for FORMAT in $FORMATS;do
4c79b5
4c79b5
         # Redifine file path to add format directory
4c79b5
         local SOURCE=${FILE}.${FORMAT}
4c79b5
         local TARGET=$(dirname $FILE)/${FORMAT}
4c79b5
4c79b5
         # Check target directory existence.
4c79b5
         if [ ! -d $TARGET ];then
4c79b5
            mkdir $TARGET
4c79b5
         fi
4c79b5
4c79b5
         # Redifine file path to add file and its format.
4c79b5
         TARGET=${TARGET}/$(basename $FILE).${FORMAT}
4c79b5
4c79b5
         # Move file into its format location.
4c79b5
         echo "Moved to: $TARGET"
4c79b5
         mv ${SOURCE} ${TARGET}
4c79b5
4c79b5
      done
4c79b5
4c79b5
   fi
4c79b5
4c79b5
}