##
# systrunk <command> [<options>, ...]
#
# A version control system powered by rsync, focused on tracking large
# directory trees with large files, even binary files; system images.
#
# Main function, decide what to do ...
##
function main
{
    if [ $# -eq 0 ]; then
        echo "Fatal: no command given" >&2
        exit 1
    fi

    cmd="$1"
    shift

    if [ ! -f ".systr/BASE" ]; then
        if [[ "$cmd" == "checkout" ]]; then
            systr_checkout "$@"
        elif [[ "$cmd" == "init" ]]; then
            systr_init "$@"
        else
            echo "Fatal: not in a worktree" >&2
            exit 1
        fi

        exit
    fi

    read path <.systr/path
    read BASE <.systr/BASE
    read TRAC <.systr/TRAC
    read MERG <.systr/MERG
    read updated <.systr/updated

    if [[ "$cmd" == "status" ]]; then
        systr_status "$@"
    elif [[ "$cmd" == "init" ]]; then
        systr_init "$@"
    elif [[ "$cmd" == "checkout" ]]; then
        systr_checkout "$@"
    elif [[ "$cmd" == "commit" ]]; then
        systr_commit "$@"
    elif [[ "$cmd" == "tag" ]]; then
        systr_repo_tag "$@"
    elif [[ "$cmd" == "shortlog" ]]; then
        systr_short_log "$@"
    elif [[ "$cmd" == "log" ]]; then
        systr_log "$@"
    else
        echo "Fatal: $cmd not recognized" >&2
        exit 1
    fi
}

main "$@"