#ident "@(#)RELEASE VERSION html request form: version 5.2 by psm@leadbelly.lanl.gov compiled on 3/26/97"
#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <dirent.h>
#include <ctype.h>

main(argc, argv)
int argc;
char *argv[];
{
char name[8000],output[8000],timeres[81],units[81],compression[81],letters[10],satname[20];
char outfile[81],mailname[121],echoname[181],mailname2[121],mailname3[121], rmoutfile[81];
char dummy[81],compress[81];
int count,i,j,k,year,month,day;
double start,stop;
long lhex;
char hexstr[3],chr1,chr2,*end;
FILE *outfi;
char rcsid[] =  "$Id: html request form: version 5.2 by psm@leadbelly.lanl.gov compiled on 3/26/97 $";
time_t *tloc;
struct timeb *tp;
int t2;
short int error, namerr, emailerr, phonerr, adderr,usedforerr, timerr, colonerr, daterr, datelttr, datecolon,energyerr,yearerr, montherr, dayerr, starterr, stoperr;

/*initialize*/
adderr=0;error=0;namerr=0;emailerr=0;phonerr=0;timerr=0;colonerr=0;daterr=0;datelttr=0;datecolon=0;energyerr=0;usedforerr=0; yearerr=0; montherr=0; dayerr=0; starterr=0; stoperr=0;

t2=time(NULL);
/*
sprintf(outfile,"/belly1/www/htdocs/psm/request/%d",t2);
*/

sprintf(rmoutfile,"rm /belly1/home/lanl_geo/newrequests/%d",t2);


sprintf(outfile,"/belly1/home/lanl_geo/newrequests/%d",t2);

sprintf(mailname,"cat /belly1/home/lanl_geo/newrequests/%d|/usr/ucb/mail -s NewRequestCalled:%d reeves",t2,t2);
sprintf(mailname2,"cat /belly1/home/lanl_geo/newrequests/%d|/usr/ucb/mail -s NewRequestCalled:%d rdbelian@lanl.gov",t2,t2);
sprintf(mailname3,"cat /belly1/home/lanl_geo/newrequests/%d|/usr/ucb/mail -s NewRequestCalled:%d psm",t2,t2);

if ((outfi=fopen(outfile,"w"))==NULL)
{
	printf("cannot open file\n"); exit(1);
}

fscanf(stdin, "%s",name);
/*
fprintf(outfi,"%s\n\n",name);
*/
fprintf(outfi,"*** Name ***\n");
for(count=0;;count++) if (name[count]=='=') break;
count++;
fprintf(outfi," ");
if (name[count]=='&') {error=1; namerr=1;}
for(;;count++) 
{
  if (name[count]=='%')
    {
	sprintf(hexstr,"%c%c",name[++count],name[++count]);
	lhex=strtol(hexstr,&end,16);
	if (lhex==13) {count+=3; fprintf(outfi,"%c%c ",lhex,10);} else fprintf(outfi,"%c",lhex);
    }
  else if (name[count]=='&') break;
  else 
	{
	if (name[count]=='+') name[count]=' ';
	fprintf(outfi,"%c",name[count]);
	}
}
fprintf(outfi,"\n\n");

fprintf(outfi,"*** E-mail Address ***\n");
for(;;count++)if (name[count]=='=') break;
count++;
fprintf(outfi," ");
if (name[count]=='&') {error=1; emailerr=1;}
for(;;count++) 
{
  if (name[count]=='%')
    {
	sprintf(hexstr,"%c%c",name[++count],name[++count]);
	lhex=strtol(hexstr,&end,16);
	if (lhex==13) {count+=3; fprintf(outfi,"%c%c ",lhex,10);} else fprintf(outfi,"%c",lhex);
    }
  else if (name[count]=='&') break;
  else 
	{
	if (name[count]=='+') name[count]=' ';
	fprintf(outfi,"%c",name[count]);
	}
}
fprintf(outfi,"\n\n");


fprintf(outfi,"*** Postal Address ***\n");
for(j=0;j<5;j++)
{
for(;;count++)if (name[count]=='=') break;
count++;
fprintf(outfi," ");
for(;;count++) 
{
  if (name[count]=='%')
    {
	sprintf(hexstr,"%c%c",name[++count],name[++count]);
	lhex=strtol(hexstr,&end,16);
	if (lhex==13) {count+=3; fprintf(outfi,"%c%c ",lhex,10);} else fprintf(outfi,"%c",lhex);
    }
  else if (name[count]=='&') break;
  else 
	{
	if (name[count]=='+') name[count]=' ';
	fprintf(outfi,"%c",name[count]);
	}
}
fprintf(outfi,"\n");
}
fprintf(outfi,"\n\n");


fprintf(outfi,"*** Phone Number ***\n");
for(;;count++)if (name[count]=='=') break;
count++;
fprintf(outfi," ");
if (name[count]=='&') {error=1; phonerr=1;}
for(;;count++) 
{
  if (name[count]=='%')
    {
	sprintf(hexstr,"%c%c",name[++count],name[++count]);
	lhex=strtol(hexstr,&end,16);
	if (lhex==13) {count+=3; fprintf(outfi,"%c%c ",lhex,10);} else fprintf(outfi,"%c",lhex);
    }
  else if (name[count]=='&') break;
  else
	{
	if (name[count]=='+') name[count]=' ';
	fprintf(outfi,"%c",name[count]);
	}
}
fprintf(outfi,"\n\n");

fprintf(outfi,"*** What the data will be used for ***\n");
for(;;count++)if (name[count]=='=') break;
count++;
fprintf(outfi," ");
if (name[count]=='&') {error=1; usedforerr=1;}
for(;;count++) 
{
  if (name[count]=='%')
    {
	sprintf(hexstr,"%c%c",name[++count],name[++count]);
	lhex=strtol(hexstr,&end,16);
	if (lhex==13) {count+=3; fprintf(outfi,"%c%c ",lhex,10);} else fprintf(outfi,"%c",lhex);
    }
  else if (name[count]=='&') break;
  else 
	{
	if (name[count]=='+') name[count]=' ';
	fprintf(outfi,"%c",name[count]);
	}
}
fprintf(outfi,"\n\n");

fprintf(outfi,"*** Compression ***\n");
for(;;count++)if (name[count]=='=') break;
count++;
fprintf(outfi," ");
compression[0]=name[count];
for(;;count++) 
{
  if (name[count]=='&') break;
}

if (compression[0]=='u') {sprintf(compress,"");fprintf(outfi,"uncompressed\n");}
if (compression[0]=='g') {sprintf(compress,"gzipped,");fprintf(outfi,"gzipped\n");}
if (compression[0]=='z') {sprintf(compress,"zipped,");fprintf(outfi,"zipped\n");}

fprintf(outfi,"\n@\n\n***\n\n");


i=0;
count++;
sprintf(output,"\n");
if (name[count]=='s') {error=1; energyerr=1;}
for(;;count++) 
{
  if (name[count]=='s') break;
  else
    {
      letters[i]=name[count];
      count+=10;
      i++;
    }
}
letters[i]='\0';

for(;;count++)if (name[count]=='=') break;
count++;
i=0;
for(;;count++) 
{
  if (name[count]=='&') break;
  satname[i]=name[count];
  i++;
}
satname[i++]='\0';

fprintf(outfi,"*** Data is requested for the following satellites: %s ***\n",satname);

fprintf(outfi,"\n#cg: ***Grouped energy channels for CPA***\n");
fprintf(outfi,"%s\n",letters);

fprintf(outfi,"\n#sg: ***Grouped energy channels for SOPA ***\n");
fprintf(outfi,"%s\n\n",letters);


for(;;count++)if (name[count]=='=') break;
count++;
i=0;
sprintf(timeres,"\n");
if (name[count]=='&')  {error=1; timerr=1;}
for(;;count++) 
{
  if (name[count]=='&') break;
  if (name[count]=='%')
    {
	sprintf(hexstr,"%c%c",name[++count],name[count+1]);
	lhex=strtol(hexstr,&end,16);
	if (lhex==58) {error=1; colonerr=1;}
    }
  timeres[i]=name[count];
  i++;
}
timeres[i++]='\0';
fprintf(outfi,"*** Time resolution=%s  seconds***\n\n",timeres);

for(;;count++)if (name[count]=='=') break;
count++;
i=0;
sprintf(units,"\n");
for(;;count++) 
{
  if (name[count]=='&') break;
  units[i]=name[count];
  i++;
}
units[i++]='\0';
fprintf(outfi,"***The data will be expressed as: %s  ***\n",units);

/*channel grouping hasn't been implemented in extract yet */
/*
for(;;count++)if (name[count]=='=') break;
count++;
i=0;
sprintf(output,"\n");
for(;;count++) 
{
  if (name[count]=='&') break;
  output[i]=name[count];
  i++;
}
output[i++]='\0';
fprintf(outfi,"***The grouping will be expressed as: %s channels ***\n",output);
*/
/*----------------------------------------------------------*/

fprintf(outfi,"\n***Requested days for data***");
fprintf(outfi,"\n        YEAR    MONTH   DAY       START         STOP\n");

for(;;count++)if (name[count]=='=') break;
if (name[count+1]=='&') {error=1; daterr=1;}
for(j=0;j<14;j++)/*full day loop*/
{
if (name[count+1]=='&') 
	{
	for(k=0;k<5;k++)
		{
		count++;
		for(;;count++)if (name[count]=='=') break; 
		}
	continue;
	}
fprintf(outfi,"$\n");
/*year*/
  {
    i=0;
    count++;
    for(;;count++) 
      {
	if (name[count]=='&') break;
	if (isalpha(name[count])) {datelttr=1; error=1;}
	if (name[count]=='+') name[count]='\0';
	if (name[count]=='%')
	  {
	    sprintf(hexstr,"%c%c",name[++count],name[count+1]);
	    lhex=strtol(hexstr,&end,16);
	    if (lhex==58) {error=1; datecolon=1;}
	  }
	output[i]=name[count];
	i++;
      }
    output[i++]='\0';
    year=atoi(output);
    if ((1975<year)&&(year<2200)) fprintf(outfi,"\t%d",year);
    else {error=1; yearerr=1;}
    for(;;count++)if (name[count]=='=') break;
  }
  /*month*/
  {
    i=0;
    count++;
    for(;;count++) 
      {
	if (name[count]=='&') break;
	if (isalpha(name[count])) {datelttr=1; error=1;}
	if (name[count]=='+') name[count]='\0';
	if (name[count]=='%')
	  {
	    sprintf(hexstr,"%c%c",name[++count],name[count+1]);
	    lhex=strtol(hexstr,&end,16);
	    if (lhex==58) {error=1; datecolon=1;}
	  }
	output[i]=name[count];
	i++;
      }
    output[i++]='\0';
    month=atoi(output);
    if ((0<month)&&(month<13)) fprintf(outfi,"\t%d",month);
    else {error=1; montherr=1;}
    for(;;count++)if (name[count]=='=') break;
  }
  /*day*/
  {
    i=0;
    count++;
    for(;;count++) 
      {
	if (name[count]=='&') break;
	if (isalpha(name[count])) {datelttr=1; error=1;}
	if (name[count]=='+') name[count]='\0';
	if (name[count]=='%')
	  {
	    sprintf(hexstr,"%c%c",name[++count],name[count+1]);
	    lhex=strtol(hexstr,&end,16);
	    if (lhex==58) {error=1; datecolon=1;}
	  }
	output[i]=name[count];
	i++;
      }
    output[i++]='\0';
    day=atoi(output);
    if ((0<day)&&(day<32)) fprintf(outfi,"\t%d",day);
    else {error=1; dayerr=1;}
    for(;;count++)if (name[count]=='=') break;
  }
  /*start*/
  {
    i=0;
    count++;
    for(;;count++) 
      {
	if (name[count]=='&') break;
	if (isalpha(name[count])) {datelttr=1; error=1;}
	if (name[count]=='+') name[count]='\0';
	if (name[count]=='%')
	  {
	    sprintf(hexstr,"%c%c",name[++count],name[count+1]);
	    lhex=strtol(hexstr,&end,16);
	    if (lhex==58) {error=1; datecolon=1;}
	  }
	output[i]=name[count];
	i++;
      }
    output[i++]='\0';
    start=atof(output);
    if ((0<=start)&&(start<=24.0)) fprintf(outfi,"\t%lf",start);
    else {error=1; starterr=1;}
    for(;;count++)if (name[count]=='=') break;
  }
  /*stop*/
  {
    i=0;
    count++;
    for(;;count++) 
      {
	if (name[count]=='&') break;
	if (isalpha(name[count])) {datelttr=1; error=1;}
	if (name[count]=='+') name[count]='\0';
	if (name[count]=='%')
	  {
	    sprintf(hexstr,"%c%c",name[++count],name[count+1]);
	    lhex=strtol(hexstr,&end,16);
	    if (lhex==58) {error=1; datecolon=1;}
	  }
	output[i]=name[count];
	i++;
      }
    output[i++]='\0';
    stop=atof(output);
    if ((0<=stop)&&(stop<=24.0)) fprintf(outfi,"\t%lf",stop);
    else {error=1; stoperr=1;}
    for(;;count++)if (name[count]=='=') break;
  }
fprintf(outfi,"\n");
}/*full day loop*/
fprintf(outfi,"+\n");
fprintf(outfi,"\n");
fprintf(outfi,".\n");

/*****************************************************************************************/
if (error==1)
{
system(rmoutfile);
printf("Content-Type: text/html%c%c ", 10, 10);
printf("<html>
<title>Error Detected</title>
<body>
<form method=POST action=\"http://leadbelly.lanl.gov/cgi-bin/psm/framereq\"><p>
<h3>  An error has been detected, please go back and edit the lines indicated by the <blink><font color=red>blinking message</font></blink>
<hr>
Background Information<BR>
Your Name:   ");
if (namerr) printf("<blink><font color=red>Name required</font></blink>");
else printf("sufficient");
printf("<BR>Your E-mail Address:   ");
if (emailerr) printf("<blink><font color=red>Email required</font></blink>");
else printf("sufficient");
printf("<BR>Your Postal Address:   not required");
printf("<BR>Your Phone Number:    ");
if (phonerr) printf("<blink><font color=red>Phone required</font></blink>");
else printf("sufficient");
printf("<BR>What will the data be used for?:   ");
if (usedforerr) printf("<blink><font color=red>Information Required</font></blink>");
else printf("sufficient");
printf("

<hr>

Energy Channels:   ");
if (energyerr) printf("<blink><font color=red>You must select an energy</font></blink>");
else printf("sufficient");
printf("<hr>Satellites:  %s",satname);
printf("
<hr>

Time Resolution:   ");
if (colonerr) printf("<blink><font color=red>You must enter a decimal time *without* a colon.</font></blink>");
else if (timerr) printf("<blink><font color=red>You must enter a decimal time.</font></blink>");
else printf("%s seconds",timeres);
printf("<hr>Units: %s",units);
printf("
<hr>

Dates and Times:   ");
if (daterr) printf("<blink><font color=red>Make sure you have enterred a full line of information.</font></blink>");
else if (datelttr)  printf("<blink><font color=red>All dates and times should be written as numbers.</font></blink>");
else if (datecolon)  printf("<blink><font color=red>You must enter a decimal time *without* a colon.</font></blink>");
else if (yearerr)  printf("<blink><font color=red>You must enter an integer year beteen 1975 and today.</font></blink>");
else if (montherr)  printf("<blink><font color=red>You must enter an integer month from  1 and 12.</font></blink>");
else if (dayerr)  printf("<blink><font color=red>You must enter an integer day from 1 to 31.</font></blink>");
else if (starterr)  printf("<blink><font color=red>You must enter a decimal start time from 0.0 to 24.0 </font></blink>");
else if (stoperr)  printf("<blink><font color=red>You must enter a decimal stop time from 0.0 to 24.0 </font></blink>");
else  printf("sufficient");
printf("<hr></h3>



</BODY>
</HTML>
");
}

else{

/* Write acceptance message back to the browser */
printf("Content-Type: text/html%c%c", 10, 10);

printf("<HTML>
<HEAD>
  <TITLE>Request Received</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=+1>Your request has been recorded. The computer is now attempting
to process your data request. The data will be in %s ascii, tab-delimited format
with a one line header. The data processing will probably be complete within
1 hour. You will be notified by e-mail when the data are ready and you
will receive instructions for downloading them. When available, your data
will be placed in an anonymous ftp directory on leadbelly.lanl.gov
called:/pub/%d
If you wish to use your web browser to access the data the url will be <A
HREF=\"ftp://leadbelly.lanl.gov/pub/%d \">ftp://leadbelly.lanl.gov/pub/%d </A></FONT></P>

<P><FONT SIZE=+1>If you have any questions or difficulties please
contact:</FONT></P>

<UL>
  <LI><FONT SIZE=+1>Richard D. Belian, <A
HREF=\"mailto:rbelian@lanl.gov\">rbelian@lanl.gov</A><BR>
  Principal Investigator for the CPA and SOPA instruments</FONT>
  <LI><FONT SIZE=+1>Geoff Reeves, <A
HREF=\"mailto:reeves@lanl.gov\">reeves@lanl.gov</A><BR>
  Co-I &amp; primary data analyst for the CPA and SOPA data</FONT>
  <LI><FONT SIZE=+1>Peter McLachlan, <A
HREF=\"mailto:psm@nis.lanl.gov\">psm@nis.lanl.gov</A><BR>
  Software designer for the LANL-EP data request system<BR>
  </FONT>
</UL>

<P><BR>
</P>

<H3 ALIGN=CENTER><HR SIZE=\"10\">[ <A HREF=\"/lanl_ep_data/lanl_ep.html\">EP
Home</A> | <A HREF=\"/lanl_ep_data/ep_request.html\">Requests</A>| <A HREF=
\"/lanl_ep_data/summary_plot_choose.html\">Browse</A> | <A
HREF=\"/lanl_ep_data/ep_projects.html\">Projects</A>
| <A HREF=\"/lanl_ep_data/ep_information.html\">Information</A> ]<HR SIZE=
\"10\"></H3>

<P><A HREF=\"http://leadbelly.lanl.gov/reeves.html\"><IMG
SRC=\"/images/geoff_reeves.gif\"
WIDTH=\"106\" HEIGHT=\"34\" ALIGN=\"LEFT\" ALT=\"Geoff Reeves\" NATURALSIZEFLAG=
\"0\"></A> Produced and Maintains the LANL Energetic Particle Web Pages.<BR>
Last updated Wednesday March 5, 1997<BR CLEAR=\"ALL\">
</BODY>
</HTML>

",compress,t2,t2,t2);


};

fclose(outfi);
if (error != 1)
{

system(mailname);
system(mailname2);
system(mailname3);

sprintf(echoname,"echo>/belly1/home/lanl_geo/support/queue/%d",t2);
system(echoname);
}

/*<FORM ACTION="default.cgi" ENCTYPE="x-www-form-encoded" METHOD="POST">*/
}