/* An Example application that uses a vendor defined checkout filter routine */
/* Modified from the SDK version of lmflex.c by: Amante4  1/23/00 */

#include <stdio.h>
#if (defined( __STDC__) || defined(_WINDOWS)) && !defined(apollo)
#include <stdlib.h>
#endif
#include <time.h>
#include "lmclient.h" 
#include "lm_code.h"
#include "lm_attr.h"
#define LICPATH "license.dat;."

#define FEATURE "f1"
LM_CODE(code, ENCRYPTION_SEED1, ENCRYPTION_SEED2, VENDOR_KEY1,
	VENDOR_KEY2, VENDOR_KEY3, VENDOR_KEY4, VENDOR_KEY5);

main()
{
  char feature[MAX_FEATURE_LEN+1];
  LM_HANDLE *lm_job;

	int my_filter( CONFIG *); // prototype for checkout filter routine


	if (lc_init((LM_HANDLE *)0, VENDOR_NAME, &code, &lm_job))
	{
		lc_perror(lm_job, "lc_init failed");
		exit(lc_get_errno(lm_job));
	}

	lc_set_attr(lm_job, LM_A_CHECKOUTFILTER,(LM_A_VAL_TYPE)my_filter); // Tell FlexLM I'm using a checkout filter

	printf("Enter feature to checkout [default: \"%s\"]: ", FEATURE);

	fgets(feature, MAX_FEATURE_LEN, stdin); 
	feature[strlen(feature)-1] = '\0'; /* truncate trailing newline */
	if (!*feature) strcpy(feature, FEATURE);


	lc_set_attr(lm_job, LM_A_LICENSE_DEFAULT, (LM_A_VAL_TYPE)LICPATH);

	if (lc_checkout(lm_job, feature, "1.0", 1, LM_CO_NOWAIT, &code, 
								LM_DUP_NONE))
	{
		printf("checkout failed... press return to exit...\n");
		getchar();
		lc_perror(lm_job, "checkout failed");
		lc_free_job(lm_job);
		exit(lc_get_errno(lm_job));
	}

	printf("%s checked out...press return to exit...", feature); 
/*
 *	Wait till user hits return
 *	getchar may be interrupted by SIGALRM, so we loop if necessary
 */
	getchar();
	lc_checkin(lm_job, feature ,0);

	lc_free_job(lm_job);
        return(0);
}

/* My vendor Checkout Filter routine */
int my_filter( CONFIG *input)
{
	char *vendor_string;
	vendor_string = input->lc_vendor_def; // get the vendor defined string from the structure (see config structure in lmclient.h)
	if (strcmp("Platform:NT", vendor_string)) { // see if it matches what we expect
		return (1); // fail if not equal
	}
	else {
		return (0); // success because the vendor string is OK
	}
}
