#include <stdio.h>

#include "arg.h"
#include "common.h"
#include "crd_ut10xx.h"

int
main(int argc, const char **argv)
{
	const char *help = 
		"\n\trbg [-help] [-devnum=num] [-devname=name] [-octets=num]\n\n"
		"\t-help\n"
		"\t\tPrint this information.\n\n"
		"\t-devnum\n"
		"\t\tSpecifies number of device which should be initialised.\n"
		"\t\tNumbers of devices can be foun calling info program.\n"
		"\t\tDefault value of this number is equal 0 and correspond to\n"
		"\t\tthe first device attached to host.\n\n"
		"\t-devname\n"
		"\t\tDevice can be specified by 'name' (only if configured).\n"
		"\t\tIf user defines also -devnum, then this option is ignored.\n\n"
		"\t-octets\n"
		"\t\tSpecifies number of random bit octets which should be\n"
		"\t\tgenerated by device. Default value of generated octets\n"
		"\t\tis 20.\n\n";
	
	CRD_UT10XX_t *ut;
	
	unsigned char buf[120];
	
	int devnum = -1;
	const char *devname = 0;
	const char *outfile = 0;
	int octets = 0;
	
	FILE *fd = stdout;
	
	int generated = 0;
	int status;
	
	int i;
	int j;
	
	if ( prologue(argv, help) )
		return (-1);
	
	if ( arg_getall(argv, "issi", "-devnum", &devnum, "-devname", &devname, 
		"-outfile", &outfile, "-octets", &octets) < 0 )
		return (-2);
	
	if ((devname == 0) && (devnum < 0))
		devnum = 0;
	
	if (devnum >= 0)
	{
		if ( (ut = CRD_UT10XX_find_by_index(devnum)) == 0 )
		{
			epilogue(stderr, "there is no device of that number, use 'info'"
				" to check\n", 0, 0);
			return (-3);
		}
	}
	else
	{
		if ( (ut = CRD_UT10XX_find_by_name(devname)) == 0 )
		{
			epilogue(stderr, "there is no device of this name, use 'info'"
				" to check\n", 0, 0);
			return (-4);
		}
	}
	
	if (outfile)
	{
		fd = fopen(outfile, "a+b");
		
		if (fd == 0)
			fd = stdout;
	}
	
	CRD_UT10XX_session_open(ut);
	
	while (octets > 0)
	{
		if ( (status = CRD_UT10XX_cmd_rbg_generate_bits(ut, buf, octets > 120 ? 
			120 : octets)) < 0 )
		{
			epilogue(stderr, 0, status, ut);
			return (status);
		}
		
		generated += 8 * (octets > 120 ? 120 : octets);
		printf("\r%10d bits generated", generated);
		fflush(stdout);
		
		for (i = 0; i < (octets > 120 ? 120 : octets); i++)
		{
			for (j = 0; j < 8; j++)
			{
				fprintf(fd, "%d", (buf[i] >> (7 - j)) & 0x01);
			}
		}

		fflush(fd);

		octets -= 120;
	}
	
	printf("\n");
	
	CRD_UT10XX_session_close(ut);
	
	if (fd != stdout)
		fclose(fd);
	
	return (epilogue(stderr, 0, 0, 0));
}
