/******************************************************/
/* Reads 0D, 1D and 2D files from the server          */
/* and puts them in the current directory.            */
/* D. Boucher Oct. 1994			              */
/******************************************************/ 
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <math.h>
#include "link.h"

struct link_handle l;
char stop[] = {'$','$','E','N','D','$','$'},dat[200],name[255],tok[255],pulse[255];
char curpath[500];
/*****************************************************************/
void debug()
{
if (errno>0) 
	{fprintf(stdout,"Error msg: %s \n",strerror(errno));
	errno=0;
	}
}
/*****************************************************************/
void skip()
{char c;
 do
	{c = fgetc(l.fpin);
	if(feof(l.fpin))break;
	}
 while(c != '\n' );
}
/*****************************************************************/
void output_all()
{char c[10];
 int i,u;
 while(!feof(l.fpin))
	{c[0] = fgetc(l.fpin);
	if((int)(c[0]) == 13) 
		{c[1] = fgetc(l.fpin);
		if((int)(c[1]) != 10)
			fprintf(stdout,"%c",c[0]);
		c[0]=c[1];
		}
	u=0;while(c[u] == stop[u] && !feof(l.fpin)){u++;if(u==7)break;c[u]=fgetc(l.fpin);}
	if(u==7)
		break;
	else
		for(i=0;i<=u;i++)fprintf(stdout,"%c",c[i]);
	}
}
/*****************************************************************/
void input_read(int dsp, char *string)
{char c;
 int lg,curr;

 lg = strlen(string);
 curr=0;
 do
	{c = fgetc(l.fpin);
	if(feof(l.fpin)) break;
	if(dsp)fprintf(stdout,"%c",c);
	while(1)
		{if(c == string[curr])
			{curr++;
			break;
			}
		else
			{if(curr==0)
				break;
			else
				curr=0;
			}
		}
	if(curr>=lg)break;
	}
 while(1);
}
/*****************************************************************/
void open_ftp()
{int u,num;
 char c,s[600];

 getwd(curpath);
 link_open(&l,"rsh","picard.iterus.org");
 input_read(1,"Password:");
 link_write(&l,gets(s)); input_read(1,"$ ");
 sprintf(s,"cd %s",curpath);
 link_write(&l,s); input_read(1,"$ ");
 link_write(&l,"ftp picard.iterus.org"); input_read(1,":");
 link_write(&l,"iter");
 input_read(1,"Password:");
 link_write(&l,"iter-eda");
 input_read(1,"ftp>");
 link_write(&l,"cd /pub/profile_data"); input_read(1,"ftp>");
 link_write(&l,"get tokamak_0d.dat");   input_read(1,"ftp>");
 sprintf(s,"cd %s/%s",tok,pulse);
 link_write(&l,s);               	input_read(1,"ftp>");
 link_write(&l,"prompt"); 		input_read(1,"ftp>");
 link_write(&l,"mget *"); 		input_read(1,"ftp>");
 link_write(&l,"quit");
 input_read(1,"$ ");
 link_write(&l,"exit");
 input_read(1,"$ ");
 link_write(&l,"exit");
}
/*****************************************************************/
void close_ftp()
{
 link_kill(&l);
}
/*****************************************************************/
void main(int argc,char *argv[])
{char *p,exp[250];

if(argc != 3)
	{printf("\n%s tokamak pulse_number\n",argv[0]);
	printf("Gets from the server the files corresponding to 'Tokamak' and \n\n");
	printf("'Pulse number' and puts them in the current directory.\n\n");
	exit(0);
	}
p=&argv[1][0];
while(*p)
	{if(*p==' ')
		{*p='\0';break;}
	p++;
	}
p=&argv[2][0];
while(*p)
	{if(*p==' ')
		{*p='\0';break;}
	p++;
	}
sprintf(name,"%s_%s",argv[1],argv[2]);
sprintf(tok,"%s",argv[1]);
sprintf(pulse,"%s",argv[2]);

	open_ftp();
	close_ftp();
}
