#!/bin/bash
# Name: resizeallimgs
# Purpose: resize all jpg or png images in present directory to another sized jpg
# Written by: Rene
# CopyLeft, 2005
# Uses: convert and display commands from ImageMagick
# Syntax: resizeallimgs -j OR -p OR resizeallimgs -h / --help for help
# Example: resizeallimgs -j converts jpgs in directory to resized jpgs
# Example: resizeallimgs -p converts pngs in directory to resized jpgs

# provide help for this script
function useage {
echo -----------------------------------------
echo useage: resizeallimgs -j"|"-p "("jpg or png")"
echo help: resizeallimgs -h "/" resizeallimgs --help
echo creates new jpg files in this directory called '<'filename'>'_resized.jpg
echo pre-existing images are not changed
echo user prompted for WIDTH of thumbnail image - in pixels - Example: 200
echo thumbnail is scaled proportionately to input dimension
echo " "
echo after resizing you can view a composite image of all resized images
echo answer "'"y"'" or "'"n"'" "("without quotes")" to view a composite of resized images created in the directory
echo Notes: can take some time to display a large number of images
echo " right click on composite display to see next composite page if it exists"
echo " composite display can be saved as a single image and more by left clicking on composite window"
echo -----------------------------------------
echo
}

if [ $1 = "-h" ]; then
useage
exit
fi

if [ $1 = "--help" ]; then
useage
exit
fi

echo "Enter the WIDTH of new images (ex:200)"
read response

if [ $1 = '-j' ]; then
echo JPG images being resized
ls -1 *.jpg | sed "s/\(.*\)\.jpg/\1.jpg \1_resized.jpg/" |xargs -n 2 convert -resize $response"x"$response 
fi

if [ $1 = '-p' ]; then
echo PNG images being resized to JPGs
ls -1 *.png | sed "s/\(.*\)\.png/\1.png \1_resized.jpg/" |xargs -n 2 convert -resize $response"x"$response 
fi

# Ask if user wants to view a composite of all new jpg images
# sm: segmentation fault on wapiti, therefore commented out

#echo "Do you want to display all thumbnails created in this directory?(y/n)"
#read displayall
#if [ $displayall = "y" ]; then
#display 'vid:*_resized.*'
#exit
#fi
exit
