#!/bin/bash
#  Copyright (c) 2009 Christoph Langner
#
#  Author: Christoph Langner <mail@christoph-langner.de>
#
#  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.
#
#  scan2pdf 1.0 by Christoph Langner
#
#  Changelog
#  1.0 Initial Release
#  
#  Following packages are required
#  * sane-utils
#  * zenity
#  * libtiff-tools
#  * ghostscript

 ## SCAN settings
FORMAT="-l 0 -t 0 -x 210 -y 297"    # Keep this for A4

## PDF infos
TITLE="Scan"                        # Titel

## OUTPUT settings
OUTDIR="$HOME/Desktop"              # Where to place your scans
OUTFILE="scan"                      # Filename

## Script
OUTNR=1
TMPFILE=$(tempfile)
CFG_FILE=~/.scan2pdf
NO_ARGS=0
E_OPTERROR=65

# Check dependencies
if [ ! -f /usr/bin/tiff2pdf ]
then
	echo "Aborting. tiff2pdf is missing. Please install libtiff-tools"
	exit 1
fi
if [ ! -f /usr/bin/scanimage ]
then
	echo "Aborting... scanimage is missing. Please install sane-utils"
	exit 1
fi
if [ ! -f /usr/bin/pdfopt ]
then
	echo "Aborting... pdfopt is missing. Please install ghostscript"
	exit 1
fi
                
# Scanner setup
if [ ! -f $CFG_FILE ]
then
	echo "Searching for devices. Please wait... "
	DEVS=`scanimage -f '%d '`
	SCANNER=`echo -e ${DEVS// /\\\n} | zenity --list --title "Select your scanner" --text "Please choose your scanner out of this list" --column "Scanner"`
	if [ $? = "0" ]
	then
		echo "SCANNER=$SCANNER" > $CFG_FILE
		scanimage --help --device $SCANNER | grep -q progress
		if [ $? -eq 0 ]
		then
			echo "CANSHOWPROGRESS=1" >> $CFG_FILE
			CANSHOWPROGRESS=1
		else
			echo "CANSHOWPROGRESS=0" >> $CFG_FILE
			CANSHOWPROGRESS=0
		fi
	else
		zenity --error --text "No scanner selected. "
		exit -1
	fi
else
	. $CFG_FILE
fi

# Scanner config
if [[ $SCANNER =~ plustek ]]
then
	CALCACHE="--calibration-cache=yes"
	LINEART="lineart"
	GRAY="gray"
	COLOR="color"
elif [[ $SCANNER =~ epson || $SCANNER =~ epkowa ]]
then
	LINEART="binary"
	GRAY="gray"
	COLOR="color"
fi

if [ $CANSHOWPROGRESS -eq 1 ]
then
	PROGRESS="-p"
else
	PROGRESS=""
fi

# function to display help
function displayhelp {
	echo "Usage: `basename $0` -m {lineart,gray,color} -r [resolution in dpi]"
	echo " -m: Scan mode, choose lineart, gray or color"
	echo " -r: Resolution in dpi"
	exit $E_OPTERROR        # Exit and explain usage, if no argument(s) given.
}


# Display help
if [ $# -eq "$NO_ARGS" ]  
then
	displayhelp
elif [ $1 = "--help" -o $1 = "-help" -o $1 = "-?" -o $1 = "-h" ]
then
	displayhelp
fi

# Get options
while getopts ":m:r:bo" Option
do
  case $Option in
    m ) MODE=$OPTARG;;
    r ) RES=$OPTARG ;;
  esac
done

# adjust modes
if [ $MODE = lineart ]
then
	SCANMODE=$LINEART
elif [ $MODE = gray ]
then
	SCANMODE=$GRAY
elif [ $MODE = color ]
then
	SCANMODE=$COLOR
else
	echo "Scan mode can only be lineart, gray or color"
	exit $E_OPTERROR
fi

# scan
scanimage -d $SCANNER --format tiff --mode $SCANMODE $FORMAT --resolution $RES $CALCACHE $PROGRESS > $TMPFILE.tif

# convert
AUTHOR=$(grep $UID /etc/passwd | awk -F: '{print$5}')
AUTHOR=${AUTHOR:0:${#AUTHOR}-3}
tiff2pdf -z $TMPFILE.tif -o $TMPFILE.pdf -j -a"$AUTHOR" -c"$AUTHOR" -t"$TITLE"
pdfopt $TMPFILE.pdf $TMPFILE.opt.pdf

# copy
while [ -f $OUTDIR/$OUTFILE$OUTNR.pdf ]
	do
		OUTNR=$[$OUTNR+1]
	done
	cp $TMPFILE.opt.pdf $OUTDIR/$OUTFILE$OUTNR.pdf
exit 0

