int update_binary_tool_main(int argc, char *argv[])
{
    int opt;

    static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {0, 0, 0, 0}
    };

    int long_index = 0;

    while ((opt = getopt_long(argc, argv, "h", long_options, &long_index)) != -1) {
        switch (opt) {
        case 'h':
            update_binary_tool_usage(0);
            return EXIT_SUCCESS;

        default:
            update_binary_tool_usage(1);
            return EXIT_FAILURE;
        }
    }

    if (argc - optind != 2) {
        update_binary_tool_usage(1);
        return EXIT_FAILURE;
    }

    // Log to stderr, so the output is ordered correctly in /tmp/recovery.log
    util::log_set_logger(std::make_shared<util::StdioLogger>(stderr));

    std::string action = argv[optind];
    std::string mountpoint = argv[optind + 1];

    if (action != ACTION_MOUNT
            && action != ACTION_UNMOUNT
            && action != ACTION_FORMAT) {
        update_binary_tool_usage(1);
        return EXIT_FAILURE;
    }

    if (mountpoint != SYSTEM
            && mountpoint != CACHE
            && mountpoint != DATA) {
        update_binary_tool_usage(1);
        return EXIT_FAILURE;
    }

    if (access("/.chroot", F_OK) < 0) {
        fprintf(stderr, "update-binary-tool must be run inside the chroot\n");
        return EXIT_FAILURE;
    }

    bool ret = false;

    if (action == ACTION_MOUNT) {
        ret = do_mount(mountpoint);
    } else if (action == ACTION_UNMOUNT) {
        ret = do_unmount(mountpoint);
    } else if (action == ACTION_FORMAT) {
        ret = do_format(mountpoint);
    }

    return ret ? EXIT_SUCCESS : EXIT_FAILURE;
}
int update_binary_tool_main(int argc, char *argv[])
{
    int opt;

    static const char *short_options = "h";

    static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {0, 0, 0, 0}
    };

    int long_index = 0;

    while ((opt = getopt_long(argc, argv, short_options,
                              long_options, &long_index)) != -1) {
        switch (opt) {
        case 'h':
            update_binary_tool_usage(stdout);
            return EXIT_SUCCESS;

        default:
            update_binary_tool_usage(stderr);
            return EXIT_FAILURE;
        }
    }

    if (argc - optind != 2) {
        update_binary_tool_usage(stderr);
        return EXIT_FAILURE;
    }

    // Log to stderr, so the output is ordered correctly in /tmp/recovery.log
    log::log_set_logger(std::make_shared<log::StdioLogger>(stderr, false));

    const char *action = argv[optind];
    const char *mountpoint = argv[optind + 1];

    bool is_valid_action = strcmp(action, ACTION_MOUNT) == 0
            || strcmp(action, ACTION_UNMOUNT) == 0
            || strcmp(action, ACTION_FORMAT) == 0;
    bool is_valid_mountpoint = strcmp(mountpoint, SYSTEM) == 0
            || strcmp(mountpoint, CACHE) == 0
            || strcmp(mountpoint, DATA) == 0;

    if (!is_valid_action || !is_valid_mountpoint) {
        update_binary_tool_usage(stderr);
        return EXIT_FAILURE;
    }

    if (access("/.chroot", F_OK) < 0) {
        fprintf(stderr, "update-binary-tool must be run inside the chroot\n");
        return EXIT_FAILURE;
    }

    bool ret = false;

    if (strcmp(action, ACTION_MOUNT) == 0) {
        ret = do_mount(mountpoint);
    } else if (strcmp(action, ACTION_UNMOUNT) == 0) {
        ret = do_unmount(mountpoint);
    } else if (strcmp(action, ACTION_FORMAT) == 0) {
        ret = do_format(mountpoint);
    }

    return ret ? EXIT_SUCCESS : EXIT_FAILURE;
}