#!/bin/sh
set -e

WORKDIR=`mktemp -d`
trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM
cd $WORKDIR
cat <<EOF >buildtest.c
#include <r3.h>

int main()
{
    node *n;

    // create a router tree with 10 children capacity (this capacity can grow dynamically)
    n = r3_tree_create(10);

    int route_data = 3;

    // insert the route path into the router tree
    r3_tree_insert_path(n, "/bar", &route_data); // ignore the length of path

    r3_tree_insert_pathl(n, "/zoo", strlen("/zoo"), &route_data );
    r3_tree_insert_pathl(n, "/foo/bar", strlen("/foo/bar"), &route_data );

    r3_tree_insert_pathl(n ,"/post/{id}", strlen("/post/{id}") , &route_data );

    r3_tree_insert_pathl(n, "/user/{id:\\\\d+}", strlen("/user/{id:\\\\d+}"), &route_data );

    // let's compile the tree!
    r3_tree_compile(n, 0);

    // dump the compiled tree
    r3_tree_dump(n, 0);

    // match a route
    node *matched_node = r3_tree_match(n, "/foo/bar", NULL);
    if (matched_node) {
        matched_node->endpoint; // make sure there is a route end at here.
    }

    // release the tree
    r3_tree_free(n);
    return 0;
}
EOF

gcc -std=c99 -o buildtest buildtest.c `pkg-config --cflags --libs r3`
./buildtest
