#![cfg(feature = "with_capi")]

use std::{env, io::ErrorKind, process::Command};

use tempdir::TempDir;

#[test]
fn test_server()
{
    let gcc_test = 
        Command::new("gcc").arg("-v").status();

    if let Err(e) = gcc_test && e.kind() == ErrorKind::NotFound
    {
        eprintln!("cannot complete test, gcc not found, check PATH");
        return;
    }

    let workdir = TempDir::new("scramrs").unwrap();

    let lib_path = env::current_dir().unwrap().join("target/debug/libscram_rs.a");
    let c_filepath =  env::current_dir().unwrap().join("tests/server/server.c");
    let binary_location = workdir.path().join("server");

    if lib_path.exists() == false
    {
        let cargo_build = 
            Command::new("cargo").arg("build").status();

        if cargo_build.unwrap().success() == false
        {
            eprintln!("cannot complete test, cargo build failed");
            return;
        }
    }

    let gcc = 
        Command::new("gcc")
            .arg("-o").arg(&binary_location)
            .arg(&c_filepath)
            .arg(lib_path)
            .status();

    
    let test_res = 
        Command::new(&binary_location).status().unwrap();

    assert_eq!(test_res.success(), true);

}