#!/usr/bin/with-contenv bashio

# Allow OCR setting
OCRLANG="$(bashio::config "ocrlang")"

if [ -n "$OCRLANG" ]; then
  LINE=$(sed -n '/OCR_LANGUAGES/=' /data/config/papermerge.conf.py)  
  echo "OCRLANG variable is set, processing the language packages"
  for i in $(echo "$OCRLANG" | tr "," " "); do
    if apt-cache show tesseract-ocr-"${i}" > /dev/null 2>&1; then
      echo "installing tesseract-ocr-${i}"
      apt-get install -y tesseract-ocr-"${i}"
    else
      echo "package tesseract-ocr-${i} not found in the repository, skipping"
    fi
    sed "$LINE a \"${i}\": \"lang\"," products.txt
    LINE=$((LINE+1)) 
  done
fi
