/*
 * Copyright 1990 by Baylor College of Medicine ALL RIGHTS RESERVED. 
 *
 * This program is subject to a license agreement between 
 * Baylor College of Medicine and MIT. Any use inconsistent with
 * said license and any use by persons other than the faculty, 
 * students and staff at MIT or any use on a computer not operated 
 * as part of the Athena Computing Environment (ACE) is expressly 
 * prohibited.
 */
/****************************************************************
 * File: license.c
 * Date: 05/14/91
 *
 * Description:
 *   This file contains functions for setting and checking the
 *   system date to allow applications to set expiration dates
 *   for executable programs.
 *
 * Revisions:
 ****************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <sys/types.h>
#include <time.h>

#include "LicenseP.h"

/****************************************************************
 *                Private Functions
 ****************************************************************/

PRIVATE
int
lic_check_date(lic)
	License *lic;
{
	time_t time();
	time_t clock = time((time_t *)0);
	struct tm *tm = localtime(&clock);
	int flag;

	if (lic->date_set == 0)
	{
		fprintf(stderr,"lic_check_date: Date has not been set\n");
		return 1;
	}

	/* we increment the month for comarison sine it is from 0 to 11 */
	++tm->tm_mon;

	printf("Checking expiration date:\n");
	printf("     Current Date: %d/%d/%d\n",tm->tm_mon,tm->tm_mday,tm->tm_year);
	printf("  Expiration Date: %d/%d/%d\n",lic->month,lic->day,lic->year);

	if (tm->tm_year < lic->year)
	{
		flag = 1;
	}
	else if (tm->tm_year == lic->year)
	{
		if (tm->tm_mon < lic->month)
		{
			flag = 1;
		}
		else if (tm->tm_mon == lic->month)
		{
			if (tm->tm_mday < lic->day)
			{
				flag = 1;
			}
			else if (tm->tm_mday == lic->day)
			{
				flag = 1;
			}
			else
			{
				flag = 0;
			}
		}
		else
		{
			flag = 0;
		}
	}
	else
	{
		flag = 0;
	}

	return flag;
}

PRIVATE
int
lic_check_host(lic)
	License *lic;
{
	long host_id;

	host_id = gethostid();

	if (lic->host_id == host_id)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/****************************************************************
 *                Public Functions
 ****************************************************************/

/****************************************************************
 * Function: lic_init
 * Date: 05/14/91
 *
 * Description:
 *   This function initializes the license library and allocates
 *   the license structure and returns this to the application.
 *
 * Linkage: License *lic_init(type)
 *   int type - type of license to be inforced
 *
 * Revisions:
 ****************************************************************/
PUBLIC
License *
lic_init(type)
	int type;
{
	License *lic;

	if ((type != LIC_EXPIRATION_DATE) &&
		(type != LIC_MACHINE_SPECIFIC) )
	{
		fprintf(stderr,"lic_initialize: Error: Bad type %d\n",type);
		exit(-1);
	}

	lic = (License *)malloc(sizeof(License));
	if (lic == NULL)
	{
		fprintf(stderr,"lic_initialize: Malloc failed\n");
		exit(-1);
	}

	lic->type = type;
	lic->date_set = 0;
	lic->month = 0;
	lic->day = 0;
	lic->year = 0;
	lic->host_id = 0;

	return lic;
}

/****************************************************************
 * Function: lic_expire
 * Date: 05/14/91
 *
 * Description:
 *   This function sets the expiration date that is used for
 *   checking that the license is correct.
 *
 * Linkage: void lic_expire(lic,month,day,year)
 *   License *lic - pointer to license info
 *   int month    - number of the month (1 to 12)
 *   int day      - day of the month (1 to 31)
 *   int year     - four digit year
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
lic_expire(lic,month,day,year)
	License *lic;
	int month;
	int day;
	int year;
{
	if (lic == NULL)
	{
		fprintf(stderr,"lic_set_date: License is NULL. Can't set date\n");
		return;
	}

	lic->date_set = 1;
	lic->month = month;   /* ? */
	lic->day = day;
	lic->year = year;
}

/****************************************************************
 * Function: lic_host
 * Date: 05/14/91
 *
 * Description:
 *   This function specifies a machine specific license.
 *
 * Linkage: void lic_host(lic,host_id)
 *   License *lic - pointer to license info
 *   long host_id - host id for specific machine license
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
lic_host(lic,host_id)
	License *lic;
	long host_id;
{
	if (lic == NULL)
	{
		fprintf(stderr,"lic_host: License is NULL\n");
		return;
	}

	lic->host_id = host_id;
}

/****************************************************************
 * Function: lic_check
 * Date: 05/14/91
 *
 * Description:
 *   This function checks to see if the license is valid or not.
 *   This function can be called as many times as desired within
 *   a given applicaton.
 *
 * Linkage: int lic_check(lic)
 *   License *lic - pointer to license info
 *
 * Returns:
 *   1 - If the license is still valid
 *   0 - If the license is no longer valid (expired)
 *
 * Revisions:
 ****************************************************************/
PUBLIC
int
lic_check(lic)
	License *lic;
{
	if (lic == NULL)
	{
		fprintf(stderr,"lic_check: License is NULL\n");
		return 1;
	}

	if (lic->type == LIC_EXPIRATION_DATE)
	{
		return lic_check_date(lic);
	}
	else
	{
		return lic_check_host(lic);
	}
}

/****************************************************************
 * Function: lic_revoke
 * Date: 05/14/91
 *
 * Description:
 *   This function "revoke" the target license and frees the memory.
 *
 * Linkage: void lic_revoke(lic)
 *   License *lic - pointer to license info
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
lic_revoke(lic)
	License *lic;
{
	free((char *)lic);
}

/****************************************************************
 * Function: lic_print
 * Date: 05/14/91
 *
 * Description:
 *   This function prints out the license info to the specified
 *   file pointer. This function also call the lic_check function
 *   to determine if the license is still valid or not and includes
 *   this information in the printout.
 *
 * Linkage: void lic_print(lic,fp)
 *   License *lic - pointer to license info
 *   FILE *fp     - file pointer
 *
 * Revisions:
 ****************************************************************/
PUBLIC
void
lic_print(lic,fp)
	License *lic;
	FILE *fp;
{
	if (lic_check(lic))
	{
		fprintf(fp,"The license is still valid\n");
	}
	else
	{
		fprintf(fp,"The license has expired!!!!\n");
	}
}

#ifdef TEST
main()
{
	License *lic;

	lic = lic_init(LIC_EXPIRATION_DATE);

	lic_expire(lic,5,1,91);

	lic_print(lic);
}
#endif
