main() {
    ret=0
    cmd=""
    # variable to indicate whether it's a modification command
    modified=0

    if [ -z "$1" ]; then
        printf "No command specified\n\n"
        usage
        exit 1
    fi

    case "$1" in
        "new"|"n" )
            cmd="new_note"
            modified=1
            ;;
        "ls" )
            cmd="ls_notes"
            ;;
        "search"|"s" )
            cmd="search_filenames_and_contents"
            ;;
        "find"|"f" )
            cmd="find_notes"
            ;;
        "grep"|"g" )
            cmd="grep_notes"
            ;;
        "realpath" )
            cmd="realpath"
            ;;
        --help | -help | -h )
            cmd="usage"
            ;;
        --version | -version )
            cmd="version"
            ;;
        * )
            printf "%s\n" "$1 is not a recognized $(basename "$0") command."
            cmd="usage"
            ret=1
            ;;
    esac
    shift

    $cmd "$@"
    ret="$?"

    # run POST_COMMAND hook when modification cmd succeeds
    if [ "$ret" -eq 0 ] && [ "$modified" -eq 1 ] && [ -n "$POST_COMMAND" ]; then
        eval "$POST_COMMAND"
    fi

    exit "$ret"
}

main "$@"
