#!/usr/local/bin/bash
# Copyright (C) Naotoshi Seo, s1080134

function printUsage(){
  cmd=${0##*/}
  echo "Batch convert. convert front end." 1>&2
  echo "Usage: $cmd [[convert options] :] files ... ext" 1>&2
  echo "          ex) $cmd *.png jpg" 1>&2
  echo "          ex) $cmd : *.png jpg" 1>&2
  echo "          ex) $cmd -size 1024x768 : *.png ../*.gif jpg" 1>&2
}
if [ $# -lt 2 ]; then
  printUsage
  exit 0
fi

if [ "$1" = ":" ];then
  shift
elif [ $(expr "x$1" : 'x-') -ne 0 ];then
  #Options
  while [ $# -gt 0 ]
    do
    if [ "$1" = ":" ];then
      shift
      break
    fi
    options="$options$1 "
    shift
  done
  if [ $# -eq 0 ]; then
    printUsage
    exit 1
  fi
fi
#$@ become files ... ext

while [ $# -gt 0 ]
  do
  case $# in
    1)
    ext=$1
    break
  esac
  files="$files $1"
  shift
done

#main
for file in $files
  do
  echo "convert $options$file ${file%.*}.$ext"
  convert $options$file ${file%.*}.$ext
  if [ "$?" != "0" ];then
    break
  fi
done
