#!/bin/bash
#-------------------------- =+- Shell script -+= --------------------------
# @file      draftpaper.sh
# @date      Mon Mar  8 12:11:55 2004
# @brief
#
#  $RCSfile: draftpaper.sh,v $
#  $Source: /home/cvs/yoh/soft/draftpaper.sh,v $
#
# Created: Mon Mar  8 12:11:55 2004
#  Commited: $Date: 2006/04/05 15:41:41 $
#  Revision: $Revision: 1.5 $
#
#  Yaroslav Halchenko                                      CS@UNM, CS@NJIT
#  web:     http://www.onerussian.com                      & PSYCH@RUTGERS
#  e-mail:  yoh@onerussian.com                              ICQ#: 60653192
#
# DESCRIPTION (NOTES):
#
#    Simple script intended to be used as a wrapper around impose+
#    http://imagic.weizmann.ac.il/~dov/freesw/impose+/ or impose+
#    package under Debian and also other tools (+acroread, xpdf, and
#    others) for ease of printing drafts of the paper in
#    readable/paper-saving format: duplex 2 pages per sheet, with
#    margins as small as impose could guess. Can accept PS, PDF files
#    as input argument or simple url (file will be downloaded, printed
#    and removed)
#
#    Usually impose (bboxx to be precise) does better job than mpage
#    in extracting Bounding Box parameter from the postscript and then
#    using that size for proper rescaling of the material from 2 pages
#    onto a single sheet.
#
# COPYRIGHT: Yaroslav Halchenko 2005, 2006
#
# LICENSE:
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the
#  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
# On Debian system see /usr/share/common-licenses/GPL for the full license.
#-----------------\____________________________________/------------------
#set -x

justprint=false
justps=false
deleteafter=true
takestdin=false
printopt=""

while [ $# -ge 0 ]
  do
  case $1 in
      -p)
	  justprint=true
	  shift

	  ;;
      -P)
	  justprint=true
	  shift
	  printopt="-P $1 "
	  shift
	  ;;

      -i)
	  takestdin=true
          shift;;

      -s)
	  justps=true
	  shift
	  ;;

      -d)
	  deleteafter=false
	  shift
	  ;;

      -*)
	  echo "$0: unrecognized option $1" >&2
	  shift
	  ;;
      *)
	  break;
	  ;;
  esac
done

url="$1"
if [ "x$url" == "x" ] || [ $takestdin == true ]
then
#    echo "No file specified - taking stdin"
    psname=`tempfile`
    rmps=0
    impose -stdout -letter >| "$psname.imposed"
else

    if [ "x${url##http://}" = "x${url}" ] || [ "x${url##ftp://}" = "x${url}" ]
	then
	    fname="$url"
	else
	    fname="${url//*\/}"
	    wget "$url"
	fi

	rmps=0;
	ext="${fname//*./}"
	case $ext in
	    djvu)
		psname="${fname//.djvu/.ps}"
		djvups "$fname" > "$psname"
		rmps=1;
		;;
	    pdf)
		psname="${fname//.pdf/.ps}"
		acroread -toPostScript "$fname";
		rmps=1;
		;;
	    gz)
		psname="${fname//.ps.gz/.ps}";
		gunzip "$fname";
		;;
	    ps)
		psname="$fname"
		;;
	    *)
		type=`file -bi "$fname"`
#		echo "Unknown extension $ext. Type: $type";
		case $type in
		    application/postscript)
			psname=$fname;
			;;
		    *)
			echo "Unknown type $type";
			exit 1;
		esac
#		exit 1;
	esac

	impose -stdout -letter "$psname" > "$psname.imposed"
fi

if [ $justprint = true ]
then
    lpr $printopt "${psname}.imposed"
else
    if [ $justps = true ]
    then
       gv "${psname}.imposed"
    else
       ps2pdf "${psname}.imposed" "${psname}.imposed.pdf"
     #acroread "${psname}.imposed.pdf"
       xpdf "${psname}.imposed.pdf"
    fi
fi

if [ $deleteafter = true ]
then
    test $rmps -eq 1 && rm "${psname}"
    # could not figure out the rigth way :-/
    rm ${psname// /?}.imposed*
fi
