#!/bin/bash

set -e

normalize() {
    # | remove comments | remove trailing spaces | remove empty lines
    cat | sed -r 's/#.+$//g' | sed -r 's/\s*$//g' | sed -r '/^\s*$/d'
}

getkey() {
    cat | grep -oP "$1: \K.+" || true
}

delkey() {
    cat | sed "/^$1:/d"
}

read_opts() {
    cat | delkey runner | delkey name | delkey default | sed -e 's/^/--/' | sed -e 's/://' | tr '\n' ' '
}

zouqi() {
    name=""
    runner=""
    config_opts=""

    parse_recursively() {
        local file=$1

        if [ ! -f $file ]; then
            echo $file does not exist.
            exit 1
        fi

        local yaml=$(cat $file | normalize)

        local dfiles=$(echo "$yaml" | getkey default | tr -d '[]')
        for dfile in ${dfiles}; do
            parse_recursively ${dfile%,}
        done

        local name_tmp=$(echo "$yaml" | getkey name)
        if [ ! -z $name_tmp ]; then
            name=$name_tmp
        fi

        local runner_tmp=$(echo "$yaml" | getkey runner)
        if [ ! -z $runner_tmp ]; then
            runner=$runner_tmp
        fi

        config_opts="$config_opts $(echo "$yaml" | read_opts)"

        if [ -z $name ]; then
            name=$(basename "${s%.*}")
        fi
    }

    parse_recursively $1

    if [ -z $runner ]; then
        echo No runner detected in the configs, please specify a runner in the config file.
        exit 1
    fi

    cmd="$runner $config_opts --name $name ${@:2}"
    bash -c "$cmd"
}

case "$1" in
*.y*ml)
    zouqi "$@"
    ;;
board)
    zqboard "${@:2}"
    ;;
*)
    echo "Error: Unknown command: \"$1\"."
    ;;
esac
