/*
 * This file is part of the OpenKropki project.
 *
 * Copyright (C) 2014-2026 Mateusz Viste
 *
 * MIT License
 *
 * 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.
 *
 * ---------------------------------------------------------------------------
 * This module provides a function that tries to detect the current system
 * language on Linux and Windows and returns its identifier.
 *
 * First, it looks for the $LANG env variable. If not found, and the platform
 * is Windows, then it attemps to call the Windows NLS API.
 *
 * Returns LANG_ERR if detection was unable to match anything.
 */

#include <stdlib.h>
#include <string.h>

#include "lang.h"
#include "langdetect.h"

#ifdef _WIN32
#include <windows.h>
#endif

enum langid_t langdetect(void) {
  const char *LANG;

  /* is $LANG defined and non-empty ? */
  LANG = getenv("LANG");
  if ((LANG != NULL) && (LANG[0] != 0)) {
    char l[3];
    l[0] = LANG[0];
    l[1] = LANG[1];
    l[2] = 0;
    /* match it to one of the supported langs */
    if (strcmp(l, "cs") == 0) return(LANG_CZ);
    if (strcmp(l, "de") == 0) return(LANG_DE);
    if (strcmp(l, "en") == 0) return(LANG_EN);
    if (strcmp(l, "fr") == 0) return(LANG_FR);
    if (strcmp(l, "pl") == 0) return(LANG_PL);
    if (strcmp(l, "ru") == 0) return(LANG_RU);
    return(LANG_ERR);
  }

#ifdef _WIN32
  /* on Windows, ask the NLS API -- API call reference:
   * https://docs.microsoft.com/en-us/windows/win32/api/winnls/nf-winnls-getuserdefaultlangid
   *
   * The Windows "Language ID" value is defined in the MS-LCID documentation:
   * https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/70feba9f-294e-491e-b6eb-56532684c37f */
  switch (PRIMARYLANGID(GetUserDefaultLangID())) {
    case 0x05: return(LANG_CZ);  /* cs (czech) */
    case 0x07: return(LANG_DE);  /* de */
    case 0x09: return(LANG_EN);  /* en */
    case 0x0c: return(LANG_FR);  /* fr */
    case 0x15: return(LANG_PL);  /* pl */
    case 0x19: return(LANG_RU);  /* ru */
  }
#endif

  return(LANG_ERR);
}
