#! /bin/sh
# #############################################################################

       NAME_="2bmp"
       HTML_="convert to bmp image format"
    PURPOSE_="convert image file to bmp format"
   SYNOPSIS_="$NAME_ [-vhlr] <file> [file...]"
   REQUIRES_="standard GNU commands, ImageMagick"
    VERSION_="1.1"
       DATE_="2004-06-25; last update: 2004-07-08"
     AUTHOR_="Dawid Michalczyk <dm@eonworks.com>"
        URL_="www.comp.eonworks.com"
   CATEGORY_="gfx"
   PLATFORM_="Linux"
      SHELL_="bash"
 DISTRIBUTE_="yes"

# #############################################################################
# This program is distributed under the terms of the GNU General Public License

usage () {

echo >&2 "$NAME_ $VERSION_ - $PURPOSE_
Usage: $SYNOPSIS_
Requires: $REQUIRES_
Options:
     -r, remove the input file after conversion
     -v, verbose
     -h, usage and options (this help)
     -l, see this script"
    exit 1
}

# args check
[ $# -eq 0 ] && { echo >&2 missing argument, type $NAME_ -h for help; exit 1; }

# var init
rm_input=
verbose=

# option and argument handling
while getopts vhlr options; do

    case "$options" in
        r) rm_input=on ;;
        v) verbose=on ;;
        h) usage ;;
        l) more $0; exit 1 ;;
       \?) echo invalid argument, type $NAME_ -h for help; exit 1 ;;
    esac

done
shift $(( $OPTIND - 1 ))

# check if required command is in $PATH variable
which mogrify &> /dev/null
[[ $? != 0 ]] && { echo >&2 the required ImageMagick \"mogrify\" command \
is not in your PATH variable; exit 1; }

# main execution
for a in "$@"; do

    newf=$(echo "${a%.*}".bmp)
    if [ -f "$newf" ]; then
        echo "${NAME_}: skipping converting $a - $newf already exist" && continue
    else
        [ -f "$a" ] && mogrify -format bmp $a || continue
        [[ $verbose ]] && echo "${NAME_}: $a -> $newf"
        [[ $rm_input ]] && rm -f -- "$a"
    fi

done
