ipmi_sdr_main(struct ipmi_intf *intf, int argc, char **argv)
{
	int rc = 0;

	/* initialize random numbers used later */
	srand(time(NULL));

	if (argc == 0)
		return ipmi_sdr_print_sdr(intf, 0xfe);
	else if (strncmp(argv[0], "help", 4) == 0) {
		printf_sdr_usage();
	} else if (strncmp(argv[0], "list", 4) == 0
		   || strncmp(argv[0], "elist", 5) == 0) {

		if (strncmp(argv[0], "elist", 5) == 0)
			sdr_extended = 1;
		else
			sdr_extended = 0;

		if (argc <= 1)
			rc = ipmi_sdr_print_sdr(intf, 0xfe);
		else if (strncmp(argv[1], "all", 3) == 0)
			rc = ipmi_sdr_print_sdr(intf, 0xff);
		else if (strncmp(argv[1], "full", 4) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_FULL_SENSOR);
		else if (strncmp(argv[1], "compact", 7) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_COMPACT_SENSOR);
		else if (strncmp(argv[1], "event", 5) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_EVENTONLY_SENSOR);
		else if (strncmp(argv[1], "mcloc", 5) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_MC_DEVICE_LOCATOR);
		else if (strncmp(argv[1], "fru", 3) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_FRU_DEVICE_LOCATOR);
		else if (strncmp(argv[1], "generic", 7) == 0)
			rc = ipmi_sdr_print_sdr(intf,
						SDR_RECORD_TYPE_GENERIC_DEVICE_LOCATOR);
		else if (strcmp(argv[1], "help") == 0) {
			lprintf(LOG_NOTICE,
				"usage: sdr %s [all|full|compact|event|mcloc|fru|generic]",
				argv[0]);
			return 0;
		}
		else {
			lprintf(LOG_ERR,
				"Invalid SDR %s command: %s",
				argv[0], argv[1]);
			lprintf(LOG_NOTICE,
				"usage: sdr %s [all|full|compact|event|mcloc|fru|generic]",
				argv[0]);
			return (-1);
		}
	} else if (strncmp(argv[0], "type", 4) == 0) {
		sdr_extended = 1;
		rc = ipmi_sdr_print_type(intf, argv[1]);
	} else if (strncmp(argv[0], "entity", 6) == 0) {
		sdr_extended = 1;
		rc = ipmi_sdr_print_entity(intf, argv[1]);
	} else if (strncmp(argv[0], "info", 4) == 0) {
		rc = ipmi_sdr_print_info(intf);
	} else if (strncmp(argv[0], "get", 3) == 0) {
		rc = ipmi_sdr_print_entry_byid(intf, argc - 1, &argv[1]);
	} else if (strncmp(argv[0], "dump", 4) == 0) {
		if (argc < 2) {
			lprintf(LOG_ERR, "Not enough parameters given.");
			lprintf(LOG_NOTICE, "usage: sdr dump <file>");
			return (-1);
		}
		rc = ipmi_sdr_dump_bin(intf, argv[1]);
	} else if (strncmp(argv[0], "fill", 4) == 0) {
		if (argc <= 1) {
			lprintf(LOG_ERR, "Not enough parameters given.");
			lprintf(LOG_NOTICE, "usage: sdr fill sensors");
			lprintf(LOG_NOTICE, "usage: sdr fill file <file>");
			lprintf(LOG_NOTICE, "usage: sdr fill range <range>");
			return (-1);
		} else if (strncmp(argv[1], "sensors", 7) == 0) {
			rc = ipmi_sdr_add_from_sensors(intf, 21);
		} else if (strncmp(argv[1], "nosat", 5) == 0) {
			rc = ipmi_sdr_add_from_sensors(intf, 0);
		} else if (strncmp(argv[1], "file", 4) == 0) {
			if (argc < 3) {
				lprintf(LOG_ERR,
					"Not enough parameters given.");
				lprintf(LOG_NOTICE,
					"usage: sdr fill file <file>");
				return (-1);
			}
			rc = ipmi_sdr_add_from_file(intf, argv[2]);
		} else if (strncmp(argv[1], "range", 4) == 0) {
			if (argc < 3) {
				lprintf(LOG_ERR,
					"Not enough parameters given.");
				lprintf(LOG_NOTICE,
					"usage: sdr fill range <range>");
				return (-1);
			}
			rc = ipmi_sdr_add_from_list(intf, argv[2]);
		} else {
		    lprintf(LOG_ERR,
			    "Invalid SDR %s command: %s",
			    argv[0], argv[1]);
		    lprintf(LOG_NOTICE,
			    "usage: sdr %s <sensors|nosat|file|range> [options]",
			    argv[0]);
		    return (-1);
		}
	} else {
		lprintf(LOG_ERR, "Invalid SDR command: %s", argv[0]);
		rc = -1;
	}

	return rc;
}