#!/bin/bash
# Author: Mikolaj Machowski (mikmach AT wp DOT pl)
# vim:set ft=sh:

function help_message () {
	echo Version: IMpos 1.0 11.10.2006
	echo Copyright: Mikolaj Machowski 2006 \( mikmach AT wp DOT pl \)
	echo Usage: impos -o format [options]
	echo
	echo Where options are:
	echo -e "  -c\t - color of background (default: white)"
	echo -e "  -d\t - directory for outut (default: imposout)"
	echo -e "  -h\t - this help"
	echo -e "  -m\t - multiply first image to fill empty sheets"
	echo -e "  -o\t - format of transformation, arguments are"
	echo -e "\t\t2up\t- 2-Up"
	echo -e "\t\t4ss\t- 4-Up Saddle-Stitching"
	echo -e "\t\t4tumble\t- 4-Up Work-and-Tumble"
	echo -e "\t\t4turn\t- 4-Up Work-and-Turn"
	echo -e "\t\t8tumble\t- 8-Up Work-and-Tumble"
	echo -e "\t\t8turn\t- 8-Up Work-and-Turn"
	echo
	echo More info about imposition on: http://www.prepress.pps.com/TechReports/imposetr.html
}
while getopts ":bc:d:fhjmo:s" Option; do
	case $Option in
		a ) echo Crop, field and registration marks are not supported \(yet\);;
		b ) echo Color end density bars are not supported;;
		c ) color=$OPTARG;;
		d ) directory=$OPTARG;;
		h ) help_message
			exit ;;
		j ) echo Job information is not yet supported;;
		m ) multiply=1;;
		o ) if [ $OPTARG == '2up' ]; then
				output=$OPTARG; nup=2; cycle=$(($nup*2))
			elif [ $OPTARG == '4ss' ]; then
				output=$OPTARG; nup=4; cycle=$(($nup*2))
			elif [ $OPTARG == '4tumble' ]; then
				output=$OPTARG; nup=4; cycle=$nup
			elif [ $OPTARG == '4turn' ]; then
				output=$OPTARG; nup=4; cycle=$nup
			elif [ $OPTARG == '8tumble' ]; then
				output=$OPTARG; nup=8; cycle=$nup
			elif [ $OPTARG == '8turn' ]; then
				output=$OPTARG; nup=8; cycle=$nup
			else
				echo Unknown output transformation
				echo Allowed arguments for option -o are:
				echo -e "2up\t- 2-Up"
				echo -e "4ss\t- 4-Up Saddle-Stitching"
				echo -e "4tumble\t- 4-Up Work-and-Tumble"
				echo -e "4turn\t- 4-Up Work-and-Turn"
				echo -e "8tumble\t- 8-Up Work-and-Tumble"
				echo -e "8turn\t- 8-Up Work-and-Turn"
				exit
			fi;;
		s ) echo Bleed space not supported;;
	esac
done

# Transform format is required for working of this script
if [ -z $cycle ]; then
	help_message
	exit
fi

# Directory for output creation
if [ -z $directory ]; then
	directory=imposout
fi
# Fill empty sheets with first image
if [ -z $multiply ]; then
	multiply=0
fi
# CAVEAT: Default color is white
if [ -z $color ]; then
	color=white
fi
# CAVEAT: unlawful characters, spaces, etc.
mkdir -p $directory

FILES=(`ls *.png`)
HEIGHT=`identify -format %h $FILES[0]`
WIDTH=`identify -format %w $FILES[0]`
RESOLUTION=`identify -format %x $FILES[0] | cut -d' ' -f1`
RESUNITS=`identify -format %x $FILES[0] | cut -d' ' -f2`
RSTRING="-density $RESOLUTION -units $RESUNITS"

