~ubuntu-branches/ubuntu/oneiric/pct-scanner-scripts/oneiric

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/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-22 / ... / 2009-02-26 / 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.log

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

version="0.0.4"
author="Jelle de Jong <jelledejong@powercraft.nl>"
donation="http://www.tuxcrafter.net/pages/contact.html#paypal-donation"
homepage="https://secure.powercraft.nl/svn/packages/trunk/source/pct-scanner-scripts/"
licence="http://www.gnu.org/licenses/gpl-3.0.html"

SYSCONF=/etc/pct-scanner-script.conf
SYSDIR=/etc/pct-scanner-script.conf.d/
PROCESS=/usr/lib/pct-scanner-scripts/pct-scanner-script-process

unset LINEART
unset COLOR
unset GRAYSCALE
unset SEARCH
unset RESUME
unset OUTDIR
unset DEVICE
unset SOURCE
unset RESOLUTION
unset CONFIG
unset OPEN

unset NOPDF
unset NODJVU

unset CLEAN
unset VERBOSE
unset EXEC_VERBOSE

function version()
{
    echo $"Usage:     $0 --help"
    echo $"Version:   $version"
    echo $"Author:    $author"
    echo $"Donation:  $donation"
    echo $"Homepage:  $homepage"
    echo $"Licence:   $licence"
}

function usage()
{
    echo "easy scan and create merged DjVu and PDF documents for archiving"
    echo
    echo "Usage:    $0 [options]"
    echo
    echo "Options:"
    echo "  -h, --help, -?       show this help information"
    echo "  -l, --lineart        scan in lineart mode [recommended]"
    echo "  -c, --color          scan in color mode, when supported"
    echo "  -g, --grayscale      scan in grayscale mode, when supported"
    echo "  -o, --outdir ...     set the output location [check diskspace]"
    echo "  -s, --source ...     set the scan source, see --search"
    echo "  -r, --resolution ... set the scan resolution, see --search"
    echo "  -d, --device ...     set the device resolution, see --search"
    echo "  -m, --resume ...     set the resume counter for the page number"
    echo "  -e, --clean          cleanup all files in outdir/scanscript/"
    echo "  -p, --nopdf          don't create PDF documents [recommend]"
    echo "  -j, --nodjvu         don't create DjVu documents"
    echo "  -a, --search         search for scan devices and there capabilities"
    echo "  -n, --config ...     set a configuration to use, when availible"
    echo "  -v, --version        show the version and author information"
    echo "  -b, --verbose        make commands verbose, providing more info"
    echo
    echo "Examples:"
    echo "  pct-scanner-script --search"
    echo "  pct-scanner-script --lineart --outdir $HOME --source ADF --resolution 600 --device net:192.168.1.1:hpaio:/usb/Officejet_J5700_series?serial=CN7BOCF3HP04TC"
    echo "  pct-scanner-script --resume 11 --lineart --outdir $HOME --source ADF --resolution 600 --device net:192.168.1.1:hpaio:/usb/Officejet_J5700_series?serial=CN7BOCF3HP04TC"
    echo
    echo "  pct-scanner-script --lineart"
    echo "  pct-scanner-script --config config0"
    echo "  pct-scanner-script --clean --config config0 --nopdf --verbose"
    echo
    echo "Configuration:"
    echo "  $SYSCONF"
    echo "  $SYSDIR"
    echo
    echo "  In the configuration files most settings can be set, so not all"
    echo "  command arguments are needed to start the program. This makes it"
    echo "  very easy to setup your favorite setups and use it with only the"
    echo "  --config myconfig0 argument. Please take a look inside the config"
    echo "  files to get a better idea how to use them. The --config conf0"
    echo "  argument can be used to select cascading configurations. As an"
    echo "  example --lineart --config mystyle0 will use the section"
    echo "  [lineart-mystyle0] in the configuration files. There is also an"
    echo "  OPEN=COMMAND option in the configuration file to open the combined"
    echo "  output file with a program of own choice, when the scan is done."
    echo
    echo "  The config system uses a priority based cascading system, the"
    echo "  command arguments options will overwrite the settings found in"
    echo "  the configuration files, and config files with an higher number"
    echo "  will overwrite previous found configuration files, but not the"
    echo "  command line arguments, when they are used."
    echo
    echo "Description:"
    echo "  Easy scan and create merged DjVu and PDF documents for archiving"
    echo "  This package contains a script and configuration system to easily"
    echo "  scan documents and create a digital archiving. It can create DjVu"
    echo "  PDF and JPEG outputs, in merged and individual files. It also has"
    echo "  a configuration file system where different configurations can be"
    echo "  defined, making it easy to switch between configurations. The "
    echo "  script is designed for productivity."
    echo
    echo "Features:"
    echo "  - auto archiving in merged and individual DjVu, PDF, and JPEG files"
    echo "  - auto and manual resume when scanning somehow fails"
    echo "  - cascading configuration system, for easy customizing"
    echo "  - easy command-line-style bash program, with useful options"
    echo "  - archiving made possible with both adf and flatbed scanners"
    echo "  - built for productivity and daily use"
    echo "  - uses bash, scanadf and other mainstream processing tools"
    echo "  - multilanguage commandline output."
    echo
    echo "  This program is a helper script to easy scan and automated digital"
    echo "  document archiving for normal and adf scanners. You can debug this"
    echo "  script by running it as bash -x pct-scanner-script [options]. The"
    echo "  script is just a nice wrapper for the scanadf, DjVu and PDF tools."
    echo
    echo "Website:"
    echo "  https://secure.powercraft.nl/svn/packages/"
    echo "Mailinglist:"
    echo "  http://www.powercraft.nl/cgi-bin/mailman/listinfo/packages-commit"
    echo "  http://www.powercraft.nl/cgi-bin/mailman/listinfo/packages-devel"
}

