#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void
strtrm (char *string);

typedef struct sp_list
{
     char username[80];
     struct sp_list *next;
}SP_LIST;

SP_LIST *userids =0;
SP_LIST *wcard =0;
int id_count = 0;
int w_count = 0;

void print_body()
{
    puts(" var number = \"1234567890\";");
    puts("var i;");
    puts("var len = list.length;");
    puts("for(i =0; i<len; i++)");
    puts("{");
    puts("   if(list[i] == userid)");
    puts("     return 1;");
    puts("}");
    puts("len = anothercase.length;");
    puts("var d;");
    puts("for(i =0; i<len; i++)");
    puts("{");
    puts("    var index = userid.lastIndexOf(anothercase[i]);");
    puts("    if( index == 2)");
    puts("    {");
    puts("    for ( d = 3; d< userid.length; d++)");
    puts("        {");
    puts(" if(number.indexOf( userid.charAt(d)) == -1)");
    puts("           return 0;");
    puts("        }");
    puts("        return 1;");                   
    puts("    }");
    puts("} ");
    puts("return 0;");
    puts("}");
}

		 


void read_from_file()
{
     FILE *fp;
     char buffer[80];
     SP_LIST *userid_current =0, *userid_ptr;
     SP_LIST *wcard_current =0, *wcard_ptr;

     fp = fopen("../etc/special_logins.txt", "r");
     if(!fp)
     {
	 printf("no file");
	 exit(0);
     }
     fgets(buffer, 50, fp);
     while(fgets(buffer, 50, fp)) 
     {
         strtrm (buffer);
	 if(strlen(buffer)>0)
	 {
	   if(*buffer == '*')
	   {
             w_count++;
	     wcard_ptr = (SP_LIST*)malloc(sizeof(SP_LIST));
	     snprintf(wcard_ptr->username, 50, "%c", buffer[1]);
	     wcard_ptr->next = 0;
	     if(!wcard)
		   wcard = wcard_ptr;
	     else
		   wcard_current->next = wcard_ptr;
	     wcard_current = wcard_ptr;
	   }
	   else
	   {
             id_count++;
	     userid_ptr = (SP_LIST*)malloc(sizeof(SP_LIST));
	     strncpy(userid_ptr->username, buffer, 50);
	     userid_ptr->next = 0;
	     if(!userids)
		   userids = userid_ptr;
	     else
		   userid_current->next = userid_ptr;
	     userid_current = userid_ptr;
	  }
	 }
     }
     fclose(fp);
}
void send_list()
{
   SP_LIST *ptr;
   int i = 1;
    printf("function is_InList(userid)\n{\n");
    printf("var list = new Array (");
    
    for(ptr = userids; ptr; ptr = ptr->next)
    {
	 if (i  < id_count)
	   printf("\"%s\",", ptr->username);
	 else
	   printf("\"%s\"", ptr->username);
	 i++;
    }
    printf(");\n");

    printf("var anothercase = new Array (");
    i = 1;
    for(ptr = wcard; ptr; ptr = ptr->next)
    {
	 if (i < w_count)
	   printf("\"%s\",", ptr->username);
	 else
	   printf("\"%s\"", ptr->username);

	 i++;
    } 
    printf(");\n");
    print_body();
}
void free_sp_list(SP_LIST *list)
{
   SP_LIST *ptr;
  
   while(list)
   {
	ptr = list->next;
	free(list);
	list = ptr;
   } 

}

int main()
{
 
    printf("Content-type: text/javascript\n\n"); 
  read_from_file();
  send_list();
   free_sp_list(userids);
   free_sp_list(wcard);
 return 0;
 
}

void
strtrm (char *string)
{

  char *ptr;
  char *newstring;
  int len;

  len = strlen (string);
  newstring = (char *) malloc ((len + 1) * sizeof (char));
  if (!newstring)
      printf("malloc failed");

  for (ptr = string; *ptr && isspace (*ptr); ptr++);
  strncpy (newstring, ptr, len + 1);

  if (strlen (newstring))
    {
      ptr = strchr (newstring, '\0');
      for (ptr--; (ptr >= newstring) && isspace (*ptr); ptr--);
      *(ptr + 1) = '\0';
    }

  strncpy (string, newstring, len + 1);
  free (newstring);

}
