#!/bin/bash
#-------------------------------------------------------------------------------
# Copyright (C) 2012-2019 British Crown (Met Office) & Contributors.
#
# This file is part of Rose, a framework for meteorological suites.
#
# Rose is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rose is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rose. If not, see <http://www.gnu.org/licenses/>.
#-------------------------------------------------------------------------------
# Provides init, setup and teardown functions for rose macro tests.
#-------------------------------------------------------------------------------
. $(dirname $0)/../lib/bash/test_header

function init() {
    mkdir -p $TEST_DIR/config
    cat >$TEST_DIR/config/rose-app.conf
}

function init_opt() {
    local opt_key=$1
    mkdir -p $TEST_DIR/config/opt/
    cat >$TEST_DIR/config/opt/rose-app-$opt_key.conf
}

function init_meta() {
    mkdir -p $TEST_DIR/config/meta
    cat >$TEST_DIR/config/meta/rose-meta.conf
}

function init_rose_meta() {
    category=$1
    mkdir -p $TEST_DIR/rose-meta/$category/
    shift
    for version; do
        mkdir -p $TEST_DIR/rose-meta/$category/$version/
        touch $TEST_DIR/rose-meta/$category/$version/rose-meta.conf
    done
}

function init_rose_meta_content() {
    category=$1
    version=$2
    cat >$TEST_DIR/rose-meta/$category/$version/rose-meta.conf
}

function init_macro() {
    mkdir -p $TEST_DIR/config/meta/lib/python/macros/
    cat >$TEST_DIR/config/meta/lib/python/macros/$1
}

function init_rose_meta_macro() {
    category=$1
    version=$2
    macro_filename=$3
    mkdir -p "$TEST_DIR/rose-meta/$category/$version/lib/python/macros/"
    touch "$TEST_DIR/rose-meta/$category/$version/lib/python/macros/__init__.py"
    cat >"$TEST_DIR/rose-meta/$category/$version/lib/python/macros/$macro_filename"
}

function setup() {
    mkdir $TEST_DIR/run
    cd $TEST_DIR/run
}

function teardown() {
    cd $TEST_DIR
    rm -rf $TEST_DIR/run
    rm -rf $TEST_DIR/config/meta
}

function grep_count_ok() {
    local TEST_KEY=$1
    local PATTERN=$2
    local FILE=$3
    local COUNT=$4
    if (( $(grep "$PATTERN" "$FILE" -c) == $COUNT )); then
        pass "$TEST_KEY"
        return
    fi
    fail "$TEST_KEY"
}
