/* main.rs
 *
 * Copyright 2023 Felipe Kinoshita
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

mod application;
#[rustfmt::skip]
mod config;
mod flags_dialog;
mod i18n;
mod capture;
mod quick_reference_dialog;
mod window;

use std::env;

use gettextrs::{gettext, LocaleCategory};
use gtk::{gio, glib};
use log::info;

use self::application::Application;
use self::config::{GETTEXT_PACKAGE, LOCALEDIR, RESOURCES_FILE};

fn main() -> glib::ExitCode {
    //init logger
    pretty_env_logger::init();

    // Prepare i18n
    let app_lang = gettextrs::setlocale(
        LocaleCategory::LcAll,
        env::var("LC_ALL").unwrap_or_else(|_| {
            env::var("LC_CTYPE").unwrap_or_else(|_| env::var("LANG").unwrap_or_default())
        }),
    )
    .expect("Failed to set locale, please use a valid system locale and make sure it's enabled");
    gettextrs::bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR).expect("Unable to bind the text domain");
    gettextrs::textdomain(GETTEXT_PACKAGE).expect("Unable to switch to the text domain");
    info!("Application locale: {}", String::from_utf8_lossy(&app_lang));

    glib::set_application_name(&gettext("Wildcard"));

    let res = gio::Resource::load(RESOURCES_FILE).expect("Could not load gresource file");
    gio::resources_register(&res);

    let app = Application::default();

    app.run()
}
