#!/bin/bash
#
# Author: Ben Kochie <ben@nerp.net>
# Date: 2007-09-02
# Homepage: http://ben.nerp.net/scan_docs.sh
# Version: 1.0
# 
# Automatic scan/conversion script
# Requires sane, imagemagick, and unpaper
#
# Thanks to Adam Drake for the original version of this script.
# Original: http://blogcritics.org/archives/2006/01/31/222424.php

bins="scanadf unpaper convert"
for bin in scanadf unpaper convert; do
  bin="$(type ${bin} 2> /dev/null)"
  if [ $? -ne 0 ] ; then
    echo "ERROR: Unable to find ${bin}"
    exit 1
  fi
done

# Function to print usage statement
usage() {
  cat << USAGE
usage: $(basename $0) -gclL NUM
colormode: (must have one)
  -m    Mono  (400 dpi)
  -g    Grey  (150 dpi) (default)
  -c    Color (100 dpi)
sizes:
  -l    Letter  (215.9 x 279.4mm) (default)
  -L    Legal   (215.9 x 355.6mm)
  -a    A4      (210.0 x 297.0mm)
  -U    US Bank (190.0 x 280.0mm)
options:
  -1    Single multi-page document mode
  -q N  Image quality % (1-100) (default 70)
  -r N  DPI Resolution
USAGE
}

# Default quality and docmode settings
docmode=0
quality="-quality 70"
# You can get scandev with "scanadf -L"
scandev="hpaio:/net/Officejet_Pro_L7500?ip=192.168.19.2"

# Default to letter
SIZE="letter"
WIDTH="215.9"
HEIGHT="279.4" 

# Default to Mono mode
MODE="Gray"
RES="150"

# Variables for getopts
OPTARG="" OPTIND=0 opt=""
while getopts "acglLmq:r:U1" opt; do
  case ${opt} in
    a) SIZE="a4" WIDTH="210" HEIGHT="297" ;;
    c) MODE="Color" RES="100" ;;
    g) MODE="Gray" RES="150" ;;
    l) SIZE="letter" WIDTH="215.9" HEIGHT="279.4" ;;
    L) SIZE="legal" WIDTH="215.9" HEIGHT="355.6" ;;
    m) MODE="Lineart" RES="400" ;;
    q) quality="-quality ${OPTARG}" ;;
    r) RES="${OPTARG}" ;;
    U) SIZE="USBank" WIDTH="190" HEIGHT="280" ;;
    1) docmode=1 ;;
    *) echo "Invalid argument."
       usage
       exit 1
       ;;
  esac
done 
shift $((${OPTIND} - 1))

# Get the number of documents to scan
num="$1"

# Validate Input
if [ -z "${num}" ] ; then
  usage
  exit 1
fi

if [ "${num}" -lt 0 ] ; then
  usage
  exit 1
fi

# Get a temp directory for scanning to
scandir="$(mktemp -d -p /var/tmp scan-XXXXXX)"
if [ ! -d "${scandir}" ] ; then
  echo "ERROR: Unable to get a tempdir"
  exit 1
fi

echo "Scanning documents"
pushd "${scandir}"
scanadf --output-file "doc-%04d" -e "${num}" --mode "${MODE}" \
  -d "${scandev}" \
  --resolution "${RES}" -x "${WIDTH}" -y "${HEIGHT}"
docs="$(ls doc-*)"
popd

# Scan each page
scaned_files=""
n=1 docdate=""
if [ -n "${docs}" ] ; then
  for file in ${docs}; do
    scan="${scandir}/${file}"
    # Read a date code for the document, only once if we're in multipage mode
    if [ -z "${docdate}" -o ${docmode} -eq 0 ] ; then
      echo -n "Enter document date: "
      read docdate
    fi
    # Set filename for final document
    if [ ${docmode} -eq 0 ] ; then
      newfile="doc-${docdate}.png"
    else
      newfile="doc-${docdate}_page${n}.png"
      ((n += 1))
    fi
    if [ -f "${newfile}" ] ; then
      echo "ERROR ${newfile} exists"
      continue
    fi
    # unpaper the scan
    unpaper --dpi "${RES}" "${scan}" "${file}"
    # Convert to PNG
    convert -comment "Size=${SIZE}" ${quality} "${file}" "${newfile}"
    scaned_files="${scaned_files} ${newfile}"
    ls -lh "${scan}" "${newfile}"
    rm "${scan}" "${file}"
  done
else
  # Whups! probably a scanner error
  echo "No documents scanned"
fi

echo "Converting to PDF"
if [ ${docmode} -eq 0 ] ; then
  for file in ${scaned_files}; do
    convert -compress zip ${file} "doc-${docdate}.pdf"
    ls -lh "doc-${docdate}.pdf"
  done
else
  convert -compress zip ${scaned_files} "doc-${docdate}.pdf"
  ls -lh "doc-${docdate}.pdf"
fi

# Cleanup the converted PNGs
rm -v ${scaned_files}

# Cleanup the temp scanning dir
rm -rf "${scandir}" 
