/*
 * Copyright (C) 2012 Jan Luebbe <j.luebbe@pengutronix.de>
 * Copyright (C) 2015 Marc Kleine-Budde <mkl@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <bootstate.h>
#include <command.h>
#include <common.h>
#include <getopt.h>
#include <malloc.h>
#include <stdio.h>

static int do_bootchooser(int argc, char *argv[])
{
	unsigned flags = 0, timeout = 0;
	char *name = NULL;
	int opt, ret;

	while ((opt = getopt(argc, argv, "kdrzvDRw::")) > 0) {
		switch (opt) {
		case 'k':
			flags |= BOOTCHOOSER_FLAG_ATTEMPTS_KEEP;
			break;
		case 'd':
			flags |= BOOTCHOOSER_FLAG_ATTEMPTS_DEC;
			break;
		case 'r':
			flags |= BOOTCHOOSER_FLAG_ATTEMPTS_RESET;
			break;
		case 'z':
			flags |= BOOTCHOOSER_FLAG_DEACTIVATE_ON_ZERO_ATTEMPTS;
			break;
		case 'v':
			flags |= BOOTCHOOSER_FLAG_VERBOSE;
			break;
		case 'D':
			flags |= BOOTCHOOSER_FLAG_DRYRUN;
			break;
		case 'R':
			flags |= BOOTCHOOSER_FLAG_RETRY_WITH_DEC;
			break;
		case 'w':
			if (optarg)
				timeout = simple_strtoul(optarg, NULL, 0);
			else
				flags |= BOOTCHOOSER_FLAG_WATCHDOG_TIMEOUT_FROM_STATE;
			flags |= BOOTCHOOSER_FLAG_WATCHDOG_ENABLE;
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (optind < argc)
		name = argv[optind];

	if (!(flags & (BOOTCHOOSER_FLAG_ATTEMPTS_KEEP |
		       BOOTCHOOSER_FLAG_ATTEMPTS_DEC |
		       BOOTCHOOSER_FLAG_ATTEMPTS_RESET))) {
		bootstate_info();
		return 0;
	}

	if ((flags & BOOTCHOOSER_FLAG_ATTEMPTS_KEEP) &&
	    (flags & (BOOTCHOOSER_FLAG_ATTEMPTS_DEC | BOOTCHOOSER_FLAG_ATTEMPTS_RESET)))
		return COMMAND_ERROR_USAGE;

	ret = bootstate_bootchooser(name, flags, timeout);

	return ret ? COMMAND_ERROR : COMMAND_SUCCESS;
}

BAREBOX_CMD_HELP_START(bootchooser)
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-k","keep - boot, don't modify attempts counter")
BAREBOX_CMD_HELP_OPT ("-d","decrement - boot, but decrement attempts counter by one")
BAREBOX_CMD_HELP_OPT ("-r","reset - boot, but reset _all_ attempts counter to default")
BAREBOX_CMD_HELP_OPT ("-z","deactivate choosen target in on zero remaining boot attemts")
BAREBOX_CMD_HELP_OPT ("-v","verbose output")
BAREBOX_CMD_HELP_OPT ("-D","dryrun. Do not boot - but handle watchdog and reset.")
BAREBOX_CMD_HELP_OPT ("-R","retry - boot, retry next boot target and decrement attempts")
BAREBOX_CMD_HELP_OPT ("-w","activate watchdog, use timeout specified in <BOOTSTATE>.watchdog_timeout")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(bootchooser)
	.cmd = do_bootchooser,
	BAREBOX_CMD_DESC("automatically select a boot target and boot")
	BAREBOX_CMD_OPTS("[-kdrzvDR] -w <TIMEOUT> [BOOTSTATE]")
	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
	BAREBOX_CMD_HELP(cmd_bootchooser_help)
BAREBOX_CMD_END
