#!/bin/bash

# 
# Copyright (c) 2015, 2017-2018, 2020 Samuel Thibault <samuel.thibault@ens-lyon.org>
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# 

TABLESDIR=@TABLESDIR@

echo 'Group "Braille/Braille transcription"'
echo 'Option "LibLouis/Braille transcription" PickOne AnySetup 10'
echo '  Choice "None/None" ""'
echo '  *Choice "Locale/Default for language" ""'
echo '  Choice "Locale-g0/Default for language grade 0" ""'
echo '  Choice "Locale-g1/Default for language grade 1" ""'
echo '  Choice "Locale-g2/Default for language grade 2" ""'
echo '  Choice "Locale-g3/Default for language grade 3" ""'
echo '  Choice "HyphLocale/Default hyphenation rules for language" ""'

(
  for i in $TABLESDIR/*.utb $TABLESDIR/*.ctb
  do
    file=${i##*/}
    ext=${file##*.}
    name=${file%.$ext}
    TYPE=""
    LANGUAGE="$name"
    LOCATION=""
    GRADE=""
    DISPLAY_NAME=""
    display_name=$(grep ^#-display-name: "$i" | cut -d ' ' -f 2-)
    if [ -n "$display_name" ]; then
      # Table provides a display name, neat
      DISPLAY_NAME=$display_name
      LANGUAGE=$DISPLAY_NAME
    else
      case $name in 
	"afr-za-g1")	LANGUAGE=Afrikaans TYPE="grade 1" ;;
	"ar-ar-g1")	LANGUAGE=Arabic TYPE="grade 1" ;;
	"ar-fa")	LANGUAGE=Persian TYPE="grade 1" ;;
	"as-in-g1")	LANGUAGE=Assamese TYPE="grade 1" ;;
	"aw-in-g1")	LANGUAGE=Awadhi TYPE="grade 1" ;;
	"be-in-g1")	LANGUAGE=Bengali TYPE="grade 1" ;;
	"bg")		LANGUAGE=Bulgarian TYPE="grade 1" ;;
	"bh")		LANGUAGE=Bihari TYPE="grade 1" ;;
	"bo")		LANGUAGE=Tibetan TYPE="grade 1" ;;
	"boxes")	TYPE="Box drawing" ;;
	"br-in-g1")	LANGUAGE=Braj TYPE="Box drawing" ;;
	"ca-g1")	LANGUAGE=Catalan TYPE="grade 1" ;;
	"chr-us-g1")	LANGUAGE=Cherokee TYPE="grade 1" ;;
	"ckb-g1")	LANGUAGE="Sorani (Kurdish)" TYPE="grade 1" ;;
	"cs-g1")	LANGUAGE=Czech TYPE="grade 1" ;;
	"cy-cy-g1")	LANGUAGE=Welsh TYPE="grade 1" ;;
	"cy-cy-g2")	LANGUAGE=Welsh TYPE="grade 2" ;;
	"Cz-Cz-g1")	LANGUAGE=Czech TYPE="grade 1" ;;
	"da-dk-g08")	LANGUAGE=Danish TYPE="grade 0" ;;
	"da-dk-g16")	LANGUAGE=Danish TYPE="grade 1 6 dots" ;;
	"da-dk-g18")	LANGUAGE=Danish TYPE="grade 1 8 dots" ;;
	"da-dk-g26")	LANGUAGE=Danish TYPE="grade 2 6 dots" ;;
	"da-dk-g26l")	LANGUAGE=Danish TYPE="grade 2 6 dots limited" ;;
	"da-dk-g28")	LANGUAGE=Danish TYPE="grade 2 8 dots" ;;
	"da-dk-g28l")	LANGUAGE=Danish TYPE="grade 2 8 dots limited" ;;
	"da-lt")	LANGUAGE=Danish TYPE="LogText" ;;
	"de-chess")	LANGUAGE=German TYPE="Chess" ;;
	"de-ch-g0")	LANGUAGE=German LOCATION=Swiss TYPE="grade 0" ;;
	"de-ch-g1")	LANGUAGE=German LOCATION=Swiss TYPE="grade 1" ;;
	"de-ch-g2")	LANGUAGE=German LOCATION=Swiss TYPE="grade 2" ;;
	"de-de-comp8")	LANGUAGE=German LOCATION=Germany TYPE="computer" ;;
	"de-de-g0")	LANGUAGE=German LOCATION=Germany TYPE="grade 0" ;;
	"de-de-g1")	LANGUAGE=German LOCATION=Germany TYPE="grade 1" ;;
	"de-de-g2")	LANGUAGE=German LOCATION=Germany TYPE="grade 2" ;;
	"dra")		LANGUAGE=Dravidian TYPE="grade 1" ;;
	"el")		LANGUAGE=Greek TYPE="grade 1" ;;
	"en_CA")	LANGUAGE=English LOCATION=Canada TYPE="grade 1" ;;
	"en-chess")	LANGUAGE=English TYPE="Chess" ;;
	"en-gb-comp8")	LANGUAGE=English LOCATION=U.K. TYPE="computer" ;;
	"en-gb-g1")	LANGUAGE=English LOCATION=U.K. TYPE="grade 1" ;;
	"en-GB-g2")	LANGUAGE=English LOCATION=U.K. TYPE="grade 2" ;;
	"en-in-g1")	LANGUAGE=English LOCATION=India  TYPE="grade 1" ;;
	"en-ueb-g1")	LANGUAGE=English TYPE="Unified grade 1" ;;
	"en-ueb-g2")	LANGUAGE=English TYPE="Unified grade 2" ;;
	"en-ueb-math")	LANGUAGE=English TYPE="Unified math definitions" ;;
	"en-us-comp6")	LANGUAGE=English LOCATION=U.S. TYPE="computer 6 dots" ;;
	"en-us-comp8")	LANGUAGE=English LOCATION=U.S. TYPE="computer 8 dots" ;;
	"en-us-comp8-ext")	LANGUAGE=English LOCATION=U.S. TYPE="computer 8 dots extended" ;;
	"en-us-compbrl")	LANGUAGE=English LOCATION=U.S. TYPE="computer" ;;
	"en-us-g1")	LANGUAGE=English LOCATION=U.S. TYPE="grade 1" ;;
	"en-us-g2")	LANGUAGE=English LOCATION=U.S. TYPE="grade 2" ;;
	"en-us-interline")	LANGUAGE=English LOCATION=U.S. TYPE="interline" ;;
	"en-us-mathtext")	LANGUAGE=English LOCATION=U.S. TYPE="mathtext" ;;
	"eo-g1")	LANGUAGE=Esperanto TYPE="grade 1" ;;
	"eo-g1-x-system")	LANGUAGE=Esperanto TYPE="grade 1 x-system" ;;
	"Es-Es-G0")	LANGUAGE=Spanish TYPE="grade 0" ;;
	"Es-Es-g1")	LANGUAGE=Spanish TYPE="grade 1" ;;
	"es-g1")	LANGUAGE=Spanish TYPE="grade 1" ;;
	"et")		LANGUAGE=Estonian TYPE="grade 1" ;;
	"et-g0")	LANGUAGE=Estonian TYPE="grade 0" ;;
	"ethio-g1")	LANGUAGE=Ethiopic TYPE="grade 1" ;;
	"fi")		LANGUAGE=Finnish TYPE="6 dot" ;;
	"fi1")		LANGUAGE=Finnish TYPE="grade 1" ;;
	"fi2")		LANGUAGE=Finnish TYPE="grade 2" ;;
	"fi-fi-8dot")	LANGUAGE=Finnish TYPE="8dot" ;;
	"fi-fi")	LANGUAGE=Finnish TYPE="6dot" ;;
	"fr-2007")	LANGUAGE=French TYPE="2007" ;;
	"fr-bfu-comp6")	LANGUAGE=French TYPE="Braille Français Unifié computer 6 dots" ;;
	"fr-bfu-comp8")	LANGUAGE=French TYPE="Braille Français Unifié computer 8 dots" ;;
	"fr-bfu-g2")	LANGUAGE=French TYPE="Braille Français Unifié grade 2" ;;
	"fr-ca-g1")	LANGUAGE=French LOCATION=Canada TYPE="grade 1" ;;
	"Fr-Ca-g2")	LANGUAGE=French LOCATION=Canada TYPE="grade 2" ;;
	"fr-fr-g1")	LANGUAGE=French LOCATION=France TYPE="grade 1" ;;
	"Fr-Fr-g2")	LANGUAGE=French LOCATION=France TYPE="grade 2" ;;
	"ga-g1")	LANGUAGE=Gaeilge TYPE="grade 1" ;;
	"ga-g2")	LANGUAGE=Gaeilge TYPE="grade 2" ;;
	"gd")		LANGUAGE=Gaelic TYPE="grade 1" ;;
	"gon")		LANGUAGE=Gondi TYPE="grade 1" ;;
	"gr-bb")	LANGUAGE=Greek TYPE="bb" ;;
	"gr-gr-g1")	LANGUAGE=Greek TYPE="grade 1" ;;
	"gu-in-g1")	LANGUAGE=Gujarati TYPE="grade 1" ;;
	"haw-us-g1")	LANGUAGE=Hawaiian TYPE="grade 1" ;;
	"he")		LANGUAGE=Hebrew TYPE="grade 1" ;;
	"hi-in-g1")	LANGUAGE=Hindi TYPE="grade 1" ;;
	"hr"|"hr-g1")	LANGUAGE=Croatian TYPE="grade 1" ;;
	"hr-comp8")	LANGUAGE=Croatian TYPE="computer 8 dots" ;;
	"hu-hu-comp8")	LANGUAGE=Hungarian TYPE="computer 8 dots" ;;
	"hu-hu-g1")	LANGUAGE=Hungarian TYPE="grade 1" ;;
	"hy")		LANGUAGE=Armenian TYPE="grade 1" ;;
	"is")		LANGUAGE=Icelandic TYPE="grade 1" ;;
	"it-it-comp6")	LANGUAGE=Italian TYPE="computer 6 dots" ;;
	"it-it-comp8")	LANGUAGE=Italian TYPE="computer 8 dots" ;;
	"iu-ca-g1")	LANGUAGE=Inuktitut TYPE="grade 1" ;;
	"ka-in-g1")	LANGUAGE=Kannada TYPE="grade 1" ;;
	"kh-in-g1")	LANGUAGE=Khasi TYPE="grade 1" ;;
	"ko-2006-g1")	LANGUAGE=Korean TYPE="grade 1 2006" ;;
	"ko-2006-g2")	LANGUAGE=Korean TYPE="grade 2 2006" ;;
	"ko-g1")	LANGUAGE=Korean TYPE="grade 1" ;;
	"ko-g2")	LANGUAGE=Korean TYPE="grade 2" ;;
	"kok")		LANGUAGE=Konkani TYPE="grade 1" ;;
	"kru")		LANGUAGE=Kurukh TYPE="grade 1" ;;
	"ks-in-g1")	LANGUAGE=Kashmiri TYPE="grade 1" ;;
	"lt")		LANGUAGE=Lithuanian TYPE="grade 1" ;;
	"lt-6dot")	LANGUAGE=Lithuanian TYPE="6 dots" ;;
	"Lv-Lv-g1")	LANGUAGE=Latvian TYPE="grade 1" ;;
	"mao-nz-g1")	LANGUAGE=Maori TYPE="grade 1" ;;
	"marburg")	TYPE="Marburg maths" ;;
	"marburg_edit")	TYPE="Marburg maths post-translation editing" ;;
	"ml-in-g1")	LANGUAGE=Malayalam TYPE="grade 1" ;;
	"mn-in-g1")	LANGUAGE=Manipuri TYPE="grade 1" ;;
	"mn-MN"|"mn-MN-g1")	LANGUAGE=Mongolian TYPE="grade 1" ;;
	"mn-MN-g2")	LANGUAGE=Mongolian TYPE="grade 2" ;;
	"mr-in-g1")	LANGUAGE=Marathi TYPE="grade 1" ;;
	"mt")		LANGUAGE=Maltese TYPE="grade 1" ;;
	"mun")		LANGUAGE=Munda TYPE="grade 1" ;;
	"mwr")		LANGUAGE=Marwari TYPE="grade 1" ;;
	"ne")		LANGUAGE=Nepali TYPE="grade 1" ;;
	"nemeth")	TYPE="Nemeth Maths" ;;
	"nemeth_edit")	TYPE="Nemeth Maths post-translation editing" ;;
	"nl-BE-g0")	LANGUAGE=Dutch LOCATION=Belgium TYPE="grade 0" ;;
	"nl-g0")	LANGUAGE=Dutch TYPE="grade 0" ;;
	"nl-NL-g0")	LANGUAGE=Dutch LOCATION="Netherlands" TYPE="grade 0" ;;
	"Nl-Nl-g1")	LANGUAGE=Dutch LOCATION=Netherlands TYPE="grade 1" ;;
	"no-no-8dot-fallback-6dot-g0")	LANGUAGE=Norwegian TYPE="grade 0 8 dots fallback 6 dots" ;;
	"no-no-8dot")	LANGUAGE=Norwegian TYPE="grade 0 8 dots" ;;
	"no-no-comp8")	LANGUAGE=Norwegian TYPE="grade 0 computer" ;;
	"no-no-g0")	LANGUAGE=Norwegian TYPE="grade 0" ;;
	"no-no-g1")	LANGUAGE=Norwegian TYPE="grade 1" ;;
	"no-no-g2")	LANGUAGE=Norwegian TYPE="grade 2" ;;
	"no-no-g3")	LANGUAGE=Norwegian TYPE="grade 3" ;;
	"no-no-generic")	LANGUAGE=Norwegian TYPE="generic" ;;
	"np-in-g1")	LANGUAGE=Nepali TYPE="grade 1" ;;
	"or-in-g1")	LANGUAGE=Oriya TYPE="grade 1" ;;
	"pi")		LANGUAGE=Pali TYPE="grade 1" ;;
	"pl-pl-comp8")	LANGUAGE=Polish TYPE="computer" ;;
	"Pl-Pl-g1")	LANGUAGE=Polish TYPE="grade 1" ;;
	"pt-pt-comp8")	LANGUAGE=Portuguese TYPE="computer" ;;
	"pt-pt-g1")	LANGUAGE=Portuguese TYPE="grade 1" ;;
	"pt-pt-g2")	LANGUAGE=Portuguese TYPE="grade 2" ;;
	"pu-in-g1")	LANGUAGE=Punjabi TYPE="grade 1" ;;
	"ro")		LANGUAGE=Romanian TYPE="grade 1" ;;
	"ru-compbrl")	LANGUAGE=Russian TYPE="computer" ;;
	"ru")		LANGUAGE=Russian TYPE="grade 1" ;;
	"ru-litbrl")	LANGUAGE=Russian TYPE="literary" ;;
	"ru-ru-g1")	LANGUAGE=Russian TYPE="grade 1" ;;
	"sa-in-g1")	LANGUAGE=Sasnskrit TYPE="grade 1" ;;
	"se-se")	LANGUAGE=Swedish TYPE="grade 1" ;;
	"Se-Se-g1")	LANGUAGE=Swedish TYPE="grade 1" ;;
	"si-in-g1")	LANGUAGE=Sindhi TYPE="grade 1" ;;
	"sin")		LANGUAGE=Sinhala TYPE="grade 1" ;;
	"sk-g1")	LANGUAGE=Slovak TYPE="grade 1" ;;
	"sk-sk-g1")	LANGUAGE=Slovak TYPE="grade 1" ;;
	"sk-sk")	LANGUAGE=Slovak TYPE="grade 1" ;;
	"sl-si-comp8")	LANGUAGE=Slovenian TYPE="computer" ;;
	"sl-si-g1")	LANGUAGE=Slovenian TYPE="grade 1" ;;
	"sot-za-g1")	LANGUAGE=Sotho TYPE="grade 1" ;;
	"spaces")	TYPE="Spaces" ;;
	"sr-g1")	LANGUAGE=Serbian TYPE="grade 1" ;;
	"sv-1989")	LANGUAGE=Swedish TYPE="1989" ;;
	"sv-1996")	LANGUAGE=Swedish TYPE="1996" ;;
	"ta")		LANGUAGE=Tamil TYPE="grade 1" ;;
	"ta-ta-g1")	LANGUAGE=Tamil TYPE="grade 1" ;;
	"te-in-g1")	LANGUAGE=Telugu TYPE="grade 1" ;;
	"tr"|"tr-g1")	LANGUAGE=Turkish TYPE="grade 1" ;;
	"tr-g2")	LANGUAGE=Turkish TYPE="grade 2" ;;
	"tsn-za-g1")	LANGUAGE=Tswana TYPE="grade 1" ;;
	"UEBC-g1")	LANGUAGE=English TYPE="Unified grade 1" ;;
	"UEBC-g2")	LANGUAGE=English TYPE="Unified grade 2" ;;
	"ukmaths")	TYPE="U.K maths" ;;
	"ukmaths_edit")	TYPE="U.K maths post-translation editing" ;;
	"ur-pk-g1")	LANGUAGE=Urdu TYPE="grade 1" ;;
	"ur-pk-g2")	LANGUAGE=Urdu TYPE="grade 2" ;;
	"vi")		LANGUAGE=Vietnamese TYPE="grade 1" ;;
	"vi-g1")	LANGUAGE=Vietnamese TYPE="grade 1" ;;
	"wiskunde")	LANGUAGE=Flemish TYPE="grade 1" ;;
	"zh-hk")	LANGUAGE=Chinese LOCATION="Hong Kong" TYPE="grade 1" ;;
	"zh-tw")	LANGUAGE=Chinese LOCATION="Taiwan" TYPE="grade 1" ;;
	"zh-chn")	LANGUAGE=Chinese LOCATION="China" TYPE="grade 1" ;;
	*)		locale=$(grep ^#+locale: "$i" | cut -d ':' -f 2-)
			if [ -n "$locale" ]; then
			  LANGUAGE="$locale"
			fi
			if [ $ext = ctb ]; then
			  TYPE="contracted"
			else
			  TYPE="computer"
			fi
			;;
      esac
    fi
    echo "$file:$LANGUAGE:$LOCATION:$TYPE:$DISPLAY_NAME"
  done

  for i in $TABLESDIR/hyph_*.dic
  do
    file=${i##*/}
    name=${file%.dic}
    name=${name#hyph_}
    DESC=$name
    LANGUAGE=""
    LOCATION=""
    GRADE=""
    case $name in
	brl_da_dk)	LANGUAGE=Danish ;;
	cs_CZ)	LANGUAGE=Czech ;;
	da_DK)	LANGUAGE=Danish ;;
	de_DE)	LANGUAGE=German ;;
	en_US)	LANGUAGE=English ;;
	eo)	LANGUAGE=Esperanto ;;
	es_ES)	LANGUAGE=Spanish ;;
	fr_FR)	LANGUAGE=French ;;
	hu_HU)	LANGUAGE=Hungarian ;;
	it_IT)	LANGUAGE=Italian ;;
	nb_NO)	LANGUAGE="Norwegian Bokmål" ;;
	nl_NL)	LANGUAGE=Dutch ;;
	nn_NO)	LANGUAGE="Norwegian Nynorsk" ;;
	pl_PL)	LANGUAGE=Polish ;;
	pt_PT)	LANGUAGE=Portuguese ;;
	ru)	LANGUAGE=Russian ;;
	sv_SE)	LANGUAGE=Swedish ;;
	*)      LANGUAGE=$name ;;
    esac
    echo "$file:$LANGUAGE:$LOCATION:hyphenation rules:"
  done

) | LC_ALL=C sort -f -t : -k 2,3 | (

IFS=:
while read file LANGUAGE LOCATION TYPE DISPLAY_NAME
do
	DESC="$TYPE ($file)"
	[ -n "$LOCATION" ] && DESC="$LOCATION $DESC"
	[ -n "$LANGUAGE" ] && DESC="$LANGUAGE $DESC"
	[ -n "$DISPLAY_NAME" ] && DESC="$DISPLAY_NAME ($file)"
	echo "  Choice \"$file/$DESC\" \"\""
done
)