while [[ "$1" == -* ]]; do
    case "$1" in
        -h|--help|-\?) usage; exit 0;;
        -l|--lineart) LINEART=1; shift;;
        -c|--color) COLOR=1; shift;;
        -g|--grayscale|--gray) GRAYSCALE=1; shift;;
        -o|--outdir) OUTDIR="$2"; shift; shift;;
        -s|--source) SOURCE="$2"; shift; shift;;
        -r|--resolution) RESOLUTION="$2"; shift; shift;;
        -d|--device) DEVICE="$2"; shift; shift;;
        -m|--resume) RESUME="$2"; shift; shift;;
        -e|--clean) CLEAN=1; shift;;
        -p|--nopdf) NOPDF=1; shift;;
        -j|--nodjvu) NODJVU=1; shift;;
        -a|--search) SEARCH=1; shift;;
        -n|--config) CONFIG="$2"; shift; shift;;
        -b|--verbose) VERBOSE="--verbose"; shift;;
        -v|--version) version; exit 0;;
        --) shift; break;;
        -*) echo 1>&2 $"invalid option: $1"; usage; exit 1;;
    esac
done

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

function scan_busy_unset()
{
    scanbusy="$OUTDIR"/scanscript/.scanbusy.txt
    rm -f "$scanbusy"
}

function process_busy_scan()
{
    seconds=0
    processbusy="$OUTDIR"/scanscript/.processbusy.txt
    while [ -e "$processbusy" ]
    do
        /bin/sleep 3
        seconds=$(($seconds+3))
    done
}

function scan_configuration()
{
    file="$1"
    unset options
    [ ! -z "$RESUME" ] && options+=("RESUME")
    [ ! -z "$OUTDIR" ] && options+=("OUTDIR")
    [ ! -z "$DEVICE" ] && options+=("DEVICE")
    [ ! -z "$SOURCE" ] && options+=("SOURCE")
    [ ! -z "$RESOLUTION" ] && options+=("RESOLUTION")
    for value in "${options[@]}"
    do
        sed -n -i -e "/$value/!p" "$file"
    done
}

