// Copyright (C) 2026 Michael Wilson <mike@mdwn.dev>
//
// 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, version 3.
//
// 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 <https://www.gnu.org/licenses/>.
//
use std::{env, error::Error, path::PathBuf, process::Command};

/// Check that a pkg-config library is available and print a helpful message if not.
fn check_lib(lib: &str, package_hint: &str) {
    let status = Command::new("pkg-config").args(["--exists", lib]).status();
    match status {
        Ok(s) if s.success() => {}
        _ => {
            println!(
                "cargo:warning=System library '{lib}' not found. \
                 Install it (e.g. `apt install {package_hint}`) or run: ./setup.sh"
            );
        }
    }
}

fn main() -> Result<(), Box<dyn Error>> {
    // Check for required system libraries early so the user gets a clear message
    // instead of cryptic linker errors.
    if Command::new("pkg-config").arg("--version").status().is_ok() {
        if env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("linux") {
            check_lib("alsa", "libasound2-dev");
            check_lib("libudev", "libudev-dev");
        }
        check_lib("openssl", "libssl-dev");
    }

    let out_dir = PathBuf::from(env::var("OUT_DIR")?);

    tonic_prost_build::configure()
        .file_descriptor_set_path(out_dir.join("player_descriptor.bin"))
        .compile_protos(&["src/proto/player/v1/player.proto"], &["src/proto"])?;

    // Embed git hash.
    let git_hash = Command::new("git")
        .args(["rev-parse", "--short", "HEAD"])
        .output()
        .ok()
        .filter(|o| o.status.success())
        .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
        .unwrap_or_else(|| "unknown".to_string());
    println!("cargo:rustc-env=MTRACK_GIT_HASH={git_hash}");

    // Embed build timestamp (UTC, ISO 8601).
    let build_time = jiff::Timestamp::now().strftime("%Y-%m-%dT%H:%M:%SZ");
    println!("cargo:rustc-env=MTRACK_BUILD_TIME={build_time}");

    // Re-run when git HEAD changes.
    println!("cargo:rerun-if-changed=.git/HEAD");
    println!("cargo:rerun-if-changed=.git/refs");

    Ok(())
}
