#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/crypt.h"

#define LICENSE_HD_INFO_LIST_SIZE 32

typedef struct _license_hd_info
{
	int	used_flag;
	unsigned char HDSerialNumber[256];
}license_hd_info;

typedef struct _license_hd_info_list
{
	char			  system_created_time[128+1];
	license_hd_info list[LICENSE_HD_INFO_LIST_SIZE];
}license_hd_info_list;

license_hd_info_list hd_info_list;
static unsigned char system_created_time[128+1];

static int GetSysCreatedTime()
{
	FILE *fpConf=NULL;
	int i,j=0,k,len;
	char created_time[128] = {0};
	char	buf[128] = {0};
	unsigned char created_time_des[128] = {0}, TmpStr[64]= {0};
	
	memset(created_time, 0, 64);
	//sprintf(buf,"tune2fs -l /dev/sda1 | grep create >../log/created_time.txt");
   	sprintf(buf,"ls -lact --full-time /etc | tail -1 | awk '{print $6,$7}' >../log/created_time.txt");
	system(buf);
	fpConf = fopen("../log/created_time.txt","r");
	if(fpConf != NULL)
	{
		len = fread(created_time,128,1,fpConf);
		//if(created_time[len-1] == '\r' || created_time[len-1] == '\n')
		//	 created_time[len - 2] = 0;
		fclose(fpConf);
	}
	else
		return 0;
	
	j = strlen(created_time);
	if (j == 0)
	{
		return 0;
	}
	
	k = (j + 7) / 8;
	memset(created_time + j, '0', 128 - j);
	
	memset(created_time_des, 0, 128);
	for (i = 0; i < k; i++)
	{
		memset(TmpStr, 0, 64);
		DesCrypt(TmpStr, (unsigned char *)created_time + 8 * i, NULL, 0);
		memcpy(created_time_des + 8 * i, TmpStr, 8);
	}
	system_created_time[0] = k*8;
	memcpy(&system_created_time[1], created_time_des, 8*k);
	
	return 1;
}

int ReadHDInfoList()
{
	char name[32]={"/etc/lhdlist"};
	FILE *fp = NULL;
	
	GetSysCreatedTime();

	fp = fopen(name, "r");
	if(fp == NULL)
		return 0;

	memset(&hd_info_list, 0x00, sizeof(license_hd_info_list));
	fread(&hd_info_list,sizeof(license_hd_info_list),1,fp);
	
	fclose(fp);
	return 1;
}

int SaveHDInfoList(unsigned char *new_hd_info)
{
	char name[32]={"/etc/lhdlist"};
	FILE *fp = NULL;
	int	i;
	license_hd_info *info_ptr = NULL;	
	
	
	for(i=0; i<LICENSE_HD_INFO_LIST_SIZE; i++)
	{
		info_ptr = &hd_info_list.list[i];

		if(info_ptr->used_flag == 1)
		{
			if(memcmp(info_ptr->HDSerialNumber, new_hd_info, 256) == 0)
				return 1;
		}
	} 

	fp = fopen(name, "w");
	if(fp == NULL || new_hd_info == NULL)
		return 0;

	memcpy(hd_info_list.system_created_time, system_created_time, 128+1);
	
	for(i=0; i<LICENSE_HD_INFO_LIST_SIZE; i++)
	{
		info_ptr = &hd_info_list.list[i];

		if(info_ptr->used_flag == 0)
		{
			info_ptr->used_flag = 1;
			memcpy(info_ptr->HDSerialNumber, new_hd_info, 256);
			break;
		}
	} 

	fwrite(&hd_info_list,sizeof(license_hd_info_list),1,fp);
	
	fclose(fp);
	return 1;
	
}

int CheckIfHDInfoBackup(unsigned char *license_info)
{
	int length;
	int i;
	license_hd_info *info_ptr = NULL;	

	length = system_created_time[0];
	if(memcmp(&system_created_time[1], &hd_info_list.system_created_time[1], length))
		return 0;
	
	for(i=0; i<LICENSE_HD_INFO_LIST_SIZE; i++)
	{
		if(hd_info_list.list[i].used_flag == 1)
		{
			info_ptr = &hd_info_list.list[i];
			length = info_ptr->HDSerialNumber[0];
			if (memcmp(&info_ptr->HDSerialNumber[1], license_info, length) == 0)
				return 1;
		}
	} 
	
	return 0;
}
