#! /bin/bash

if test "${1+set}" != "set"
then 
  echo "Usage; lncs2up.sh file [...]"
  exit 1
fi

function do_pstops {
  if test "${1+set}" != "set"
  then 
    echo "Usage: do_pstops name"
    exit 1
  elif test -x "$1.ps"
  then
    echo "File doesn't exist: $1.ps"
    exit 1
  fi

  echo "Writing $1.2up.ps"

# Edit this command to tweak 2-up formatting. See:
# http://procrastiblog.blogspot.com/2007/01/printing-lncs-format-pdfs-wiki-import.html
  pstops '2:0L@.8(8.5in,-.25in)+1L@.8(8.5in,4.5in)' $1.ps $1.2up.ps
}

while test "${1+set}"
do
  base="${1%.*}"
  suffix="${1##*.}"
  case $suffix in
    pdf)
      if test -e "$base.ps"
      then
        read -p "$base.ps exists. Overwrite? [y/N] " yesno
        case $yesno in
          "y" | "Y" | "ye" | "YE" | "yes" | "YES" ) keep_ps=1;;
          *) shift; continue;;
        esac
      fi

      acroread -toPostScript -size letter $1
      do_pstops $base
      if test $keep_ps -ne 1
      then rm -rf $base.ps 
      fi
      ;;
    
    ps)
      do_pstops $base 
      ;;

    *)
      echo Invalid input file: $1. Must be PDF or PS
      exit 1 
      ;;
  esac
  shift
done