#Number of files
NUMBEROFFILES=${#FILES[*]}
# We need $MISS faux pages to fill sheets
MISS=$(($cycle - ($NUMBEROFFILES % $cycle)))
if [ $MISS -eq $cycle ]; then
	MISS=0
fi
i=1
while [ $i -le $MISS ]; do
	if [ $multiply -eq 1 ]; then
		cp ${FILES[0]} zzzzzzzzzzzzzzzzzz$i.png
	else
		convert -size "$WIDTH x $HEIGHT" xc:white zzzzzzzzzzzzzzzzzz$i.png
	fi
	i=$((i+1))
done
unset i

# Recreate list of files
unset FILES
unset NUMBEROFFILES
FILES=(`ls *.png`)
NUMBEROFFILES=${#FILES[*]}
cycles=$(($NUMBEROFFILES/$cycle))
i=0
while [ $i -lt $cycles ]; do
	p1=${FILES[$((0+$cycle*$i))]}
	p2=${FILES[$((1+$cycle*$i))]}
	p3=${FILES[$((2+$cycle*$i))]}
	p4=${FILES[$((3+$cycle*$i))]}
	convert -shave 1x1 -border 1x1 -bordercolor LightGray $p1 b$p1
	convert -shave 1x1 -border 1x1 -bordercolor LightGray $p2 b$p2
	convert -shave 1x1 -border 1x1 -bordercolor LightGray $p3 b$p3
	convert -shave 1x1 -border 1x1 -bordercolor LightGray $p4 b$p4
	if [ $cycle -eq 8 ]; then
		p5=${FILES[$((4+$cycle*$i))]}
		p6=${FILES[$((5+$cycle*$i))]}
		p7=${FILES[$((6+$cycle*$i))]}
		p8=${FILES[$((7+$cycle*$i))]}
		convert -shave 1x1 -border 1x1 -bordercolor LightGray $p5 b$p5
		convert -shave 1x1 -border 1x1 -bordercolor LightGray $p6 b$p6
		convert -shave 1x1 -border 1x1 -bordercolor LightGray $p7 b$p7
		convert -shave 1x1 -border 1x1 -bordercolor LightGray $p8 b$p8
	fi
	# Loop counter incrementation - here
	i=$((i+1))
	# Nulls filling for better sorting of output files
	if [ $i -lt 10 ]; then
		nulls=00
	elif [ $i -lt 100]; then
		nulls=0
	else
		nulls=
	fi
	if [ $output == '2up' ]; then
		montage -geometry +0+0 -tile 1x2 $RSTRING b$p4 b$p1 -rotate 90 $nulls$i-1.png
		montage -geometry +0+0 -tile 1x2 $RSTRING b$p2 b$p3 -rotate 90 $nulls$i-2.png
		convert -shave 1x1 -border 1x1 -bordercolor $color $nulls$i-1.png $nulls$i-1.png
		convert -shave 1x1 -border 1x1 -bordercolor $color $nulls$i-2.png $nulls$i-2.png
		mv -f $nulls$i-* $directory
	elif [ $output == '4ss' ]; then
		montage -geometry +0+0 -tile 2x2 $RSTRING \( -rotate 180 b$p5 b$p4 \) b$p8 b$p1 $nulls$i-1.png
		montage -geometry +0+0 -tile 2x2 $RSTRING \( -rotate 180 b$p3 b$p6 \) b$p2 b$p7 $nulls$i-2.png
		convert -shave 1x1 -border 1x1 -bordercolor $color $nulls$i-1.png $nulls$i-1.png
		convert -shave 1x1 -border 1x1 -bordercolor $color $nulls$i-2.png $nulls$i-2.png
		mv -f $nulls$i-* $directory
	elif [ $output == '4turn' ]; then
		montage -geometry +0+0 -tile 2x2 $RSTRING \( -rotate 180 b$p3 b$p4 \) b$p1 b$p2 $nulls$i-1.png
		convert -shave 1x1 -border 1x1 -bordercolor $color $nulls$i-1.png $nulls$i-1.png
		mv -f $nulls$i-* $directory
	elif [ $output == '4tumble' ]; then
		montage -geometry +0+0 -tile 2x2 $RSTRING \( -rotate 180 b$p2 b$p4 \) b$p1 b$p3 $nulls$i-1.png
		convert -shave 1x1 -border 1x1 -bordercolor $color $nulls$i-1.png $nulls$i-1.png
		mv -f $nulls$i-* $directory
	elif [ $output == '8turn' ]; then
		montage -geometry +0+0 -tile 4x2 $RSTRING \
			\( -rotate 180 b$p5 b$p4 b$p3 b$p6 \) \
			b$p8 b$p1 b$p2 b$p7 $nulls$i-1.png
		convert -rotate 90 $nulls$i-1.png $nulls$i-1.png
		convert -shave 1x1 -border 1x1 -bordercolor $color $nulls$i-1.png $nulls$i-1.png
		mv -f $nulls$i-* $directory
	elif [ $output == '8tumble' ]; then
		montage -geometry +0+0 -tile 4x2 $RSTRING \
			\( -rotate 180 b$p7 b$p2 b$p3 b$p6 \) \
			b$p8 b$p1 b$p4 b$p5 $nulls$i-1.png
		convert -rotate 90 $nulls$i-1.png $nulls$i-1.png
		convert -shave 1x1 -border 1x1 -bordercolor $color $nulls$i-1.png $nulls$i-1.png
		mv -f $nulls$i-* $directory
	fi
	# Remove auxiliary files
	rm -f b$p1 b$p2 b$p3 b$p4 b$p5 b$p6 b$p7 b$p8
done
unset i
# Remove auxiliary faux pages
rm -f zzzzzzzzzzzzzzzzzz*.png
rm -f bzzzzzzzzzzzzzzzzzz*.png
