#!/bin/sh
version=1.2
#set -x
################################################################################
#
# mkbook - makes an A5-booklet from a PostScript file
#
# Copyright (C) 1999-2001, 2007 Dimitar Ivanov, <dimitar.ivanov@mirendom.net>
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
################################################################################
# np=`grep %%Pages $1 |grep [0-9] |cut -f2 -d  |head -1`
progname=`basename $0`
sheets=1          # sheets per signature
offset=0.0        #
voff=21.0         #
Hshif=0.0         #
Vshif=0.0         #
Hsymmetric=no     #
Vsymmetric=no     #
prev=             # Reversing order of the pages
direct=no         # Use cat for piping the input
psin=cat          # Page inserting possible if 'psin' available (see -n option)
psinok=`which psin |grep -E "/psin"`
################################################
gops="hdinrs:So:H:V:"
set -- `getopt $gops $*`

if [ $? != 0 -o $# -eq 1 ]; then
    set -- "-h"
fi
cline="%%`basename $0` $*"
while [ $# -gt 0 ]
do
    case $1 in
	-i) interactive=1;;
	-d) direct=yes;;
	-r) prev="-";;
	-s) shift
	    sheets=$1;;
	-S) sheets=0;;
	-n) if [ -n "$psinok" ]; then
           psin=psin
        fi
        ;;
	-o) shift
	    offset=$1;;
	-H) shift
		Hshif=$1
		Hsymmetric=yes;;
	-V) shift
		Vshif=$1
		Vsymmetric=yes;;
    -h) 
	    cat << HELP
--------------------------------------------------------------------------------
mkbook $version: makes an A5-booklets from a PostScript file (requires psutils)
--------------------------------------------------------------------------------

Usage: $progname [option(s)] <ps_file(s)>
   -r         = reverse page ordering for the back sides
   -s <int>   = number of sheets for signature (default=$sheets).
                4 text pages fit on one sheet of paper.
   -S         = try to find automatically the number of pages and create
                the booklet as a single signature
   -o <float> = offset in cm (default=${offset})
   -H <float> = symmetric shift in cm (default=${Hshif})
   -V <float> = vertical symmetric shift in cm (default=${Vshif})
   -n         = page numbering (needs 'psin')
   -d         = do not use 'cat' for piping input (useful for very large files)
   -i         = interactive mode useful for direct printing. Otherwise,
                the default output is: <file>.f.ps (front pages)
                                       <file>.b.ps (back pages)
   -h         = this short description

HELP
	    exit 0;;
	--) shift
	    infile=$*
	    break;;
    esac
    shift
done

for file in $infile
do
   if [ ! -f "$file" -o -z "$file" ];
   then
      echo "Can't open file $file"
      continue
   fi

   if [ $sheets -eq 0 ]; then
        sheets=`grep "%%Pages: [0-9]*" $file |tail -1 |cut -f2 -d" "`
        [ $sheets -eq 0 ] && \
          echo "Can't find  the number of pages: use '-s' option" && \
          exit 1
        signature=`echo "scale=0; (($sheets+3)/4)*4" |bc`
        sheets=0
   else
        signature=`echo "scale=0; $sheets*4" |bc`
   fi

   off0="`echo \"    0 - $offset\" | bc -l`"
   off1="`echo \"14.85 + $offset\" | bc -l`"
   if [ $Hsymmetric = "yes" ]; then
      off0="`echo \"$off0 - $Hshif\" | bc -l`"
      off1="`echo \"$off1 - $Hshif\" | bc -l`"
   fi
   if [ $Vsymmetric = "yes" ]; then
      voff="`echo \"21 - $Vshif\" | bc -l`"
   fi
   off0="${off0}cm"
   off1="${off1}cm"
   voff="${voff}cm"

   frt_format="4:0L@.7($voff,$off0)+1L@.7($voff,$off1)"
   bck_format="4:${prev}2L@.7($voff,$off0)+${prev}3L@.7($voff,$off1)"

   frt_pages="$psin $file |psbook -s$signature |pstops \"$frt_format\""
   bck_pages="$psin $file |psbook -s$signature |pstops \"$bck_format\""

   if [ $direct = "yes" ]; then  # do not use cat for pipe
      frt_pages="psbook -s$signature $file |pstops \"$frt_format\""
      bck_pages="psbook -s$signature $file |pstops \"$bck_format\""
   fi

   if [ -z "$interactive" ]
   then
      echo "Writing front pages in $file.f.ps ..."
      eval $frt_pages |awk -v cl="$cline" '{ if( $0 == "%%EOF" ) { print cl; } print $0; }' > $file.f.ps
      echo "Writing back pages in $file.b.ps ..."
      eval $bck_pages |awk -v cl="$cline" '{ if( $0 == "%%EOF" ) { print cl; } print $0; }' > $file.b.ps
      echo "Front pages:   $file.f.ps"
      echo "Back pages:    $file.b.ps"
   else
      echo "Print front pages of $file (y/n):"
      read pro 
      if [ "$pro" = "y" ]; then
	     eval $frt_pages | lpr -h
      fi
    
      echo "Print back pages of $file (y/n):"
      read pro 
      if [ "$pro" = "y" ]; then
	     eval $bck_pages | lpr -h
      fi
   fi
done

exit 0
