#!/bin/bash
#
# digitalizar.sh es un sencillo script para digitalizar documentos.
# Copyright (C) 2010 Jaime Gil de Sagredo Luna <jaimegildesagredo@gmail.com>
#
# 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/>.

scan_resolution=150
scan_mode="lineart"
scan_size_x="210mm"
scan_size_y="297mm"

image_compressor=cjb2
tmpfile="tmp/image"

outputfile="output.djvu"

function help_()
{
	echo "Modo de uso: ${0} [opciones]"
	echo
	echo "Opciones:"
	echo -e "-h --help\tMuestra la ayuda."
	echo -e "-s --scanner ESCANER\tUsa el ESCANER indicado."
	echo -e "-o --output FICHERO\tGuarda el documento en el FICHERO."
	echo -e "-x X\tDimesion X del documento."
	echo -e "-y Y\tDimesion Y del documento."
	echo -e "-o --output RESOLUCIóN\tEscanea el documento con la RESULOCIÓN dada."
	echo

}

while [[ $# != 0 ]];
do
	case $1 in
	-h|--help)
		help_
		exit 0
		;;
	-s|--scanner)
		shift
		scan_device=${1}
		;;
	-o|--output)
		shift
		outputfile=${1}
		;;
	-x)
		shift
		scan_size_x=${1}
		;;
	-y)
		shift
		scan_size_y=${1}
		;;
	-r|--resolution)
		shift
		scan_resolution=${1}
		;;
	*)
		echo "Argumento ${1} inválido."
		exit 1
		;;
	esac
	shift
done

if [[ -z  ${scan_device} ]];
then
	echo "Detectando dispositivos de entrada..."
	scanimage_output=$(scanimage -L)
	if [[ $(echo ${scanimage_output} | grep "No scanners were identified.") ]];
	then
		echo "No se encontró ningún escáner."
		exit 1
	else
		scan_device=$(echo ${scanimage_output} | awk '{ print $2 }' | sed -e 's/`//' -e "s/'//")
	fi
fi

num=0
while true;
do
	echo "Pulse una tecla para escanear o elija el modo de escaneo 'blanco y (n)egro', '(c)olor' o '(s)alir': "
	read option
	if [[ ${option} == "c" ]];
	then
		scan_mode="color"
		image_compressor=c44
	elif [[ ${option} == "s" ]];
	then
		break
	else
		scan_mode="lineart"
		image_compressor=cjb2
	fi

	scanimage --device-name ${scan_device} --format pnm --mode ${scan_mode} \
			--resolution ${scan_resolution} \
			-x ${scan_size_x} -y ${scan_size_y} > ${tmpfile}${num}.pnm
	if [[ $? != 0 ]];
	then
		echo "Error al escanear la página."
		exit 1
	fi
	${image_compressor} ${tmpfile}${num}.pnm ${tmpfile}${num}.djvu
	if [[ $? != 0 ]];
	then
		echo "Error al comprimir la imagen."
		exit 1
	fi

	if [[ ! -f ${outputfile} ]];
	then
		djvm -c ${outputfile} ${tmpfile}${num}.djvu
	else
		djvm -i ${outputfile} ${tmpfile}${num}.djvu
	fi
	
	rm ${tmpfile}${num}.djvu ${tmpfile}${num}.pnm
	num=$((${num}+1))
done

echo "Se han digitalizado ${num} páginas correctamente."
exit 0


