#!/usr/bin/env python
# Part of measurement-kit <https://measurement-kit.github.io/>.
# Measurement-kit is free software. See AUTHORS and LICENSE for more
# information on the copying conditions.

""" Usage: bundle-ca libressl-version ca-bundle-path output-header-path """

import sys

def main():
    """ Main function """
    libressl_version = sys.argv[1].encode("ascii")
    ca_bundle_path = sys.argv[2]
    output_header_path = sys.argv[3]
    with open(ca_bundle_path, "rb") as filep:
        ca_bundle_data = filep.read()
    with open(output_header_path, "wb") as filep:
        filep.write(b"""
/*
 * Warning: autogenerated file; do not edit!
 *
 * Returns the CA file shipped with the libressl version we compile with
 * as a vector of unsigned characters. This could then be passed to libressl
 * specific functions for loading CAs from a memory buffer.
 *
 * Based on curl cacert-%s
 */

#include "private/net/builtin_ca_bundle.hpp"

namespace mk {
namespace net {

std::vector<uint8_t> builtin_ca_bundle() {
    static std::vector<uint8_t> cav{
""" % libressl_version)
        for number in ca_bundle_data:
            if sys.version_info.major == 2:
                number = ord(number)
            filep.write(b"%d," % number)
        filep.write(b"""
    };
    return cav;
}

} // namespace net
} // namespace mk
""")

if __name__ == "__main__":
    main()
