#!/usr/bin/env bash
set -e

function doConfigure()
{
    if [ ! -f configure ]; then
        ./buildconf
    fi
    ./configure
}

function doBuild()
{
    if [ ! -f Makefile ]; then
        doConfigure
    fi;
    make -j`nproc`
}

function doTest()
{
    if [ ! -f Makefile ]; then
        doConfigure
    fi;
    TESTS="$1" make -j`nproc` test
}

function doRunCli()
{
    if [ ! -f sapi/cli/php ]; then
        doBuild
    fi;
    sapi/cli/php "$@"
}

case "$1" in
    "build")
        doBuild
        ;;
    "configure")
        doConfigure
        ;;
    "test")
        doTest "$2"
        ;;
    "run-cli")
        doRunCli "${@:2}"
        ;;
    *)
        echo "Unknown command"
        exit 2
        ;;
esac
