use std::process::Command;

fn main() {
    // Base version from Cargo
    let pkg_version = std::env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "0.0.0".to_string());

    // Try to append short git SHA and dirty flag
    let mut version = pkg_version.clone();

    if let Ok(out) = Command::new("git").args(["rev-parse", "--short", "HEAD"]).output() {
        if out.status.success() {
            let sha = String::from_utf8_lossy(&out.stdout).trim().to_string();
            if !sha.is_empty() {
                version = format!("{}+{}", pkg_version, sha);

                // Mark dirty if there are local changes
                if let Ok(st) = Command::new("git").args(["status", "--porcelain"]).output() {
                    if !st.stdout.is_empty() {
                        version.push_str("-dirty");
                    }
                }
            }
        }
    }

    println!("cargo:rustc-env=SNIPPETS_BUILD_VERSION={}", version);
}

