#!/usr/bin/python3

# Copyright 2021, Carsten Schoenert <carsten.schoenert@puri.sm>
# SPDX-License-Identifier:  GPL-2.0+

import gzip
import os.path
import sys

# data file to check
DATA_FILE = "/usr/share/localechooser/languagelist.data.gz"

# reference entries to check for
REFERENCE = (
 ['C', 'C'],
 ['am', 'Amharic'],
 ['ar', 'Arabic'],
 ['ast', 'Asturian'],
 ['be', 'Belarusian'],
 ['bg', 'Bulgarian'],
 ['bn', 'Bangla'],
 ['bo', 'Tibetan'],
 ['bs', 'Bosnian'],
 ['ca', 'Catalan'],
 ['cs', 'Czech'],
 ['cy', 'Welsh'],
 ['da', 'Danish'],
 ['de', 'German'],
 ['dz', 'Dzongkha'],
 ['el', 'Greek'],
 ['en', 'English'],
 ['eo', 'Esperanto'],
 ['es', 'Spanish'],
 ['et', 'Estonian'],
 ['eu', 'Basque'],
 ['fa', 'Persian'],
 ['fi', 'Finnish'],
 ['fr', 'French'],
 ['ga', 'Irish'],
 ['gl', 'Galician'],
 ['gu', 'Gujarati'],
 ['he', 'Hebrew'],
 ['hi', 'Hindi'],
 ['hr', 'Croatian'],
 ['hu', 'Hungarian'],
 ['id', 'Indonesian'],
 ['is', 'Icelandic'],
 ['it', 'Italian'],
 ['ja', 'Japanese'],
 ['ka', 'Georgian'],
 ['kab', 'Kabyle'],
 ['kk', 'Kazakh'],
 ['km', 'Khmer'],
 ['kn', 'Kannada'],
 ['ko', 'Korean'],
 ['ku', 'Kurdish'],
 ['lo', 'Lao'],
 ['lt', 'Lithuanian'],
 ['lv', 'Latvian'],
 ['mk', 'Macedonian'],
 ['ml', 'Malayalam'],
 ['mr', 'Marathi'],
 ['my', 'Burmese'],
 ['nb_NO', 'Norwegian Bokmaal'],
 ['ne', 'Nepali'],
 ['nl', 'Dutch'],
 ['nn_NO', 'Norwegian Nynorsk'],
 ['oc', 'Occitan'],
 ['pa', 'Punjabi (Gurmukhi)'],
 ['pl', 'Polish'],
 ['pt', 'Portuguese'],
 ['pt_BR', 'Portuguese (Brazil)'],
 ['ro', 'Romanian'],
 ['ru', 'Russian'],
 ['se_NO', 'Northern Sami'],
 ['si', 'Sinhala'],
 ['sk', 'Slovak'],
 ['sl', 'Slovenian'],
 ['sq', 'Albanian'],
 ['sr', 'Serbian (Cyrillic)'],
 ['sv', 'Swedish'],
 ['ta', 'Tamil'],
 ['te', 'Telugu'],
 ['tg', 'Tajik'],
 ['th', 'Thai'],
 ['tl', 'Tagalog'],
 ['tr', 'Turkish'],
 ['ug', 'Uyghur'],
 ['uk', 'Ukrainian'],
 ['vi', 'Vietnamese'],
 ['zh_CN', 'Chinese (Simplified)'],
 ['zh_TW', 'Chinese (Traditional'],
)

# unicode sign for success (Heavy Check Mark)
SUCCESS="\u2705"
# unicode sign for failure (Cross Mark Emoji)
FAIL="\u274C"


def check_file_available(file):
    ''' check the file can be found '''
    try:
        with open(file, 'r') as f:
            return
    except OSError:
        return 'ERROR'


def get_file_content(file):
    ''' extract the content of the file as list '''
    with gzip.open(file,'rt') as f:
        CONTENT_LIST = []
        for line in f:
            data_content = line.split(':')
            CONTENT_LIST.append((data_content[1], data_content[2]))
        return CONTENT_LIST


def get_l10n_list(data):
    list_data =[]
    for l10n_code, lang in data:
        list_data.append(l10n_code)
    return(sorted(list_data))


def compare_data(data):
    ret = 0
    l10n_LIST_DATA = get_l10n_list(data)
    l10n_LIST_REFERENCE = get_l10n_list(REFERENCE)

    for i in range(len(l10n_LIST_REFERENCE)):
        if l10n_LIST_DATA[i] == l10n_LIST_REFERENCE[i]:
            print(f"  {SUCCESS} Index {i}: Found l10n code {l10n_LIST_REFERENCE[i]}")
        else:
           print(f"  {FAIL} Index {i}: Couldn't found l10n code {l10n_LIST_REFERENCE[i]} in data!")
           ret = 1
    return ret


if __name__ == "__main__":
    ret = check_file_available(DATA_FILE)
    if ret == 'ERROR':
        print(f" {FAIL} Couln't find '{DATA_FILE}'!")
        sys.exit(1)
    else:
        print(f" {SUCCESS} Checking for existance of '{DATA_FILE}'.")

    DATA_FILE_CONTENT = get_file_content(DATA_FILE)
    COUNT_DATA_FILE = len(DATA_FILE_CONTENT)
    COUNT_DATA_REFERENCE = len(REFERENCE)

    if COUNT_DATA_FILE == COUNT_DATA_REFERENCE:
        print(f" {SUCCESS} Checking counts of data entries are equal to reference (count reference {COUNT_DATA_REFERENCE} equal count data {COUNT_DATA_FILE}).")

    if COUNT_DATA_FILE < COUNT_DATA_REFERENCE:
        print(f" {FAIL} Count data entries ({COUNT_DATA_FILE}) is less than count of reference data {COUNT_DATA_REFERENCE})!!!")
        sys.exit(1)

    if COUNT_DATA_FILE > COUNT_DATA_REFERENCE:
        print(f" {FAIL} Count data entries ({COUNT_DATA_FILE}) is greater than count of reference data {COUNT_DATA_REFERENCE})!!!")
        sys.exit(1)

    if compare_data(DATA_FILE_CONTENT) == 0:
        print(f" {SUCCESS} Data to compare are equal to reference data.")
    else:
        print(f" {FAIL} Data to compare are NOT equal to reference data!!!")
        sys.exit(1)

