#!/bin/bash

# This program depends on:
#     texlive (epstopdf)
#     pdfjam (pdfjoin)
#     imagemagick (convert)

set -e

if [ $# -lt 2 ]; then
	exit -1
fi

eval last=\$$#
if [ -f $last ]; then
	echo -n "Override $last? [y/N] "
	read decision
	if [ "$decision" != 'y' ]; then
		exit -1
	fi
fi
set -- ${@:1:$#-1}

for img in $@; do
	eps=$(mktemp tmp-XXXXXXXXX.eps)
	pdf=$(mktemp tmp-XXXXXXXXX.pdf)
	pdfs="$pdf $pdfs"
	epss="$eps $epss"
	trap "rm -f $pdfs $epss" EXIT
	convert $img $eps
	epstopdf --outfile=$pdf $eps
done

pdfjoin -o $last $pdfs
