#!/bin/bash

package=tesseract
version=-5.0.0

progname=$0

source ../cascade-make/lib/cascade-lib.bash GEXT_TESSERACT ../.. $*

prefix=$GEXT_TESSERACT_INSTALLED

# See imagemagick ext
if [ "x$CROSSCONFIGURE_ARGS" != "x" ] ; then
    echo "WARNING: Crossconfiguring not supported yet"
fi

export CFLAGS="$CFLAGS -I$GEXT_TESSERACT_INSTALLED/include"
export CPPFLAGS="$CPPFLAGS -I$GEXT_TESSERACT_INSTALLED/include"
export CXXFLAGS="$CXXFLAGS -I$GEXT_TESSERACT_INSTALLED/include"
export LDFLAGS="$LDFLAGS -L$GEXT_TESSERACT_INSTALLED/lib"
export LD_LIBRARY_PATH="$GEXT_TESSERACT_INSTALLED/lib"
# Need PKG_CONFIG_PATH set tp leptonica's lib/pkgconfig folder (containing lept.pc file)
export PKG_CONFIG_PATH=$GEXT_TESSERACT_INSTALLED/lib/pkgconfig

opt_run_untar $force_untar $auto_untar $package $version

# Need to do this for TESSERACT, before we can do configure->make->make install
pushd $package$version;
libtoolize
#aclocal
#autoheader
sh autogen.sh
popd

opt_run_configure $force_config $auto_config $package $version $prefix \
  --disable-shared --enable-static

opt_run_make $compile $package $version
opt_run_make $install $package $version "install"
opt_run_make $clean $package $version "clean"
opt_run_make $distclean $package $version "distclean"

opt_run_tarclean $tarclean $package $version


echo "Installing basic tesseract languages support (tessdata)"
# Untar OCR language support tarball one level above TESSDATA_PREFIX ($GEXT_TESSERACT_INSTALLED/shared),
# Then go into that folder to finish setting up language files.
cp $GEXT_TESSERACT_DEVEL/packages/tessdata-langs.tar.gz $TESSDATA_PREFIX/../.
pushd $TESSDATA_PREFIX/..
tar -xvzf tessdata-langs.tar.gz
# Above creates linux/shared/tessdata-langs folder - move files there into
# linux/shared/tessdata (i.e. TESSDATA_PREFIX) and delete both tarball and temporary
# tessdata-langs folder created at current location of one level up from TESSDATA_PREFIX
mv tessdata-langs/*.traineddata $TESSDATA_PREFIX/.
rm tessdata-langs.tar.gz
rm -rf tessdata-langs
popd


echo "Done installing basic tesseract languages for OCR (Optical Character Recognition, to recognise text from images)."
echo "Visit https://github.com/tesseract-ocr/tessdata for a full list of trained language data for OCR."
echo "To download OCR support for any specific language(s), note the 3 letter code of that language"
echo "Go into your $TESSDATA_PREFIX folder and for each language you want OCR abilities for, run: "
echo "   wget https://github.com/tesseract-ocr/tessdata/raw/master/<3-letter-lang-code>.traineddata"
echo "To get all languages currently supported by Tesseract (beware, this may be a few Gigabytes), delete"
echo "$TESSDATA_PREFIX"
echo "and in $GEXTTES_INSTALLED/shared run:"
echo "   git clone https://github.com/tesseract-ocr/tessdata"
echo ""
