#! /bin/sh
#  get-text-orientation -- of images to allow automatic reorientation
#  Copyright (C) 2015  SEIKO EPSON CORPORATION
#
#  License: GPL-3.0+
#  Author : EPSON AVASYS CORPORATION
#
#  This file is part of the 'Utsushi' package.
#  This package 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 ought to have received a copy of the GNU General Public License
#  along with this package.  If not, see <http:#www.gnu.org/licenses/>.

: ${engine:=tesseract}

progname=$(basename $0)

unset LANGUAGE
LC_ALL=C
export LC_ALL

if test $# -gt 0; then
    engine=$1
    shift
fi

if test $# -gt 0; then
    convert=$1
    shift
fi

if test $# -gt 0; then
    echo "$progname: ignoring extra arguments: '$@'" >&2
fi

msg=$(type "$engine" 2>&1)
if test $? != 0; then
    echo "$progname: $msg" >&2
    exit 1
fi

case "$engine" in
    */tesseract|tesseract)

        #  Tesseract 3.03 outputs the results we want to standard
        #  error; Tesseract 3.04 dumps it on standard output.  So
        #  do later versions.  We want it on the latter.
        #  Command-line options changed in 3.05.00.

        version=$($engine --version 2>&1 | sed -n 's/.*tesseract *//p')

        case "$version" in
            3.0[34].* )
                $engine - - -psm 0 -l osd 2>&1
                ;;

            3.05.* | [45].* )
                $engine - - --psm 0 -l osd 2>&1
                ;;
        esac
        ;;

    */ocr-engine-getrotate)

        #  The ocr-engine-getrotate utility expects an uncompressed
        #  BMP image.

        tmpfile=$(mktemp -q .reorient.XXX)
        trap "rm -f $tmpfile" 0 1 2 15

        $convert - -compress None bmp3:$tmpfile
        $engine $tmpfile
        ;;
    *)
        echo "$progname: unsupported engine: $engine" >&2
        ;;
esac
