#!/bin/bash

# Company:  PowerCraft Technology
# Author:   Copyright Jelle de Jong <jelledejong@powercraft.nl>
# Note:     Please send me an email if you enhanced the script
# Date:     2009-01-09 / 2009-01-19 / ... / 2009-02-24 / 2009-03-31

# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

# set -x
# exec 2>$HOME/pct-scanner-script-process.log
# STRACE=$(which strace)

TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=pct-scanner-script-process

file="$1"
version="0.0.4"

function process_busy_set()
{
    if [ -d "$OUTDIR"/scanscript/ ]
    then
        mkdir --parents "$OUTDIR"/scanscript/
    fi
    processbusy=$(mktemp -p "$OUTDIR"/scanscript/ .processbusy.txt)
    trap "rm -f $processbusy" 0 1 2 3 15
}

function process_counter()
{
    logfile="$1"
    input=$(head -n 1 "$logfile")
    input=${input##*: }
    if [ -n "$input" ]
    then
        echo $"processing: page: $input"
        count=${input}
        let count+=1
        find='counter:.*'
        replace="counter: $count"
        sed -i -e "s,$find,$replace,g" "$logfile"
        echo $"processing: next page: $count"
    fi
}

function process_grayscale()
{
    sourcefile="$1"
    logfile="$LOG"
    date=$(date)

    dirname=${sourcefile%/*}
    filename=${sourcefile##*/}; filename=${filename%.*}
    extension=${sourcefile##*.}

    if [ ! -z "$SCAN_RES" ] && [ ! -z "$SCAN_WIDTH" ] && [ ! -z "$SCAN_HEIGHT" ] && [ ! -z "$SCAN_DEPTH" ] && [ ! -z "$SCAN_FORMAT" ]&& [ ! -z "$SCAN_FORMAT_ID" ]; then
        echo $"processing: all needed variables are found, continuing now"
    else
        echo 1>&2 $"processing: system variables not found, exiting now"
        exit 1
    fi

    echo "-- -- -- -- -- -- -- --" >> "$logfile"
    echo "date: $date" >> "$logfile"
    echo "scanadf: res:$SCAN_RES width:$SCAN_WIDTH height:$SCAN_HEIGHT depth:$SCAN_DEPTH format:$SCAN_FORMAT id:$SCAN_FORMAT_ID" >> "$logfile"

    echo "processing: convert $sourcefile $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg" >> "$logfile"
    $STRACE convert "$sourcefile" $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg

    if [ -z $NOPDF ]; then
        echo "processing: convert $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf" >> "$logfile"
        $STRACE convert $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf

        if [ -e $OUTDIR/scanscript/output/document-multi-page-grayscale.pdf ] && [ -e $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf ]
        then
            mv $OUTDIR/scanscript/output/document-multi-page-grayscale.pdf $OUTDIR/scanscript/output/document-multi-page-old.pdf
            pdftk $OUTDIR/scanscript/output/document-multi-page-old.pdf $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf cat output $OUTDIR/scanscript/output/document-multi-page-grayscale.pdf
            rm $OUTDIR/scanscript/output/document-multi-page-old.pdf
        else
            pdftk $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf cat output $OUTDIR/scanscript/output/document-multi-page-grayscale.pdf
        fi
    fi

    if [ -z $NODJVU ]; then
        echo "processing: c44 -crcbnone -dpi $SCAN_RES $sourcefile $OUTDIR/scanscript/processing/document-single-page-$filename.djvu" >> "$logfile"
        echo $"processing: executing c44 compression command, please standby ..."
        c44 -crcbnone -dpi $SCAN_RES "$sourcefile" $OUTDIR/scanscript/processing/document-single-page-$filename.djvu $EXEC_VERBOSE

        # cpaldjvu -verbose -bgwhite -colors 8 -dpi 600 image-0007.pgm $OUTDIR/scanscript/processing/document-single-page-image-0007.djvu
        # c44 -crcbnone -dpi 600 image-0008.pgm $OUTDIR/scanscript/processing/document-single-page-image-0008.djvu

        if [ -e $OUTDIR/scanscript/output/document-multi-page-grayscale.djvu ] && [ -e $OUTDIR/scanscript/processing/document-single-page-$filename.djvu ]
        then
            djvm -insert $OUTDIR/scanscript/output/document-multi-page-grayscale.djvu $OUTDIR/scanscript/processing/document-single-page-$filename.djvu
        else
            djvm -create $OUTDIR/scanscript/output/document-multi-page-grayscale.djvu $OUTDIR/scanscript/processing/document-single-page-$filename.djvu
        fi
    fi

    if [ ! -z "$RMINPUT" ]; then
        echo "processing: removing input file: $sourcefile" >> "$logfile"
        rm $VERBOSE "$sourcefile"
    fi

    echo "-- -- -- -- -- -- -- --" >> "$logfile"

    process_counter "$logfile"

    scanbusy="$OUTDIR"/scanscript/.scanbusy.txt
    if [ ! -z "$OPEN" ] && [ -z "$NODJVU" ] && [ ! -e "$scanbusy" ]; then
        nohup "$OPEN" "$OUTDIR"/scanscript/output/document-multi-page-grayscale.djvu > "$OUTDIR"/scanscript/.open.log 2>/dev/null &
    elif [ ! -z "$OPEN" ] && [ -z "$NOPDF" ] && [ ! -e "$scanbusy" ]; then
        nohup "$OPEN" "$OUTDIR"/scanscript/output/document-multi-page-grayscale.pdf > "$OUTDIR"/scanscript/.open.log 2>/dev/null &
    fi

    return
}

function process_color()
{
    sourcefile="$1"
    logfile="$LOG"
    date=$(date)

    dirname=${sourcefile%/*}
    filename=${sourcefile##*/}; filename=${filename%.*}
    extension=${sourcefile##*.}

    if [ ! -z "$SCAN_RES" ] && [ ! -z "$SCAN_WIDTH" ] && [ ! -z "$SCAN_HEIGHT" ] && [ ! -z "$SCAN_DEPTH" ] && [ ! -z "$SCAN_FORMAT" ]&& [ ! -z "$SCAN_FORMAT_ID" ]; then
        echo $"processing: all needed variables are found, continuing now"
    else
        echo 1>&2 $"processing: system variables not found, exiting now"
        exit 1
    fi

    echo "-- -- -- -- -- -- -- --" >> "$logfile"
    echo "date: $date" >> "$logfile"
    echo "scanadf: res:$SCAN_RES width:$SCAN_WIDTH height:$SCAN_HEIGHT depth:$SCAN_DEPTH format:$SCAN_FORMAT id:$SCAN_FORMAT_ID" >> "$logfile"

    echo "processing: convert $sourcefile $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg" >> "$logfile"
    $STRACE convert "$sourcefile" "$OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg"

    if [ -z $NOPDF ]; then
        echo "processing: convert $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf" >> "$logfile"
        $STRACE convert "$OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg" "$OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf"

        if [ -e $OUTDIR/scanscript/output/document-multi-page-color.pdf ] && [ -e $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf ]
        then
            mv $OUTDIR/scanscript/output/document-multi-page-color.pdf $OUTDIR/scanscript/output/document-multi-page-old.pdf
            pdftk $OUTDIR/scanscript/output/document-multi-page-old.pdf $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf cat output $OUTDIR/scanscript/output/document-multi-page-color.pdf
            rm $OUTDIR/scanscript/output/document-multi-page-old.pdf
        else
            pdftk $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf cat output $OUTDIR/scanscript/output/document-multi-page-color.pdf
        fi
    fi

    if [ -z $NODJVU ]; then
        echo "processing: c44 -dpi $SCAN_RES $sourcefile $OUTDIR/scanscript/processing/document-single-page-$filename.djvu" >> "$logfile"
        echo $"processing: executing c44 compression command, please standby ..."
        c44 -dpi $SCAN_RES "$sourcefile" $OUTDIR/scanscript/processing/document-single-page-$filename.djvu $EXEC_VERBOSE

        # cpaldjvu -verbose -bgwhite -colors 8 -dpi 600 image-0007.pgm $OUTDIR/scanscript/processing/document-single-page-image-0007.djvu
        # c44 -crcbnone -dpi 600 image-0008.pgm $OUTDIR/scanscript/processing/document-single-page-image-0008.djvu
        # cpaldjvu -verbose -dpi $SCAN_RES -bgwhite "$sourcefile" ~/scanjet/processing/document-single-page-$filename.djvu
        # c44 -dpi 600 /home/jelle/scanjet/input/image-0001.ppm /home/jelle/scanjet/processing/document-single-page-image-0001.djvu

        if [ -e $OUTDIR/scanscript/output/document-multi-page-color.djvu ] && [ -e $OUTDIR/scanscript/processing/document-single-page-$filename.djvu ]
        then
            djvm -insert $OUTDIR/scanscript/output/document-multi-page-color.djvu $OUTDIR/scanscript/processing/document-single-page-$filename.djvu
        else
            djvm -create $OUTDIR/scanscript/output/document-multi-page-color.djvu $OUTDIR/scanscript/processing/document-single-page-$filename.djvu
        fi
    fi

    if [ ! -z "$RMINPUT" ]; then
        echo "processing: removing input file: $sourcefile" >> "$logfile"
        rm $VERBOSE "$sourcefile"
    fi

    echo "-- -- -- -- -- -- -- --" >> "$logfile"

    process_counter "$logfile"

    scanbusy="$OUTDIR"/scanscript/.scanbusy.txt
    if [ ! -z "$OPEN" ] && [ -z "$NODJVU" ] && [ ! -e "$scanbusy" ]; then
        nohup "$OPEN" $OUTDIR/scanscript/output/document-multi-page-color.djvu > "$OUTDIR"/scanscript/.open.log 2>/dev/null &
    elif [ ! -z "$OPEN" ] && [ -z "$NOPDF" ] && [ ! -e "$scanbusy" ]; then
        nohup "$OPEN" $OUTDIR/scanscript/output/document-multi-page-color.pdf > "$OUTDIR"/scanscript/.open.log 2>/dev/null &
    fi

    return
}

function process_lineart()
{
    sourcefile="$1"
    logfile="$LOG"
    date=$(date)

    dirname=${sourcefile%/*}
    filename=${sourcefile##*/}; filename=${filename%.*}
    extension=${sourcefile##*.}

    if [ ! -z "$SCAN_RES" ] && [ ! -z "$SCAN_WIDTH" ] && [ ! -z "$SCAN_HEIGHT" ] && [ ! -z "$SCAN_DEPTH" ] && [ ! -z "$SCAN_FORMAT" ]&& [ ! -z "$SCAN_FORMAT_ID" ]; then
        echo $"processing: all needed variables are found, continuing now"
    else
        echo 1>&2 $"processing: system variables not found, exiting now"
        exit 1
    fi

    echo "-- -- -- -- -- -- -- --" >> "$logfile"
    echo "date: $date" >> "$logfile"
    echo "scanadf: res:$SCAN_RES width:$SCAN_WIDTH height:$SCAN_HEIGHT depth:$SCAN_DEPTH format:$SCAN_FORMAT id:$SCAN_FORMAT_ID" >> "$logfile"

    echo "processing: convert $sourcefile $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg" >> "$logfile"
    $STRACE convert "$sourcefile" $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg

    if [ -z $NODJVU ]; then
        echo "processing: cjb2 -verbose -clean -dpi $SCAN_RES $sourcefile $OUTDIR/scanscript/processing/document-single-page-$filename.djvu" >> "$logfile"
        cjb2 -verbose -clean -dpi $SCAN_RES "$sourcefile" $OUTDIR/scanscript/processing/document-single-page-$filename.djvu $EXEC_VERBOSE

        if [ -e $OUTDIR/scanscript/output/document-multi-page-lineart.djvu ] && [ -e $OUTDIR/scanscript/processing/document-single-page-$filename.djvu ]
        then
            djvm -insert $OUTDIR/scanscript/output/document-multi-page-lineart.djvu $OUTDIR/scanscript/processing/document-single-page-$filename.djvu
        else
            djvm -create $OUTDIR/scanscript/output/document-multi-page-lineart.djvu $OUTDIR/scanscript/processing/document-single-page-$filename.djvu
        fi
    fi

    if [ -z $NOPDF ]; then
        echo "processing: convert $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf" >> "$logfile"
        $STRACE convert $OUTDIR/scanscript/processing/jpg/image-single-$filename.jpg $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf

        if [ -e $OUTDIR/scanscript/output/document-multi-page-lineart.pdf ] && [ -e $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf ]
        then
            mv $OUTDIR/scanscript/output/document-multi-page-lineart.pdf $OUTDIR/scanscript/output/document-multi-page-old.pdf
            pdftk $OUTDIR/scanscript/output/document-multi-page-old.pdf $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf cat output $OUTDIR/scanscript/output/document-multi-page-lineart.pdf
            rm $OUTDIR/scanscript/output/document-multi-page-old.pdf
        else
            pdftk $OUTDIR/scanscript/processing/pdf/document-single-page-$filename.pdf cat output $OUTDIR/scanscript/output/document-multi-page-lineart.pdf
        fi
    fi

    if [ ! -z "$RMINPUT" ]; then
        echo "processing: removing input file: $sourcefile" >> "$logfile"
        rm $VERBOSE "$sourcefile"
    fi

    echo "-- -- -- -- -- -- -- --" >> "$logfile"

    process_counter "$logfile"

    scanbusy="$OUTDIR"/scanscript/.scanbusy.txt
    if [ ! -z "$OPEN" ] && [ -z "$NODJVU" ] && [ ! -e "$scanbusy" ]; then
        nohup "$OPEN" $OUTDIR/scanscript/output/document-multi-page-lineart.djvu > "$OUTDIR"/scanscript/.open.log 2>/dev/null &
    elif [ ! -z "$OPEN" ] && [ -z "$NOPDF" ] && [ ! -e "$scanbusy" ]; then
        nohup "$OPEN" $OUTDIR/scanscript/output/document-multi-page-lineart.pdf > "$OUTDIR"/scanscript/.open.log 2>/dev/null &
    fi

    return
}

if [ ! -z "$LOG" ] && [ ! -z "$OUTDIR" ]; then
    process_busy_set;
    if [ ! -z $LINEART ]; then
        process_lineart "$file";
    elif [ ! -z $COLOR ]; then
        process_color "$file";
    elif [ ! -z $GRAYSCALE ]; then
        process_grayscale "$file";
    fi
else
    echo 1>&2 $"processing: system variables not found, exiting now"
    exit 1
fi

exit
