#!/bin/sh

if [ `uname -s` == "Darwin" ] ; then 
	MAGICK_HOME="/usr/local/ImageMagick"
	export MAGICK_HOME
	PATH="$MAGICK_HOME/bin:/usr/local/bin:$PATH"
	export DYLD_LIBRARY_PATH="$MAGICK_HOME/lib"
	export PATH
else
	PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"
	export PATH
fi

MODE=${1}
originalFile=${2}
thumbSize=${3}
finalFile=${4}

ulimit -v 1500000

if [ ${MODE} == "psd" ] ; then 
	convert ${originalFile} -colorspace rgb -depth 8 -render -flatten -resize  ${thumbSize}x${thumbSize} ${finalFile}
elif [ ${MODE} == "pdf" ] ; then 
	gs -q -dNOPAUSE -dBATCH -dFirstPage=1 -dLastPage=1 -sDEVICE=jpeg -sOutputFile=${finalFile}.jpg ${originalFile}
	convert -colorspace rgb ${finalFile}.jpg -colorspace rgb -depth 8 -resize  ${thumbSize}x${thumbSize} ${finalFile}
	rm ${finalFile}.jpg
else
	convert ${originalFile} -colorspace rgb -depth 8 -resize  ${thumbSize}x${thumbSize} ${finalFile}
fi
exit 0