#include <stdio.h>

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

int 
prologue(const char **argv, const char *help)
{
	int status;

	if ( (status = arg2bool(argv, "-help")) == 1 )
	{
		printf("%s", help);
		return (-1);
	}
	
	if ( (status = CRD_UT10XX_lib_init()) < 0 )
	{
		fprintf(stderr, "crd_ut10xx error: %s\n", CRD_UT10XX_lib_error_desc(status));
		return (-1);
	}
	
	if ( (status = CRD_UT10XX_lib_find_devices()) < 0 )
	{
		fprintf(stderr, "crd_ut10xx error: %s\n", CRD_UT10XX_lib_error_desc(status));
		CRD_UT10XX_lib_finish();
		return (-1);
	}
	
	if ( (status = CRD_UT10XX_lib_actualize_devices()) < 0 )
	{
		fprintf(stderr, "crd_ut10xx error: %s\n", CRD_UT10XX_lib_error_desc(status));
		CRD_UT10XX_lib_finish();
		return (-1);
	}
	
	return (0);
}

int 
epilogue(FILE *fd, const char *msg, int status, CRD_UT10XX_t *ut)
{
	if (fd == 0)
		fd = stderr;
	
	if (msg != 0)
		fprintf(fd, "%s", msg);
	
	if (status < 0)
		fprintf(fd, "crd_ut10xx error: %s\n\n", CRD_UT10XX_lib_error_desc(status));
		
	if (ut != 0)
		CRD_UT10XX_lib_detail_error_desc_to_file(ut, "\t%s\n", fd);
	
	fprintf(fd, "\n");
	
	if (status < 0)
	{
		CRD_UT10XX_lib_finish();
		return (status);
	}
	else
	{
		return (CRD_UT10XX_lib_finish());
	}
}
