/*
 * This file is part of espanso.
 *
 * Copyright (C) 2019-2021 Federico Terzi
 *
 * espanso 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.
 *
 * espanso 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 espanso.  If not, see <https://www.gnu.org/licenses/>.
 */

use anyhow::Result;
use log::info;
use std::process::Command;
use sysinfo::{System, SystemExt};

#[cfg(target_os = "windows")]
pub fn set_command_flags(command: &mut Command) {
    // Avoid showing the shell window
    // See: https://github.com/espanso/espanso/issues/249
    // and https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags
    use std::os::windows::process::CommandExt;

    const CREATE_NO_WINDOW: u32 = 0x0800_0000;
    command.creation_flags(CREATE_NO_WINDOW);
}

#[cfg(not(target_os = "windows"))]
pub fn set_command_flags(_: &mut Command) {
    // NOOP on Linux and macOS
}

#[cfg(target_os = "windows")]
pub fn attach_console() -> Result<()> {
    // When using the windows subsystem we loose the terminal output.
    // Therefore we try to attach to the current console if available.
    use windows::Win32::System::Console::AttachConsole;
    unsafe { AttachConsole(0xFFFF_FFFF)? };
    Ok(())
}

#[cfg(not(target_os = "windows"))]
pub fn attach_console() -> Result<()> {
    // Not necessary on Linux and macOS
    Ok(())
}

pub fn log_system_info() {
    let sys = System::new();
    info!(
        "system info: {} v{} - kernel: {}",
        sys.name().unwrap_or_default(),
        sys.os_version().unwrap_or_default(),
        sys.kernel_version().unwrap_or_default()
    );
}
