#!/usr/bin/env bash

# test that we can compile a simple program using Rumur's API

if [ -z "${AUTOPKGTEST_TMP}" ]; then
  printf 'AUTOPKGTEST_TMP not set; not running in autopkgtest?\n' >&2
  exit 1
fi

set -e
set -x

# move to temporary directory
mkdir -p ${AUTOPKGTEST_TMP}/librumur-api
cd ${AUTOPKGTEST_TMP}/librumur-api

# construct a simple model
cat - >model.m <<EOT
var
  x: boolean;

startstate begin
  x := true;
end;

rule begin
  x := !x;
end;
EOT

# construct a C++11 program using Rumur's API
cat - >main.cc <<EOT
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <rumur/rumur.h>

int main(void) {

  // open the input model
  std::ifstream input("model.m");
  if (!input.is_open()) {
    std::cerr << "failed to open model.m\n";
    return EXIT_FAILURE;
  }

  // parse it into an AST
  try {
    rumur::Ptr<rumur::Model> ast = rumur::parse_model(input);
  } catch (rumur::Error &e) {
    std::cerr << "failed to parse model.m: " << e.what() << "\n";
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}
EOT

# compile the program
${CXX:-c++} -std=c++11 main.cc -lrumur -lgmpxx -lgmp

# in case it was dynamically linked, also confirm we can run it
./a.out
