#!/bin/sh

graphics_link()
{
  gtemp=""
  for i in $*; do
    # Copy locally the graphics
    if [ -d $i ]; then
      for i in $i/*.pdf ; do
        gloc=`basename $i`
        if [ ! -f $gloc ]; then
          echo "Link to $gloc"
          ln -s $i .
          gtemp="$gtemp $gloc"
        fi
      done
    else
      echo "Warning: invalid graphic path"
    fi
  done
}

scriptpath=`dirname $0`;

# Grab the graphpath option
get_graphpath()
{
  while true
  do
    case "$1" in
    --graphpath) graphpath="$2"; shift;;
    *) break;;
    esac
    shift
  done
}

get_graphpath "$@"
graphics_link "$graphpath" $scriptpath/../graphics

$scriptpath/runlatex --tex pdflatex --oxt pdf "$@"
rc=$?

# Remove the temporary links
if [ "$gtemp" != "" ]; then
  rm $gtemp
fi

exit $rc
