#!/bin/bash

lang=${LANG%_*}
declare verbose
declare -A comments
declare -A META

function base_main()
{
    action="_markdown"

    # dispatch arguments
    while [ ${#} -gt 0 ]; do
	case ${1} in
            -f|--fix) action="_fix"; shift;;
            -c|--check) action="_check"; shift;;
            -h|--help) action="_help"; break;;
	    -d|--description) action="_description"; shift;;
	    -t|--title) action="_title"; shift;;
	    --force) force=t; shift;;
	    -l|--lang) lang=${1}; shift;;
	    -m|--meta) action="_meta"; shift;;
	    -v|--verbose) verbose=t; shift;;
	    --verify) action="_verify"; break;;
            *) action="_help"; shift;;
	esac
    done

    if [ "$lang" == "en" ]; then
	lang=""
    else
	lang="_$lang"
    fi

    case $action in
	_verify)
	    echo -n "verified fixme script"
	    ;;
	_fix)
	    if [ $force ]; then

		fix
	    else
		get_translation fix
		if [[ $verbose ]]; then
		    declare -f fix
		fi
	    fi
	    ;;
	_check)
	    if [ $force ]; then
		check
	    else
		get_translation check
		if [[ $verbose ]]; then
		    declare -f check
		fi
	    fi
	    ;;
	_meta)
	    local sep=""
	    echo -n "{"
	    for key in "${!META[@]}"; do
		local v="${META[$key]}"
		if [[ $v != "true" && $v != "false" ]]; then
		    v="\"$v\""
		fi
		echo -n "$sep\"$key\":$v"
		sep=","
	    done
	    echo "}"
	    ;;
	_description)
	    get_translation "description"
	    ;;
	_title)
		head -n1 <<EOF
$(get_translation "description")
EOF
	    ;;
	_markdown) base_markdown;;
	_help) base_help;;
    esac
}

function COMMENT()
{
    if [ ${#} -eq 1 ]; then
	IFS='\n' read -r -d '' tmp || true;
	comments["${1}"]=$tmp
    else
	IFS='\n' read -r -d '' tmp || true;
	comments["${2}_${1}"]=$tmp
    fi
}

function get_translation()
{
    origin=${comments["$1"]}
    locale=${comments["$1$lang"]}
    if [ "$locale" ]; then
	echo "$locale"
    else
	echo "$origin"
    fi
}

function render_code()
{
    echo \`\`\`
    echo "$(declare -f $1)"
    echo \`\`\`
}

function base_markdown()
{
    COMMENT md_check <<EOF
How to check?
EOF
    COMMENT zh md_check <<EOF
如何检查?
EOF

    COMMENT md_fix <<EOF
How to fix?
EOF
    COMMENT zh md_fix <<EOF
如何修复?
EOF

    title=$(head -n1 <<EOF
$(get_translation description)
EOF
)

cat <<EOF
# $title

$(get_translation description)

# $(get_translation md_check)

$(get_translation check)

$([[ $verbose ]] && render_code check)

# $(get_translation md_fix)

$(get_translation fix)

$([[ $verbose ]] && render_code fix)
EOF
}

function base_help()
{
    cat <<EOF
Options:
    -f|--fix) dry run the fix function.
    -c|--check) dry run the check function.
    -d|--description) print the description of the problem.
    -t|--title) show the title of the problem.
    -l|--lang) specify display language.
    -m|--meta) print the meta data in json format.
    -v|--verbose) show code in render document.
    -h|--help) print this usage.
    --force) force run the function.
    --verify) just echo "verified fixme script" for fixme verifying validation of the script

    Use fixme client instead use this script directly.

    The default action is print the document of
    the problem in markdown foramt.

    ./fix -v > README.md
    LANG=zh_CN ./fix -v > README.zh.md
EOF
    exit 1
}
