#!/bin/bash
set -e

pkg=yaggo

if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cd "${AUTOPKGTEST_TMP}"

function compare_cksum()
{
	# $1: file whose md5sum needs to be tested
	# $2: expected checksum
	if [ "$(cat $1 | md5sum | awk '{print $1}')" != "$2" ]
	then
		echo "Checksums do not match"
		exit 1
	fi

}

# Prepare .yaggo file
cat<<EOF > parser.yaggo
purpose "Demonstrate yaggo capabilities"
description "This simple configuration file shows some of the capabilities of yaggo.
This is supposed to be a longer description of the program.
"

option("f", "flag") {
  description "This is a flag"
  off
}
option("i", "int") {
  description "This take an integer"
  int
  default 20
}
arg("path") {
  description "Path to file"
  c_string
}
EOF

# Prepare a .cpp file using the header output by yaggo
cat<<EOF > parser.cpp
#include <iostream>
#include "parser.hpp"

int main(int argc, char* argv[]) {
  parser args(argc, argv); // Does all the parsing

  std::cout << "--flag " << (args.flag_flag ? "not passed" : "passed") << "\n"
            << "--int: " << args.int_arg << "\n"
            << "path: " << args.path_arg << "\n";

  return 0;
}
EOF

# Make the header file and compile the parser C++ program
yaggo parser.yaggo
g++ -o parser parser.cpp

echo "Test 1: Check if --int and --flag work in sample program"
./parser --int=20 ./parser.yaggo > test1
compare_cksum test1 b55b48b745aa2be80024b8698a2c2718 
rm -f test1
echo "PASS"

echo "Test 2: Check if --help works in sample program"
./parser --help > test2
compare_cksum test2 e537fc705813c89adf9386e10b3f5e0b
rm -f test2
echo "PASS"
