ptionNumber GraphicDotDistance)
[ "$?" = 0 ] || exit 1

# This is the total area we will send to the embosser
TOTALGRAPHICWIDTH=$(( ( ($PRINTABLEWIDTH - 160) / $GRAPHICDOTDISTANCE ) / 2 * 2 ))
TOTALGRAPHICHEIGHT=$(( ( ($PRINTABLEHEIGHT - 160) / $GRAPHICDOTDISTANCE ) / 4 * 4 ))

echo "DEBUG: total graphical: ${TOTALGRAPHICWIDTH}x${TOTALGRAPHICHEIGHT}" >&2

# This is how many dots we have to introduce to respect at least the software left+top margin
GRAPHICHOFFSET=$(( ($PAGE_LEFT - $MARGIN_LEFT + $GRAPHICDOTDISTANCE - 1) / $GRAPHICDOTDISTANCE ))
GRAPHICVOFFSET=$(( ($PAGE_TOP - $MARGIN_TOP + $GRAPHICDOTDISTANCE - 1) / $GRAPHICDOTDISTANCE ))

echo "DEBUG: graphical offset: ${GRAPHICHOFFSET}x${GRAPHICVOFFSET}" >&2

# This is the resulting actual margin
GRAPHICLEFTMARGIN=$(( $MARGIN_LEFT + $GRAPHICHOFFSET * $GRAPHICDOTDISTANCE ))
GRAPHICTOPMARGIN=$(( $MARGIN_TOP + $GRAPHICVOFFSET * $GRAPHICDOTDISTANCE ))

echo "DEBUG: rounded graphical top-left corner margin: ${GRAPHICLEFTMARGIN}x${GRAPHICTOPMARGIN}" >&2

# Then compute how many dots we can afford until reaching the software right+bottom margin
GRAPHICWIDTH=$(( ( ( $PAGEWIDTH - $GRAPHICLEFTMARGIN - $PAGE_RIGHT ) - 160) / $GRAPHICDOTDISTANCE ))
GRAPHICHEIGHT=$(( ( ( $PAGEHEIGHT - $GRAPHICTOPMARGIN - $PAGE_BOTTOM ) - 160) / $GRAPHICDOTDISTANCE ))

echo "DEBUG: resulting graphical area: ${GRAPHICWIDTH}x${GRAPHICHEIGHT}" >&2

#
# Text translation
#

TABLESDIR=/usr/share/liblouis/tables
echo "DEBUG: Liblouis table directory is $TABLESDIR" >&2

