int main(int argc, char *argv[])
{
	int ret = 0;
	int cast_vote = 1, master_wins = 0;
	int pollcount=0, polltime=1, quiet=0, once=0;
	int send_old_ringid = 0;
	int err;
	int opt;
	votequorum_callbacks_t callbacks;
	const char *devicename = "QDEVICE";
	const char *options = "F:n:p:t:cmq1h";

	memset(&callbacks, 0, sizeof(callbacks));
	callbacks.votequorum_notify_fn = votequorum_notification_fn;

	while ((opt = getopt(argc, argv, options)) != -1) {
		switch (opt) {
		case 'm':
		        master_wins = 1;
			break;
		case 'c':
		        cast_vote = 1;
			break;
		case '1':
		        once = 1;
			break;
		case 'q':
		        quiet = 1;
			break;
		case 'F':
		        send_old_ringid = atoi(optarg)+1;
			break;
		case 'p':
		        pollcount = atoi(optarg)+1;
			break;
		case 'n':
		        devicename = strdup(optarg);
			break;
		case 't':
		        polltime = atoi(optarg);
			break;
		case 'h':
		        usage(argv[0]);
			exit(0);
		}
	}

	if ( (err=votequorum_initialize(&handle, &callbacks)) != CS_OK) {
		fprintf(stderr, "votequorum_initialize FAILED: %d\n", err);
		return -1;
	}

	if (quiet && once) {
	        fprintf(stderr, "setting both -q (quet) and -1 (once) makes no sense\n");
		usage(argv[0]);
		exit(1);
	}

	if (!quiet) {
	        print_info(1);
	}
	if (once) {
	        exit(0);
	}

	if (argc >= 2) {
		if ( (err = votequorum_trackstart(handle, handle, CS_TRACK_CHANGES)) != CS_OK) {
			fprintf(stderr, "votequorum_trackstart FAILED: %d\n", err);
			ret = -1;
			goto out;
		}

		if ( (err=votequorum_qdevice_register(handle, devicename)) != CS_OK) {
			fprintf(stderr, "qdevice_register FAILED: %d\n", err);
			ret = -1;
			goto out;
		}

		if ( (err=votequorum_qdevice_master_wins(handle, devicename, master_wins)) != CS_OK) {
			fprintf(stderr, "qdevice_master_wins FAILED: %d\n", err);
			ret = -1;
			goto out;
		}


		while (--pollcount) {
			if (votequorum_dispatch(handle, CS_DISPATCH_ALL) != CS_OK) {
				fprintf(stderr, "votequorum_dispatch error\n");
                                ret = -1;
                                goto out;
                        }

		        if (!quiet) print_info(0);

			if (no_sent_old_ringid + 1 >= send_old_ringid) {
				/*
				 * Finally send correct ringid
				 */
				memcpy(&ring_id_to_send, &last_received_ring_id, sizeof(ring_id_to_send));
			} else {
				no_sent_old_ringid++;
			}

			if ((err=votequorum_qdevice_poll(handle, devicename, cast_vote, ring_id_to_send)) != CS_OK &&
			     err != CS_ERR_MESSAGE_ERROR) {
				fprintf(stderr, "qdevice poll FAILED: %d\n", err);
				ret = -1;
				goto out;
			}
			if (err == CS_ERR_MESSAGE_ERROR) {
				fprintf(stderr, "qdevice poll passed OLD ring_id\n");
			}

			if (!quiet) print_info(0);
			sleep(polltime);
		}
		if ((err= votequorum_qdevice_unregister(handle, devicename)) != CS_OK) {
			fprintf(stderr, "qdevice unregister FAILED: %d\n", err);
			ret = -1;
			goto out;
		}
	}

        if (!quiet) print_info(1);

out:
	votequorum_finalize(handle);
	return ret;
}
int main(int argc, char *argv[])
{
	int ret = 0;
	int cast_vote = 1, master_wins = 0;
	int pollcount=0, polltime=1, quiet=0, once=0;
	int err;
	int opt;
	const char *devicename = "QDEVICE";
	const char *options = "n:p:t:cmq1h";
	
	while ((opt = getopt(argc, argv, options)) != -1) {
		switch (opt) {
		case 'm':
		        master_wins = 1;
			break;
		case 'c':
		        cast_vote = 1;
			break;
		case '1':
		        once = 1;
			break;
		case 'q':
		        quiet = 1;
			break;
		case 'p':
		        pollcount = atoi(optarg)+1;
			break;
		case 'n':
		        devicename = strdup(optarg);
			break;
		case 't':
		        polltime = atoi(optarg);
			break;
		case 'h':
		        usage(argv[0]);
			exit(0);
		}
	}

	if ( (err=votequorum_initialize(&handle, NULL)) != CS_OK) {
		fprintf(stderr, "votequorum_initialize FAILED: %d\n", err);
		return -1;
	}

	if (quiet && once) {
	        fprintf(stderr, "setting both -q (quet) and -1 (once) makes no sense\n");
		usage(argv[0]);
		exit(1);
	}

	if (!quiet) {
	        print_info(1);
	}
	if (once) {
	        exit(0);
	}

	if (argc >= 2) {
		if ( (err=votequorum_qdevice_register(handle, devicename)) != CS_OK) {
			fprintf(stderr, "qdevice_register FAILED: %d\n", err);
			ret = -1;
			goto out;
		}

		if ( (err=votequorum_qdevice_master_wins(handle, devicename, master_wins)) != CS_OK) {
			fprintf(stderr, "qdevice_master_wins FAILED: %d\n", err);
			ret = -1;
			goto out;
		}

		while (--pollcount) {
		        if (!quiet) print_info(0);
			if ((err=votequorum_qdevice_poll(handle, devicename, cast_vote)) != CS_OK) {
				fprintf(stderr, "qdevice poll FAILED: %d\n", err);
				ret = -1;
				goto out;
			}
			if (!quiet) print_info(0);
			sleep(polltime);
		}
		if ((err= votequorum_qdevice_unregister(handle, devicename)) != CS_OK) {
			fprintf(stderr, "qdevice unregister FAILED: %d\n", err);
			ret = -1;
			goto out;
		}
	}

        if (!quiet) print_info(1);

out:
	votequorum_finalize(handle);
	return ret;
}
int main(int argc, char *argv[])
{
	int ret = 0;
	int cast_vote = 1, master_wins = 1;
	int pollcount=0, polltime=1;
	int err;

	if ( (err=votequorum_initialize(&handle, NULL)) != CS_OK) {
		fprintf(stderr, "votequorum_initialize FAILED: %d\n", err);
		return -1;
	}

	print_info(1);

	if (argc >= 2 && atoi(argv[1])) {
		pollcount = atoi(argv[1]);
	}
	if (argc >= 3 && atoi(argv[2])) {
		polltime = atoi(argv[2]);
	}
	if (argc >= 4) {
		cast_vote = atoi(argv[3]);
	}
	if (argc >= 5) {
		master_wins = atoi(argv[4]);
	}

	if (argc >= 2) {
		if ( (err=votequorum_qdevice_register(handle, "QDEVICE")) != CS_OK) {
			fprintf(stderr, "qdevice_register FAILED: %d\n", err);
			ret = -1;
			goto out;
		}

		if ( (err=votequorum_qdevice_master_wins(handle, "QDEVICE", master_wins)) != CS_OK) {
			fprintf(stderr, "qdevice_master_wins FAILED: %d\n", err);
			ret = -1;
			goto out;
		}

		while (pollcount--) {
			print_info(0);
			if ((err=votequorum_qdevice_poll(handle, "QDEVICE", cast_vote)) != CS_OK) {
				fprintf(stderr, "qdevice poll FAILED: %d\n", err);
				ret = -1;
				goto out;
			}
			print_info(0);
			sleep(polltime);
		}
		if ((err= votequorum_qdevice_unregister(handle, "QDEVICE")) != CS_OK) {
			fprintf(stderr, "qdevice unregister FAILED: %d\n", err);
			ret = -1;
			goto out;
		}
	}
	print_info(1);

out:
	votequorum_finalize(handle);
	return ret;
}