#!/bin/sh function help () { echo "Usage: $(basename "$0") [-hatv] [-l ratio] [-s ratio] [-d targetdir] file1 [file2 ...]" echo " -a add slinks to thumbnails and source pictures in the current directory" echo " -l factor enlarge the picture by factor x prepending lx_ to the new file" echo " -s factor shrink the picture by factor x prepending sx_ to the new file" echo " -q factor quality factor, for jpg" echo " -t test, do no file operation" echo " -v verbose, more comments displayed" echo " -d directory all scaled files will go to directory. Default is the source file's directory." } function doit () { showdo cp -dp "$filearg" ${targetdir}/${newfilename} showdo mogrify -scale ${wnew}x${hnew} -quality $quality ${targetdir}/${newfilename} showdo touch -r "$filearg" ${targetdir}/${newfilename} } # read functions y_or_n and debug_echo if [[ "z${MYSOURCERDIR}" = "z" || ! -d ${MYSOURCERDIR} ]] then MYSOURCERDIR=. fi source ${MYSOURCERDIR}/interact envassert MYSOURCERDIR # test extra help situations if [[ z$1 = "z--help" || z$1 = "z-?" ]] then help ; exit 0; fi dohelp=NON testing=NON verbose=NON larger=NON lratio= smaller=NON eratio= destspec=NON dest= addlinks=NON qualityspec=NON quality=95 while getopts ":v" Option do case $Option in v ) verbose=OUI; ;; esac done OPTIND=1 while getopts "ad:htvl:q:s:" Option do case $Option in h ) dohelp=OUI; vecho "Option h" ;; a ) addlinks=OUI; vecho "Option a, soft links will be added as necessary in current directory" ;; t ) testing=OUI; vecho "Option t, no file operations performed, testing only" ;; v ) verbose=OUI; vecho "Option v, verbose mode" ;; s ) smaller=OUI; sratio="$OPTARG"; vecho "Option s, smaller is $smaller, sratio is $sratio" ;; l ) larger=OUI; lratio="$OPTARG"; vecho "Option l, larger is $larger, lratio is $lratio" ;; d ) destspec=OUI; dest="$OPTARG"; vecho "Option d, destspec is $destspec, dest is $dest" ;; q ) qualityspec=OUI; quality="$OPTARG"; vecho "Option d, qualityspec is $qualityspec, quality is $quality" ;; * ) dohelp=OUI; echo "Bad option $OPTARG" ;; esac done shift $(($OPTIND - 1)) # REtest help situations if [ $dohelp = OUI ] then help ; exit 1; fi if [[ $smaller = OUI && $larger = OUI ]] then help ; exit 1; fi if [[ $smaller = NON && $larger = NON ]] then help ; exit 1; fi if [ $# = 0 ]; then dohelp=OUI; echo "Nothing to transfer." ; fi dimsorig= declare -i worig declare -i horig declare -i wnew declare -i hnew for filearg in "$@" do if [ -L "$filearg" ] then echo "$filearg : sorry, don't mogrify symlinks." continue fi if [ ! -f "$filearg" ] then echo "$filearg : sorry, only mogrify regular files." continue fi filename=$(basename "$filearg") if [ $destspec = NON ] then targetdir=$(dirname "$filearg") else targetdir=$dest fi vecho filename is ${filename} targetdir is ${targetdir} worig=$(identify -format "%w" "$filearg") horig=$(identify -format "%h" "$filearg") vecho ${filearg}: worig is ${worig} horig is ${horig} if [ $smaller = OUI ] then wnew=`expr ${worig} / $sratio` hnew=`expr ${horig} / $sratio` newfilename="s${sratio}_${filename}" elif [ $larger = OUI ] then wnew=`expr ${worig} \* $lratio` hnew=`expr ${horig} \* $lratio` newfilename="l${lratio}_${filename}" else echo "nor larger nor smaller"; help ; exit 1; fi vecho ${newfilename}: wnew is ${wnew} hnew is ${hnew} if [ ! -e ${targetdir}/${newfilename} ] then doit # showdo cp -dp "$filearg" ${targetdir}/${newfilename} # showdo mogrify -scale ${wnew}x${hnew} -quality $quality ${targetdir}/${newfilename} # showdo touch -r "$filearg" ${targetdir}/${newfilename} else echo "${targetdir}/${newfilename} already exists." y_or_n "Do you want to overwrite it" if [ $? = 0 ] then doit # showdo cp -dp "$filearg" ${targetdir}/${newfilename} # showdo mogrify -scale ${wnew}x${hnew} -quality $quality ${targetdir}/${newfilename} # showdo touch -r "$filearg" ${targetdir}/${newfilename} fi fi if [ $addlinks = OUI ] then if [ $targetdir != . ] then ln -sf ${targetdir}/${newfilename} . fi if [ $(dirname "$filearg") != . ] then ln -sf "$filearg" . fi fi done