#!/bin/sh
# This is a wrapper script to let LyX/Cygwin use either a native Win32 or
# Cygwin version of convert (part of ImageMagick).
#
# This script simply changes all input pathnames into Windows or Cygwin
# pathnames depending on which version of convert is being used.
# As an exception, files in emf format require Windows style paths, even
# if using the cygwin version (maybe some native API is being called).
# If the pathnames contain spaces, either enclose them in double quotes
# or escape the spaces. For example:
# convert "eps:/path with/spaces.pdf" "png:file with spaces.eps"
# convert eps:/path\ with/spaces.pdf png:file\ with\ spaces.eps
#
# 2008-04-30 Enrico Forestieri
# =======================================================================

# The program to call (This should be in the PATH)
# Warning: if ImageMagick isn't installed, the Windows'
# file system convert utility will be called!
prog=convert.exe

# Check whether we are using the cygwin version
if [ "`which $prog 2>/dev/null`" = "/usr/bin/convert.exe" ]; then
    flag=""
else
    flag="-m"
    export TEMP=/tmp
fi

while [ $# -gt 0 ] ; do
    case "$1" in
	?:*)  ext=`echo "$1" | sed 's/.*\.\([^.]*\)/\1/'`
	      # emf needs Win32 style paths even with the cygwin version
	      if [ "$ext" = "emf" ]; then
		  prog="$prog '`cygpath -m -- "$1"`'"
	      else
		  prog="$prog '`cygpath $flag -- "$1"`'"
	      fi
	      ;;
	*:*)  fmt=`echo "$1" | cut -d: -f1`
	      file=`echo "$1" | cut -d: -f2-`
	      # emf needs Win32 style paths even with the cygwin version
	      if [ "$fmt" = "emf" ]; then
		  file=`cygpath -m -- "$file"`
	      else
		  file=`cygpath $flag -- "$file"`
	      fi
	      prog="$prog '$fmt:$file'"
	      ;;
	-*)   prog="$prog $1"
	      ;;
	*)    ext=`echo "$1" | sed 's/.*\.\([^.]*\)/\1/'`
	      # emf needs Win32 style paths even with the cygwin version
	      if [ "$ext" = "emf" ]; then
		  prog="$prog '`cygpath -m -- "$1"`'"
	      else
		  prog="$prog '`cygpath $flag -- "$1"`'"
	      fi
	      ;;
    esac
    shift
done

eval "exec $prog"
