#!/bin/bash
#    -v                    print the name of each file converted on stdout
#    -e extension    add the supplied extension to each new file created
#    -d dirname     put the converted files in the given directory (which must already exist)


set -eu
set -o pipefail

OPTDONE=0
VERBOSE=0
OUTDIR="."
EXTENSION=""
USAGE="usage: lcbunch [-v] [-e extension] [-d directory] files"

while getopts "ve:d:" opt
do
    case $opt in
       v)
           echo "setting VERBOSE"
           VERBOSE=1
           ;;
       e)
           EXTENSION=$OPTARG			# OPTARG is set by getopts
           echo "setting EXTENSION to $EXTENSION"
           ;;
       d)
           OUTDIR=$OPTARG
           ;;
       *)
           echo $USAGE
           exit 1
           ;;
    esac
done

shift $(($OPTIND-1))

if test -n "$EXTENSION"
then
    EXTENSION='.'$EXTENSION		# add the dot
fi

# check if EXTENSION or OUTDIR was supplied
if test -z "$EXTENSION" -a "$OUTDIR" == "."	# neither changed
then
    echo "Must supply -e or -d"
    echo $USAGE
    exit 2
fi

# At this point, $* just consists of files
while test $# -gt 0
do
    INFILE=$1
    if test -f $INFILE				# make sure it's a regular file
    then
        if test $VERBOSE -eq 1; then echo "converting $INFILE"; fi
        OUTFILE=$OUTDIR/${INFILE}${EXTENSION}	# curly braces to avoid running into one another
        lcasecopy $INFILE > $OUTFILE		# copy this file
    fi
    shift					# next file
done
    