function get_configuration()
{
    section="$1"
    if [ ! -z "$section" ] && [ ! -z "$CONFIG" ]; then
        section+="-$CONFIG"
    elif [ -z "$section" ] && [ ! -z "$CONFIG" ]; then
        section="$CONFIG"
    fi
    [ ! -d "$SYSDIR" ] && mkdir --parents $VERBOSE "$SYSDIR"
    tempfile=$(mktemp -p "/tmp/" tempfile.XXXXXXXXXX)
    trap "rm -f $tempfile" 0 1 2 3 15
    [ -z "$tempfile" ] && exit 1
    files="$SYSCONF"
    files+=$'\n'
    files+=$(run-parts --list "$SYSDIR" )
    if [ -n "$files" ]; then
        while read file; do
            set +e
            [ ! -r "$file" ] && continue
            sed -n '/^\['"$section"'\]$/,/^\[.*\]$/{//!p}' "$file" > "$tempfile"
            scan_configuration "$tempfile"
            . "$tempfile"
            set -e
        done < <(echo "$files")
    fi
}

function check_dependencies()
{
    if [ -z $(which scanadf) ]
    then
        echo 1>&2 $"dependencies not fulfilled, please install all necessary application"
        echo 1>&2 $"hint: you may try to install the sane package that contains the scanadf program"
        echo 1>&2 $"hint: on debian based system the sane package can be isntalled with sudo apt-get install sane"
        exit 1
    fi
}

function setup_environment()
{
    if [ -z "$OUTDIR" ]; then
        echo 1>&2 $"outdir not set, try adding --outdir $HOME, closing now"
        exit 1
    elif [ -z "$DEVICE" ]; then
        echo 1>&2 $"device not set, try adding --device net:192.168.1.1:hpaio:/usb/Officejet_J5700_series?serial=CN7BOCF3HP04TC, closing now"
        exit 1
    elif [ -z "$SOURCE" ]; then
        echo 1>&2 $"source not set, try adding --source adf, closing now"
        exit 1
    elif [ -z "$RESOLUTION" ]; then
        echo 1>&2 $"resolution not set, try adding --resolution 600, closing now"
        exit 1
    fi

    [ ! -d $OUTDIR/scanscript/input/ ] && mkdir $VERBOSE --parents $OUTDIR/scanscript/input/
    [ ! -d $OUTDIR/scanscript/output/ ] && mkdir $VERBOSE --parents $OUTDIR/scanscript/output/
    [ ! -d $OUTDIR/scanscript/processing/ ] && mkdir $VERBOSE --parents $OUTDIR/scanscript/processing/
    [ ! -d $OUTDIR/scanscript/processing/jpg/ ] && mkdir $VERBOSE --parents $OUTDIR/scanscript/processing/jpg/
    [ ! -d $OUTDIR/scanscript/processing/pdf/ ] && [ -z $NOPDF ] && mkdir $VERBOSE --parents $OUTDIR/scanscript/processing/pdf/

    [ -z "$LOG" ] && LOG="$OUTDIR/scanscript/output/status.log"
    [ -z "$COMMAND" ] && COMMAND="$PROCESS"

    export OUTDIR="$OUTDIR"
    export LOG="$LOG"

    [ ! -z "$OPEN" ] && export OPEN="$OPEN"
    [ ! -z "$RMINPUT" ] && export RMINPUT="$RMINPUT"
    [ ! -z "$VERBOSE" ] && export VERBOSE="$VERBOSE"
    [ ! -z "$EXEC_VERBOSE" ] && export EXEC_VERBOSE="$EXEC_VERBOSE"

    [ ! -z "$COLOR" ] && export COLOR="$COLOR"
    [ ! -z "$GRAYSCALE" ] && export GRAYSCALE="$GRAYSCALE"
    [ ! -z "$LINEART" ] && export LINEART="$LINEART"

    [ ! -z "$NOPDF" ] && export NOPDF="$NOPDF"
    [ ! -z "$NODJVU" ] && export NODJVU="$NODJVU"

    return 0
}

function directory_clean()
{
    if [ ! -z $CLEAN ]; then
        if [ -z "$OUTDIR" ]; then
            echo 1>&2 $"outdir not set, try adding --outdir $HOME, closing now"
            exit 1
        elif [ -e "$OUTDIR"/scanscript/ ]; then
            find $OUTDIR/scanscript/ -mindepth 1 -type f -exec rm $VERBOSE '{}' \;
        fi
    fi
}

