#!/bin/bash
METAL="ON"
CLEAN=""
BUILD_ONLY=""

WORK_DIR=`pwd`
BUILD_DIR=build
FILTER=""
DUMP_DIR=$WORK_DIR/dump_data

function usage() {
    echo "-c\tClean up build folders."
}
function die() {
    echo $1
    exit 1
}

function clean_build() {
    echo $1 | grep "$BUILD_DIR\b" > /dev/null
    if [[ "$?" != "0" ]]; then
        die "Warnning: $1 seems not to be a BUILD folder."
    fi
    rm -rf $1
    mkdir $1
}

function build_unit_test() {
    if [ "-c" == "$CLEAN" ]; then
        clean_build $BUILD_DIR
    fi
    mkdir -p $BUILD_DIR
    cd $BUILD_DIR
    cmake ../../.. \
          -DCMAKE_BUILD_TYPE=DEBUG \
          -DTNN_BENCHMARK_MODE:BOOL="ON" \
          -DTNN_TEST_ENABLE:BOOL="ON"  \
          -DTNN_UNIT_TEST_ENABLE:BOOL="ON"  \
          -DTNN_METAL_ENABLE:BOOL=$METAL
    make -j
}

function run_unit_test() {
    build_unit_test
    if [ $? != 0 ]; then
        echo "build failed!"
        exit 0
    fi
    if [ "" != "$BUILD_ONLY" ]; then
        echo "build done!"
        exit 0
    fi
    mkdir -p $DUMP_DIR
    ./test/unit_test/unit_test -dt METAL -lp ./tnn.metallib --gtest_filter="*${FILTER}*"
}

while [ "$1" != "" ]; do
    case $1 in
        -c)
            shift
            CLEAN="-c"
            ;;
        -b)
            shift
            BUILD_ONLY="-b"
            ;;
        -f)
            shift
            FILTER=$1
            shift
            ;;
        *)
            usage
            exit 1
    esac
done

run_unit_test
