# scalepics.sh - scales down pics to dims defined by $X and $Y.
#
# EXAMPLE OF USAGE:
#       scalepics.sh incomingpics/*.jpg
#                                       scales down pics in incoming directory,
#                                       results placed in ~/pics (default)

#!/bin/bash

#screen dimesions
X=800
Y=600

for img in $@ ;do
     img="$img"

     width=`identify -format %w $img`
     height=`identify -format %h $img`
     name=`identify -format %t $img`
     echo dimensions: "$width"x"$height"


     #do we need to resize this?
     if [ $height -gt $Y  -o  $width -gt $X ];
     then

          #if portrait mode, then set height to Y
          if [ $height -gt $width ];
          then

                newwidth=`echo $width / $height*$Y | bc -l`

                #"cast" float created by bc to int
                decpos=`expr index "$newwidth" .`
                newwidth=${newwidth:0:decpos-1}

                newdims="$newwidth"x"$Y"

                echo "resize to $newdims"
                newname="$name-$newdims".jpg
                echo new name: $newname

                `convert -size $newdims $img -resize $newdims $newname`

          
          #otherwise, set the width to X
          else

                newheight=`echo $height / $width*$X | bc -l`

                #"cast" float created by bc to int
                decpos=`expr index "$newheight" .`
                newheight=${newheight:0:decpos-1}

                newdims="$X"x"$newheight"

                echo "resize to $newdims"
                newname="$name-$newdims".jpg
                echo new name: $newname

                `convert -size $newdims $img -resize $newdims $newname`
          fi

          #and copy to pics

     #if no resize then
     else
        echo small image
        `mv $img ~/pics`

     fi
     echo
done