function check_startcount()
{
    if [ ! -z "$RESUME" ]; then
        startcount=${RESUME}
        echo "counter: $startcount" > "$LOG"
    elif [ -e "$LOG" ]; then
        input=$(head -n 1 "$LOG")
        input=${input##*: }
        startcount=${input}
        [ -z "$startcount" ] && startcount=1 && echo "counter: $startcount" > "$LOG"
    else
        startcount=1
        echo "counter: $startcount" > "$LOG"
    fi

    if [ -n "$startcount" ]; then
        echo $"startcount: $startcount"
    else
        echo 1>&2 $"startcount is not detected, closing now"
        exit 1
    fi

    if [ "$(echo "$SOURCE" | tr '[:upper:]' '[:lower:]')" == "flatbed" ]; then
        EXTRA="--end-count $startcount"
    fi

    if [ -z $VERBOSE ]; then
        EXEC_VERBOSE="2>/dev/null"
    else
        unset EXEC_VERBOSE
    fi
}

function scanner_search()
{
    echo $"searching for scanadf scanners and there options, please standby ..."
    scanadf --verbose --help
}

function scanner_lineart()
{
    get_configuration lineart
    directory_clean
    setup_environment
    check_startcount
    echo $"lineart: yes"
    echo $"device: $DEVICE"
    echo $"outdir: $OUTDIR/scanscript/"
    echo $"log: $LOG"
    echo $"source: $SOURCE"
    echo $"resolution: $RESOLUTION"
    echo $"scanning of document has started, please standby ..."
    scan_busy_set
    scanadf $VERBOSE --device-name "$DEVICE" --scan-script "$COMMAND" --output-file "$OUTDIR"/scanscript/input/image-%04d.pbm --source "$SOURCE" --resolution "$RESOLUTION" --mode Lineart --compression None -x 210.000mm -y 297.000mm --start-count "$startcount" $EXTRA $EXEC_VERBOSE
    scan_busy_unset
}

function scanner_color()
{
    get_configuration color
    directory_clean
    setup_environment
    check_startcount
    echo $"color: yes"
    echo $"device: $DEVICE"
    echo $"outdir: $OUTDIR/scanscript/"
    echo $"log: $LOG"
    echo $"source: $SOURCE"
    echo $"resolution: $RESOLUTION"
    echo $"scanning of document has started, please standby ..."
    scan_busy_set
    scanadf $VERBOSE --device-name "$DEVICE" --scan-script "$COMMAND" --output-file "$OUTDIR"/scanscript/input/image-%04d.pbm --source "$SOURCE" --resolution "$RESOLUTION" --mode Color -x 210.000mm -y 297.000mm --start-count "$startcount" $EXTRA $EXEC_VERBOSE
    scan_busy_unset
}

function scanner_grayscale()
{
    get_configuration grayscale
    directory_clean
    setup_environment
    check_startcount
    echo $"grayscale: yes"
    echo $"device: $DEVICE"
    echo $"outdir: $OUTDIR/scanscript/"
    echo $"log: $LOG"
    echo $"source: $SOURCE"
    echo $"resolution: $RESOLUTION"
    echo $"scanning of document has started, please standby ..."
    scan_busy_set
    scanadf $VERBOSE --device-name "$DEVICE" --scan-script "$COMMAND" --output-file "$OUTDIR"/scanscript/input/image-%04d.pbm --source "$SOURCE" --resolution "$RESOLUTION" --mode Gray -x 210.000mm -y 297.000mm --start-count "$startcount" $EXTRA $EXEC_VERBOSE
    scan_busy_unset
}

check_dependencies

if [ ! -z $CONFIG ]; then
    get_configuration;
fi

if [ ! -z $LINEART ]; then
    scanner_lineart;
elif [ ! -z $COLOR ]; then
    scanner_color;
elif [ ! -z $GRAYSCALE ]; then
    scanner_grayscale;
elif [ ! -z $CLEAN ]; then
    directory_clean;
elif [ ! -z $SEARCH ]; then
    scanner_search;
elif [ -z $CLEAN ]; then
    echo 1>&2 $"pct-scanner-script: configuration variables not found, exiting now"
    exit 1
fi

process_busy_scan;

echo $"processing: completed, closing now"

exit