// https://www.boost.org/doc/libs/1_92_0/doc/html/process.html

#include <boost/asio/io_context.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/readable_pipe.hpp>
#include <boost/process.hpp>

#include <cassert>
#include <iostream>
#include <string>

namespace bp = boost::process::v2;

int main()
{
    boost::asio::io_context ctx;
    boost::asio::readable_pipe out(ctx);

    // Read the child's standard output through a pipe
    bp::process child(ctx, bp::environment::find_executable("echo"),
                      { "hello", "from", "boost.process" },
                      bp::process_stdio{ {}, out, {} });

    std::string data;
    boost::system::error_code ec;
    boost::asio::read(out, boost::asio::dynamic_buffer(data), ec);
    assert(ec == boost::asio::error::eof || !ec);

    const int rc = child.wait();
    std::cout << "child said: " << data;
    std::cout << "exit code: " << rc << std::endl;

    assert(rc == 0);
    assert(data == "hello from boost.process\n");

    // Environment variables are passed through to the child
    bp::process env_child(ctx, bp::environment::find_executable("sh"),
                          { "-c", "test \"$BOOST_AUTOPKGTEST\" = yes" },
                          bp::process_environment{ "BOOST_AUTOPKGTEST=yes" });
    const int env_rc = env_child.wait();
    std::cout << "env check: " << env_rc << std::endl;
    assert(env_rc == 0);

    return 0;
}
