#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "pcl.icom.h"

int pclname=0,pclcards=0,pcltty=4,pclmode=0,pcladdr=0,pclports=0,pcliono=0;
int pclirq=0,pclirqno=0;
int add[8],irq[8],tty[8]; 
void pclwel();

int main ()
{
 int i;
 char endch[10]={0};

 while ( !pclgetNTC() )
 {

   for(i=1;i<=pclcards;i++)
    { 
      printf("\nSetup NO %d %s Card Resouce\n\n",i,PCLNAME[pclname-1]);	
      if(!pclgetcard())
      pclscrip();
    }
   
   printf("\nDo you want to setup another card(n\\y):");
   scanf("%s",endch);
   for (i=0;i<10;i++){
     if (endch[i]=='y'||endch[i]=='y')
       break;
     else{	
        printf("Please Run pcl.icom\n");
	return(0);
     }
   }    	 
 }
}

void pclwel()
{
  printf ("\nADVANTECH PCL Setup Tool\n\n");

  printf ("Support Industrial Comunication Card Below:\n"
          "(1)PCL-740,(2)PCL-741,(3)PCL-734/735,(4)PCL-846/847,\n"
          "(5)PCL-746+,(6)PCL-849,(7)PCL-858\n\n");
}


int pclgetNTC()
{
 char pcls[10]={0};
 int tty;
pclwel();
/* card name input */

  do
    {
      printf ("Please select Card No([1-7]):");
      scanf ("%s", pcls);
      pclname = atonum (pcls);
    }
  while (pclname < 1 || pclname > 7);

  printf ("You Selected:%s\n\n", PCLNAME[pclname - 1]);




/* card number input */

do
    {
      printf ("Please input the number of %s cards([1-9]):",
              PCLNAME[pclname - 1]);
      scanf ("%s", pcls);
      pclcards = atonum (pcls);
    }
  while (pclcards < 1 || pclcards > 9);

  printf ("You have %d card(s)\n\n", pclcards);

/* ttyS device Name  input */

do
        {
          printf ("Input device file ttyS (dos COM) No(recomend:%d):",pcltty);
          scanf ("%s", pcls);
          pcltty = atonum (pcls);
        }
      while (pcltty < 1 || pcltty > 128);

      printf ("ttyS below will be used:\n");

      for (tty=pcltty;tty < (pcltty + pclcards*PCLPORTS[pclname - 1]); tty++)
        {
          printf ("ttyS%d ", tty);
        }
      printf("\n");
  	
return 0;
}

int pclgetcard()
{
 char pcls[10]={0};

 
/* PCL 846 746 849 Mode Setup */
 
 if ( pclname ==4 || pclname ==5 || pclname ==6) 
 {

 do
    {
     printf("Please input Mode setting ( (0)Standard (1)Enhanced):\n");
     scanf("%s",pcls);
     pclmode=atonum(pcls);
    }
  while (pclmode <0 || pclmode>1);
  
  if (pclmode==0)
	printf("Standard  Mode\n\n");
  else	
	printf("Enhanced Mode\n\n");
 } 
  
/* I/O IRQ setup */

 pcliono=PCLIO[pclname-1];

 if (pclmode ==0) 
 pclirqno=PCLIRQS[pclname-1];  /* standard mode */
 else 
 pclirqno=PCLIRQE[pclname-1];  /* enhanced mode */	


 for (pclports=0;pclports<PCLPORTS[ pclname - 1 ]; pclports++){
  /* io address */
    if ( pcliono-- >0 ){
      do 
        {
         printf("Please input I/O Address(0x200-0x3f0):");
         scanf("%s",pcls);
         pcladdr=atonum(pcls);	 	
         }
      while (pcladdr <0x200 || pcladdr >0x3ff);
      add[pclports]=pcladdr;
      printf("You use address:0x%x\n",add[pclports]);

    } else {
    pcladdr=pcladdr+8 ;
    add[pclports]=pcladdr ;  
    printf("Next port address used:0x%x\n",add[pclports]);
  
    }
 
 /* irq */
       
 if ( pclirqno-- >0 ){
      do
        {
         printf("Please input IRQ([3-15]):");
         scanf("%s",pcls);
         pclirq=atonum(pcls);
         }
      while (pclirq <3 || pclirq >15);
      irq[pclports]=pclirq;
      printf("You use irq:%d\n",irq[pclports]);

    } else {
    irq[pclports]=pclirq ;
    printf("Next port IRQ used:%x\n",irq[pclports]);

    }
 
  
 } 		  

return 0;
}


int pclscrip()
{
 int i=0;
 FILE *fp;
 
 fp =fopen("pcl.icom.isa","a");
 

 fprintf(fp, "#!/bin/sh\n");
 fprintf(fp, "#Setup ADVATECH PCL Industrial Communication Card %s\n",
	  PCLNAME[pclname-1]);
 

 for (i=0;i<PCLPORTS[pclname-1];i++){
 
 fprintf(fp,"setserial /dev/ttyS%d uart %s  port 0x%x irq %d\n", 
                       pcltty, PCLUART[pclname-1], add[i], 
		       irq[i]);	    
 fprintf(fp,"setserial /dev/ttyS%d\n",pcltty);
 pcltty++;            
 
 }
 fprintf(fp,"\n");
 chmod("./pcl.icom.isa",0755); 
 fclose(fp);     
 return 0;
}
