// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

use semver::Version;

use devicemapper_sys::{DM_VERSION_MAJOR, DM_VERSION_MINOR, DM_VERSION_PATCHLEVEL};

// List of DM ioctl interface versions that introduce new ioctl commands
static DM_VERSIONS: &[&str] = &["4.1.0", "4.2.0", "4.6.0", "4.37.0", "4.41.0"];

fn main() {
    let version = Version::parse(&format!(
        "{DM_VERSION_MAJOR}.{DM_VERSION_MINOR}.{DM_VERSION_PATCHLEVEL}"
    ))
    .expect("simple version string is not parseable");

    for ver_string in DM_VERSIONS.iter() {
        let version_cfg = format!(
            "devicemapper{}supported",
            ver_string.split('.').take(2).collect::<Vec<_>>().join("")
        );
        println!("cargo::rustc-check-cfg=cfg({version_cfg})");

        let iter_version = Version::parse(ver_string).expect("Could not parse version");
        if version >= iter_version {
            println!("cargo:rustc-cfg={version_cfg}");
        }
    }
}