getOptionLibLouis () {
  OPTION=$1
  VALUE=$(getOption $OPTION)

  # Check validity of input
  case "$VALUE" in
    [-_.0-9A-Za-z]*) ;;
    *) printf "ERROR: Option $OPTION must be a valid liblouis table name, got '%s'\n" "$VALUE" >&2
       exit 1
       ;;
  esac

  LOCALE=${LANG%@*}
  LOCALE=${LOCALE%.*}
  LANGUAGE=${LOCALE%_*}
  COUNTRY=${LOCALE#*_}
  LOUIS_LOCALE=$LANGUAGE-$COUNTRY

  getLibLouisTableScore () {
    GRADE="$1"
    printf "DEBUG: looking for locale '%s' and grade '%s' \n" "$LOCALE" "$GRADE" >&2
    # Try to select a good table from its metadata
    selected=
    selectedscore=0
    for table in "$TABLESDIR/"*.tbl "$TABLESDIR/"*.ctb "$TABLESDIR/"*.utb; do
      score=0
      name=${table#$TABLESDIR/}

      if grep -q "^#+locale:$LOUIS_LOCALE$" $table; then
	printf "DEBUG: %s has correct locale %s\n" "$name" "$LOUIS_LOCALE" >&2
	score=$((score + 15))
      elif grep -q "^#+locale:$LANGUAGE$" $table; then
	printf "DEBUG: %s has correct language %s\n" "$name" "$LANGUAGE" >&2
	score=$((score + 10))
      else
	# Requested language is a must
	continue
      fi

      if [ -n "$GRADE" ]; then
	if grep -q "^#+grade:$GRADE$" $table || \
	 ( [ "$GRADE" = 0 ] && ( grep -q "^#+contraction:no" $table || grep -q "^#+type:computer" $table ) ) \
	then
	  printf "DEBUG: %s has correct grade %s\n" "$name" "$GRADE" >&2
	  score=$((score + 10))
	else
	  # Requested grade is a must
	  continue
	fi
      fi

      # Dot numbers are not always specified in liblouis :/
      if grep -q "^#+dots:$TEXTDOTS$" $table || \
	 ( [ "$TEXTDOTS" = 6 ] && grep -q "^#+grade:[1-3]" $table ) \
      then
	printf "DEBUG: %s has correct dots %s\n" "$name" "$TEXTDOTS" >&2
	score=$((score + 2))
      fi

      if [ $score -gt $selectedscore ]; then
	printf "DEBUG: %s has better score $score\n" "$name" >&2
	selected=$name
	selectedscore=$score
      fi
    done

    echo $selected
  }

  # Check presence of table
  case "$VALUE" in
    None)
      printf None
      ;;
    Locale)
      selected=$(getLibLouisTableScore '')
      # Try tagged tables before untagged ones
      if [ -n "$selected" ]; then
	printf "%s" "$selected"
      elif [ -f "$TABLESDIR/$LOCALE.tbl" ]; then
	printf "%s" "$LOCALE.tbl"
      elif [ -f "$TABLESDIR/$LANGUAGE.tbl" ]; then
	printf "%s" "$LANGUAGE.tbl"
      else
        printf "ERROR: Could not find $OPTION table with locale %s\n" "$LOCALE" >&2
	printf None
	exit 1
      fi
      ;;
    Locale-g[0-3])
      GRADE=${VALUE#Locale-g}
      selected=$(getLibLouisTableScore $GRADE)
      if [ -n "$selected" ]; then
	printf "%s" "$selected"
	exit 0
      else
	for i in "$TABLESDIR/$LOCALE.tbl" "$TABLESDIR/$LOCALE"*.tbl "$TABLESDIR/$LANGUAGE.tbl" "$TABLESDIR/$LANGUAGE"*.tbl
	do
	  if grep -q "^#+grade:$GRADE$" "$i"
	  then
	    printf "%s" "${i//*\/}"
	    exit 0
	  fi
	done
      fi
      printf "ERROR: Could not find $OPTION table with locale %s and grade %s\n" "$LOCALE" "$GRADE" >&2
      printf None
      exit 1
      ;;
    HyphLocale)
      if [ -f "$TABLESDIR/hyph_$LOCALE.dic" ]; then
	printf "%s" "hyph_$LOCALE.dic"
      elif [ -f "$TABLESDIR/hyph_$LANGUAGE.dic" ]; then
	printf "%s" "hyph_$LANGUAGE.dic"
      else
        printf "WARN: Could not find $OPTION hyphenation table with locale %s\n" "$LOCALE" >&2
	printf None
      fi
      ;;
    *)
      [ -f "$TABLESDIR/$VALUE".utb ] && VALUE="$VALUE".utb
      [ -f "$TABLESDIR/$VALUE".ctb ] && VALUE="$VALUE".ctb
      if [ ! -f "$TABLESDIR/$VALUE" ]
      then
        printf "ERROR: Could not find $OPTION table '%s'\n" "$VALUE" >&2
        exit 1
      fi

      printf "%s" "$VALUE"
      ;;
  esac
}

LIBLOUIS1=$(getOptionLibLouis LibLouis)
[ "$?" = 0 ] || exit 1
LIBLOUIS2=$(getOptionLibLouis LibLouis2)
[ "$?" = 0 ] || exit 1
LIBLOUIS3=$(getOptionLibLouis LibLouis3)
[ "$?" = 0 ] || exit 1
LIBLOUIS4=$(getOptionLibLouis LibLouis4)
[ "$?" = 0 ] || exit 1

echo "DEBUG: Table1 $LIBLOUIS1" >&2
echo "DEBUG: Table2 $LIBLOUIS2" >&2
echo "DEBUG: Table3 $LIBLOUIS3" >&2
echo "DEBUG: Table4 $LIBLOUIS4" >&2

[ "$LIBLOUIS1" != None ] && LIBLOUIS_TABLES="$LIBLOUIS1"
[ "$LIBLOUIS2" != None ] && LIBLOUIS_TABLES="${LIBLOUIS_TABLES:+$LIBLOUIS_TABLES,}$LIBLOUIS2"
[ "$LIBLOUIS3" != None ] && LIBLOUIS_TABLES="${LIBLOUIS_TABLES:+$LIBLOUIS_TABLES,}$LIBLOUIS3"
[ "$LIBLOUIS4" != None ] && LIBLOUIS_TABLES="${LIBLOUIS_TABLES:+$LIBLOUIS_TABLES,}$LIBLOUIS4"

#
# Checking for presence of tools
#
checkTool() {
  TOOL=$1
  PACKAGE=$2
  USE=$3
  if ! type $TOOL > /dev/null
  then
    printf "ERROR: The $PACKAGE package is required for $USE\n" >&2
    exit 1
  fi
}
