fn main() {
    let hash = std::process::Command::new("git")
        .args(["rev-parse", "--short", "HEAD"])
        .output()
        .ok()
        .and_then(|o| String::from_utf8(o.stdout).ok())
        .unwrap_or_default();
    let hash = hash.trim();
    println!("cargo:rustc-env=GIT_HASH={hash}");

    // Combined version string for clap --version flag
    let pkg_version = std::env::var("CARGO_PKG_VERSION").unwrap_or_default();
    if hash.is_empty() {
        println!("cargo:rustc-env=LORE_VERSION={pkg_version}");
    } else {
        println!("cargo:rustc-env=LORE_VERSION={pkg_version} ({hash})");
    }

    println!("cargo:rerun-if-changed=.git/HEAD");
    println!("cargo:rerun-if-changed=.git/refs/heads");
}
