#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <dirent.h>

#define DEVICEPATH   "/dev"
#define COMNUMBER    "ttyS%d"
#define COMNAME      "ttyS"
#define RULESNAME    "mwusb2com.rules"
#define RULESPATH    "/etc/udev/rules.d"


int GetLastComNumber( void );
void WriteRulesFile( int ComNumber);
void CopyFile(void);
void RmTmpFile(void);

//-----------------------------------------------//

int GetLastComNumber(void)
{
	int comlist[64]={0};
	int i;
	DIR * dp;
	struct dirent *filename;
	char tmpName[6];

	dp = opendir(DEVICEPATH);
	if (!dp)
	{
		fprintf(stderr,"open directory error\n");
		return 0;
	}
	while (filename=readdir(dp)) // read file name
	{
		printf("filename:%-10s\td_info:%ld\t d_reclen:%us\n",
			   filename->d_name,filename->d_ino,filename->d_reclen);
		// is it ttyS#
		if ( 0 == strncmp( filename->d_name,
					COMNAME,
	 				4 )){
			// identify the ttyS# number
			int len;
			len = strlen ( filename->d_name);
			// ttyS#
			if ( len == 5 ){
			//printf(" COM is : %-10s \n", filename->d_name);	
				int num=0;
				num = atoi(filename->d_name+4);
				//printf("get the number : %d\n", num);
				comlist[num] = 1;
				
			}
			// ttyS##
			if ( len == 6){
				//printf(" COM is : %-10s \n", filename->d_name);	
				int num=0;
				num = atoi(filename->d_name+4);
				//printf("get the number : %d\n", num);
				if ( num <= 63)
					comlist[num] = 1;
			}
			
			
							
		}// if
	}
	closedir(dp);
	for ( i=0; i < 64; i++)
	{
		if ( comlist[i] != 1 )
			return i;
	}
	return 64;
	
	
}

//-----------------------------------------------//

void WriteRulesFile( int ComNumber)
{
	FILE *out;
	out = fopen( RULESNAME, "w");
	char lastCom[6];
	char rules[256];
	char fileHeader[]={"#*****************************************************************************\n"
					   "# \n"
					   "# Module Name:  mwusb2com rules \n"
					   "#\n"
					   "# Function: \n"
					   "#	udev rules for Megawin USB Virtual COM device \n"
					   "#\n"
					   "# Version:\n"
					   "#	0.0.0.1\n"
					   "#\n"
					   "# Copyright (c) 1999-2008 Megawin Technology Co., Ltd.\n"
					   "#\n"
					   "#\n"
					   "#\n"
					   "# place this file under /etc/udev/rules.d directory\n"
					   "#*****************************************************************************\n\n" };
					   
	fwrite( fileHeader, sizeof(char), strlen(fileHeader), out);
	sprintf(lastCom,COMNUMBER, ComNumber);
	//KERNEL=="ttyACM[0-9]*", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="0e6a",
	//ATTRS{idProduct}=="0316", MODE="666", SYMLINK+="mwusb2com"
	sprintf(rules,"KERNEL==\"ttyACM[0-64]*\", SUBSYSTEM==\"tty\", SUBSYSTEMS==\"usb\", ATTRS{idVendor}==\"0e6a\""
			", ATTRS{idProduct}==\"0316\", MODE=\"666\", SYMLINK+=\"%s\" ", lastCom);
	
	fwrite( rules, 1, strlen(rules), out);
			
			
	fclose(out);
	
						
	
	
}
//-----------------------------------------------//

void CopyFile(void)
{
	char cmd[50];
	
	sprintf(cmd, "cp %s %s", RULESNAME, RULESPATH);
	system(cmd);
	
	
}
//-----------------------------------------------//

void RmTmpFile(void)
{
	
	char cmd[50];
	sprintf(cmd, "rm %s", RULESNAME);
	system(cmd);	
	
}


//-----------------------------------------------//

int main(int argc,char *argv[])
{
	int lastcomnumber ;
	
	lastcomnumber = GetLastComNumber();
	
	printf("\n start 0 - 63, last ttyS## available is : %d\n", lastcomnumber);
	WriteRulesFile( lastcomnumber);
	CopyFile();
	RmTmpFile();
	
	return 0;
}