#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
#include "citation.h"
#include "web-util.h"
#include "web-conf.h"

CitationPtr C;
char* emailAddress;
char *comments;

char* choice;

int error_flag = 0;

typedef struct {
  char *name;
  char *val;
} entry;

void trupdate();
void trupdate_htmlform();
void trpost_update();
void trpost_htmlform();
void trdelete_htmlform();
void tr_single_view_htmlform();
void tr_bulkload_htmlform();
void tr_bulkload();
void trpost_update_repeat();
void trpost_repeat();
void trpost_query();
void trdelete();
void tr_single_view();
void tr_browse();
void techrep_menu_htmlform();
int sendTechrepRequest(int methodID, char *tempFilename, char* trLocation);
void printConfirmation (int methodID);

main(int argc, char *argv[]) 
{
   entry entries[MAX_ENTRIES];
   register int x,m=0;
   int cl;

   char* cur_data;
   char* tr_task_data;
   int tr_task_miss = 0;

   tr_task_data = NULL;
   emailAddress = NULL;
   comments = NULL;
    
   /* print out the proper cgi-script heading: */
   printf ("Content-type: text/html%c%c",10,10);

   /* check for various form errors */
   if(strcmp(getenv("REQUEST_METHOD"),"POST")) {

      /* By default, just go to the main menu if something is screwy, the generated
         forms will take care of it from there */
      techrep_menu_htmlform();
      exit(1);
   }

   if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) {
      printf ("This script can only be used to decode form results. \n");
      exit(1);
   }

   /* use environment variable to determine how much data there is */
   cl = atoi(getenv("CONTENT_LENGTH"));
   
   /* put name-value pairs from form into an array for processing */
   for(x=0;cl && (!feof(stdin));x++) {
      m=x;
      entries[x].val = fmakeword(stdin,'&',&cl);
      plustospace(entries[x].val);
      unescape_url(entries[x].val);
      entries[x].name = makeword(entries[x].val,'=');
   }


   /* allocate Citation space */
   C = initCitation();
   if (!C) {
      printf ("Allocation problems.\n");
      exit (1);
   }

   /* read values from form; put into _data variables; set error flags*/
   for(x=0; x <= m; x++) {

      /* tr_task_data = the next form to go to. */
      if (!strcmp(entries[x].name,"tr_task_data")) {
         tr_task_data = strdup(entries[x].val);
         if (!strcmp(tr_task_data,""))  {
            /* The tr_task attribute was defined but not set in the invoking form.  Not Good, but we can deal. */
            tr_task_data = NULL;
            tr_task_miss = 1;
         }	
      }

      /* choice */
      else if (!strcmp(entries[x].name,"choice")) {
         choice = strdup(entries[x].val);
      }

      /* trnum */
      else if (!strcmp(entries[x].name,"trnum")) {
         cur_data = strdup(entries[x].val);
         stripTrailingBlanks (cur_data);
         if (!strcmp(cur_data,""))  {
            C->trId = NULL;
            error_flag = 1;
         } else {
            C->trId = cur_data;
         }
      }
      
      /* date */
      else if (!strcmp(entries[x].name,"date")) {
         cur_data = strdup(entries[x].val);
         stripTrailingBlanks (cur_data);
         if (!strcmp(cur_data,""))  {
            C->date = NULL;
            error_flag = 1;
         } else {
            C->date = cur_data;
         }
      }
      
      /* title */
      else if (!strcmp(entries[x].name,"title")) {
         cur_data = strdup(entries[x].val);
         stripTrailingBlanks (cur_data);
         if (!strcmp(cur_data,""))  {
            C->title = NULL;
            error_flag = 1;
         } else {
            C->title = cur_data;
         }
      }

      /* authors */	
      else if (!strcmp(entries[x].name,"authors")) {
         cur_data = strdup(entries[x].val);
         stripTrailingBlanks (cur_data);
         if (!strcmp(cur_data,""))  {
            C->author= NULL;
            error_flag = 1;
         } else {
            C->author = cur_data; 
         }
      }	

      /* url */
      else if (!strcmp(entries[x].name,"url")) {
         cur_data = strdup(entries[x].val);
         stripTrailingBlanks (cur_data);
         if (!strcmp(cur_data,""))  {
            C->url = NULL;
         } else {	
            C->url = cur_data; 
         }
      }

      /* keywords */
      else if (!strcmp(entries[x].name,"keywords")) {
         cur_data = strdup(entries[x].val);
         stripTrailingBlanks (cur_data);
         if (!strcmp(cur_data,""))  {
            C->keywords= NULL;
         } else {
            C->keywords = cur_data;
         }
      }
    
      /* categories */
      else if (!strcmp(entries[x].name,"categories")) {
         cur_data = strdup(entries[x].val);
         stripTrailingBlanks (cur_data);
         if (!strcmp(cur_data,""))  {
            C->categories = NULL;
         } else {	
            C->categories = cur_data;
         }
      }

      /* abstract */
      else if (!strcmp(entries[x].name,"abstract")) {
         cur_data = strdup(entries[x].val);
         stripBlankLines (cur_data);
         if (!strcmp(cur_data,""))  {
            C->abstract = NULL;
         } else {
            C->abstract = cur_data;
         }
      }

      /* filepath */
      else if (!strcmp(entries[x].name,"filepath")) {
         cur_data = strdup(entries[x].val);
         stripTrailingBlanks (cur_data);
         if (!strcmp(cur_data,""))  {
            C->filepath = NULL;
         } else {
            C->filepath = cur_data;
         }
      }

      /* organization */
      else if (!strcmp(entries[x].name,"organization")) {
         cur_data = strdup(entries[x].val);
         stripBlankLines (cur_data);
         if (!strcmp(cur_data,""))  {
            C->org = NULL;
         } else {
            C->org = cur_data;
         }
      }

      /* comments */
      else if (!strcmp(entries[x].name,"comments")) {
         cur_data = strdup(entries[x].val);
         stripBlankLines (cur_data);
         if (!strcmp(cur_data,""))  {
            comments = NULL;
         } else {
            comments = cur_data;
         }
      }

      /* email */
      else if (!strcmp(entries[x].name,"email")) {
         cur_data = strdup(entries[x].val);
         stripBlankLines (cur_data);
         if (!strcmp(cur_data,""))  {
            emailAddress = NULL;
            error_flag = 1;
         } else {
            emailAddress = cur_data;
         }
      }
   } /*end of for loop- finished going through form data */

   if( strcmp(tr_task_data,"trupdate")==0){
      trupdate();
   } else if ( strcmp(tr_task_data,"trupdate-htmlform")==0){
      trupdate_htmlform();
   } else if ( strcmp(tr_task_data,"trpost-update")==0){
      trpost_update();
   } else if ( strcmp(tr_task_data,"trpost-htmlform")==0){
      trpost_htmlform();
   } else if ( strcmp(tr_task_data,"trdelete-htmlform")==0){
      trdelete_htmlform();
   } else if ( strcmp(tr_task_data,"tr-single-view-htmlform")==0){
      tr_single_view_htmlform();
   } else if ( strcmp(tr_task_data,"tr-bulkload-htmlform")==0){
      tr_bulkload_htmlform();
   } else if ( strcmp(tr_task_data,"trpost-update-repeat")==0){
      trpost_update_repeat();
   } else if ( strcmp(tr_task_data,"trpost-repeat")==0){
      trpost_repeat();
   } else if ( strcmp(tr_task_data,"trpost-query")==0){
      trpost_query();
   } else if ( strcmp(tr_task_data,"tr-bulkload")==0){
      tr_bulkload();
   } else if ( strcmp(tr_task_data,"trdelete")==0){
      trdelete();
   } else if ( strcmp(tr_task_data,"tr-single-view")==0){
      tr_single_view();
   } else if ( strcmp(tr_task_data,"tr-browse")==0){
      tr_browse();
   } else if ( strcmp(tr_task_data,"techrep-menu-htmlform")==0){
      techrep_menu_htmlform();
   } else {
      /* If we can't tell where to go, just go to the main menu. */
      techrep_menu_htmlform();
   }
   exit(0);
}

void 
techrep_menu_htmlform() 
{

   printf ("<TITLE>Techrep</TITLE>\n");
   printf ("<H1>Techrep</H1>\n");
   
   printf ("<p><H2>Technical Report Bibliographic Utility</H2>\n");
   printf ("<hr>\n");

   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"trpost-htmlform\"> \n");
      
   printf ("<p>\n");
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Post a Document\">\n");
   printf ("<p>\n");
   printf ("</FORM>\n");
   
   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"trupdate-htmlform\"> \n");
   printf ("<p>\n");
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Update Bibliographic Information for a TR\">\n");
   printf ("<p>\n");
   printf ("</FORM>\n");

   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"trdelete-htmlform\"> \n");
   printf ("<p>\n");
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Delete Bibliographic Information from the Database\">\n");
   printf ("<p>\n");
   printf ("</FORM>\n");

   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"tr-single-view-htmlform\"> \n");
   printf ("<p>\n");
   printf ("<INPUT TYPE=\"submit\" VALUE=\"View a Bibliographic Entry\">\n");
   printf ("<p>\n");
   printf ("</FORM>\n");
   printf ("<p>\n");
   printf ("<p>\n");

   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"tr-browse\"> \n");
   printf ("<p>\n");
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Browse the Bibliographic Database\">\n");
   printf ("<p>\n");
   printf ("</FORM>\n");
   printf ("<p>\n");

   printf ("<hr>\n");
   printf ("<p>\n");
   printf ("<FORM METHOD=\"get\" ACTION=\"%s\">\n", HELP_URL);
   printf ("<p>\n");
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Help on Techrep\">\n");
   printf ("<p>\n");
   printf ("</FORM>\n");
   printf ("<p>\n");
   printf ("<p>\n");
   
   printf ("<hr>\n");
   printf ("<p>\n");
}


void 
tr_browse() 
{
   int c;
   FILE *fp;
   char browsePage[MAX_COMMANDLINE];
   char tempfile[MAX_COMMANDLINE];
   char *passed_file;
   
   char getdb_command[MAX_COMMANDLINE];
   char convert_command[MAX_COMMANDLINE];
   char permission_command[MAX_COMMANDLINE];
   int set_temp_permissions;
   signed int tr_getdb_answer;

   /* Theoretically, the interface is not supposed to know where */
   /* the browse page is, it should get the info from techrep.   */
   /* Oh well. */

   /* open the browse page and send it along */
   sprintf (browsePage, "%s/README.html", TR_REPORT_DIR);
   if ((fp = fopen(browsePage, "r")) == NULL) {
      printf ("Could not get browse page.\n");
   } else {
      while ((c = getc(fp)) != EOF)
         putchar(c);
      fclose (fp);
   }
}


void 
tr_bulkload() 
{
   char bulkload_command[MAX_COMMANDLINE];
  
   signed int answer;
  
   /* if there is no filepath, show the form again */
   if (!C->filepath)  {
      printf ("<TITLE>Technical Report BulkLoad Form</TITLE>");
      printf ("<H1>Technical Report BulkLoad Form</H1>");
      printf ("<HR><UL>");
      printf ("<LI><FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
      printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"tr-bulkload\">\n");
      
      printf ("<H2>Please enter the full path name of the file you want appended to the bibliographic database.\n<p>");
      printf ("<b>FILE:</b>");
      printf ("<INPUT NAME=\"filepath\"><p><p>");
      printf ("<INPUT TYPE=\"submit\" Value=\"Add Bibliographic Entries\">\n");
      printf ("</FORM>");
      printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
      printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
      printf ("<INPUT TYPE=\"submit\" VALUE=\"Cancel\"><P>\n");
      printf ("</FORM>\n");
   } /*end of if no file path entered*/
  
   /* if no error, go ahead and bulkload */
   else  {
      sendTechrepRequest (BULK, C->filepath, NULL);
      printConfirmation(BULK);
   } /*end of if no error (a file path was entered)*/
}
     
     
void 
tr_single_view() 
{
   register int x;
   FILE *fp;
   char tempfile[MAX_COMMANDLINE];
   char *passed_file;
   
   char get_command[MAX_COMMANDLINE];
   char permission_command[MAX_COMMANDLINE];
   int set_temp_permissions;
   signed int tr_get_answer;

   /* if there is no TR num, show the form again */
   if(!C->trId)  {
      printf ("<TITLE>TR Bibliographic Info Form</TITLE>");
      printf ("<H1>TR Bibliographic Info Form</H1>");
      printf ("<HR><UL>");
      printf ("<LI><FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
      printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"tr-single-view\">\n");
      printf ("<H2>Please enter the TR number of the document for which you want to view the bibliographic information.\n<p>");
      printf ("<b>TECHNICAL REPORT NUMBER:</b>");
      printf ("<INPUT NAME=\"trnum\"><p><p>");
      printf ("<INPUT TYPE=\"submit\" Value=\"View Bibliographic Info\">\n");
      printf ("</FORM>");
      printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
      printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
      printf ("<INPUT TYPE=\"submit\" VALUE=\"Return to Techrep Menu\"><P>\n");
      printf ("</FORM>\n");
   } /* end of if no tr num entered */
	
   /* if trnum was entered, get info from techrep */
   else  {
      /*create and open a temporary file to send to techrep*/
      sprintf (tempfile, "/tmp/techrep%d.XXXXXX", getpid());
      mktemp(tempfile);
      passed_file = strdup(tempfile);
      fp=fopen(passed_file,"w");

      /* write the tr number in the file (in refer format) */
      fprintf (fp,"%%%c %s\n\n", REF_REPORT, C->trId);
      fclose(fp);
      
      /* make sure the tempfile has world read and write permissions */
      /*      sprintf (permission_command, "%s ugo+rw %s", CHMOD_CMD, passed_file);*/
      /*      set_temp_permissions = system(permission_command);*/
      chmod (passed_file, 0666);
      
      /* CALL TECHREP */
      sprintf (get_command, "%s/bin/techrep -g %s", TR_HOME, passed_file);
      tr_get_answer = system(get_command);
      /* get the upper 8 bits of the returned code and convert to neg int */
      tr_get_answer = ((~(tr_get_answer>>8)&255)+1)*(-1);
/*      tr_get_answer = DOC_DOES_NOT_EXIST;*/
      
      if (tr_get_answer == DOC_DOES_NOT_EXIST)  { /*NOT IN DATABASE*/
         printf ("<title>Viewing Error</title>\n");
         printf ("<h1>Viewing Error</h1>\n");
         printf ("<h2>TR number %s is not in database</h2>\n<p><p>", C->trId);
         printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
         printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
         printf ("<INPUT TYPE=\"submit\" VALUE=\"Return to Techrep Menu\"><P>\n");
         printf ("</FORM>\n");
	 
      } /*end of if- trnum not in database (call to get)*/
      
      else if (tr_get_answer == ACCESS_DENIED)  { /*CANNOT GET EXCLUSIVE ACCESS*/
         printf ("<title>Database Access Error</title>\n");
         printf ("<h1>Database Access Error</h1>\n");
         printf ("<h2>You have been denied exclusive access to the bibliographic database\n</h2>");
         printf ("<p><FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
         printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
         printf ("<INPUT TYPE=\"submit\" VALUE=\"Return to Techrep Menu\"><P>\n");
         printf ("</FORM>\n");
      } /*end of elseif- exclusive access denied (call to get)*/
      
      else  {		/*TRNUM IN DATABASE*/
         /* read the passed back file and create a view */
         fp=fopen(passed_file,"r");
         printf ("<title>TR Bibliographic Info</title>\n");
         printf ("<h1>TR Bibligraphic Info</h1>\n");
         printf ("<p><hr>");
         Refer2View(passed_file);
         printf ("<p><p><FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
         printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
         printf ("<INPUT TYPE=\"submit\" VALUE=\"Return to Techrep Menu\"><P>\n");
         printf ("</FORM>\n");
         fclose (fp);
      } /*end of else- okay to view (trnum in database) */

      /* delete the temp file */
      unlink (passed_file);
      
   } /*end of else- no error in view entry, try to get bib info*/
}
     
void 
trdelete() 
{
   register int x;
   FILE *fp;
   FILE *fp2;
  
   char tempfile[MAX_COMMANDLINE];
   char *passed_file;
  
   char get_command[MAX_COMMANDLINE];
   char delete_command[MAX_COMMANDLINE];
   char permission_command[MAX_COMMANDLINE];
   int set_temp_permissions;
   signed int tr_get_answer;
   signed int tr_delete_answer;
  
   /* if there is no TR num, show the form again */
   if (!C->trId)  {
      printf ("<TITLE>Technical Report Delete Form</TITLE>");
      printf ("<H1>Technical Report Delete Form</H1>");
      printf ("<HR><UL>");
      printf ("<LI><FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
      printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"trdelete\">\n");
      printf ("<H2>Please enter the TR number of the document for which you want to update or modify bibliographic information.\n<p>");
      printf ("<b>TECHNICAL REPORT NUMBER:</b>");
      printf ("<INPUT NAME=\"trnum\"><p><p>");
      printf ("<INPUT TYPE=\"submit\" Value=\"Update Bibliographic Info\">\n");
      printf ("</FORM>");
      printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
      printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
      printf ("<INPUT TYPE=\"submit\" VALUE=\"Cancel\"><P>\n");
      printf ("</FORM>\n");
   } /*end of if no tr num entered*/
   
   /* if no error, go ahead and delete */
   else  {
      /*create and open a temporary file to send to techrep*/
      sprintf (tempfile, "/tmp/techrep%d.XXXXXX", getpid());
      mktemp(tempfile); 
      passed_file = strdup(tempfile);
      fp=fopen(passed_file,"w");

      if (emailAddress) 
         fprintf (fp, "ContactPerson: %s\n", emailAddress);
      if (comments) 
         fprintf (fp, "Comments: %s\n", comments);


      /* write the TRnum in the file (in refer format) */
      fprintf (fp, "### Begin Citation ### Do not delete this line ###\n");
      write_one_citation (fp, C);
      fclose(fp);
      
      /* make sure the temp file is world readable and writable*/
      chmod (passed_file, 0666);
      
      /* Send request */
      sendTechrepRequest (DELETE, passed_file, NULL);
      printConfirmation (DELETE);
      unlink (passed_file);
   } /*end of if no error (a trnum was entered)*/
}


void 
trpost_query()
{
   register int x;

   FILE *fp;
   char tempfile[MAX_COMMANDLINE];
   char *passed_file;
   
   char list_array[N_STRINGS][STRING_SIZE];
   int listCount;
  
   char post_command[MAX_COMMANDLINE];
   char permission_command[MAX_COMMANDLINE];
   int set_temp_permissions;
   signed int tr_post_answer;
   
   /* Submit for Posting */
   if (!(strcmp(choice,"Submit for Posting")))  {
      
      /* if there is missing data, create error heading for form */
      if (error_flag != 0)  {
         error_flag = 0;		/* reset flag */
         printf ("<title>TR Posting Modification Form</title>\n");
         printf ("<h1>TR Posting Modification Form</h1><hr>\n");
         printf ("<h1>INFORMATION ERROR!</h1>\n");
         printf ("<h2>You MUST provide the TR Number, Date, Title, and Author(s) of a Technical Report to be posted!</h2>\n");
         printf ("<P>\n");
         printf ("<h3>This entry is missing the following:</h3>\n");
         printf ("<OL>");
         if (!C->trId)
            printf ("<li><h3>Technical Report Number</h3>\n");
         if (!C->date)
            printf ("<li><h3>Date Written</h3>\n");
         if (!C->title)
            printf ("<li><h3>Title of Report</h3>\n");
         if (!C->author)
            printf ("<li><h3>Author(s)</h3>\n");
         if (!emailAddress)
            printf ("<li><h3>Email address of contact person</h3>\n");
         printf ("</OL>");
         printf ("<HR>\n");
         /* create form with rest of the data */
         PrintFilledForm(C);
      } /*end of if- to show error screen*/
      
      /* if there is no error, go ahead with post */
      else  {
         /*create and open a temporary file to write to*/
         sprintf (tempfile, "/tmp/techrep-%d.XXXXXX", getpid());
         mktemp (tempfile);
         passed_file = strdup(tempfile);
         fp=fopen(passed_file,"w");
	 
         if (comments) {
            fprintf (fp, "Comments: %s\n", comments);
         }
         if (emailAddress) {
            fprintf (fp, "ContactPerson: %s\n", emailAddress);
         }

         fprintf (fp, "### Begin Citation ### Do not delete this line ###\n");
         write_one_citation(fp, C);

         /* okay, everything has been written to the temp file */
         fclose(fp);
	 
         /* make sure the temp file is world readable */
         /*      sprintf (permission_command, "%s ugo+r %s", CHMOD_CMD, passed_file);*/
         /*      set_temp_permissions = system(permission_command);*/
         chmod (passed_file, 0666);
	 
         sendTechrepRequest (POST_FILE, passed_file, C->url);
         printConfirmation (POST_FILE);

         /* Delete the tempfile */
         unlink (passed_file); 
	 
      } /*end of else- go ahead and post*/
      
   } /* end of Post */
   
   /* Preview */
   else  {
      PreviewReferFormat (C);
   } /*end of Preview*/
}


void 
trpost_repeat()
{
   /* Submit for Posting */
   if (!(strcmp(choice,"Submit for Posting")))  {
      printf ("<title>Technical Report Posting Form</title>\n");
      printf ("<h1>Technical Report Posting Form</h1>\n");
      printf ("<hr>\n");
      PrintFilledForm(C);
   } /* end of post choice */
   
   /* Preview First */
   else  {
      PreviewReferFormat (C);
   } /*end of else preview*/
}


void 
trpost_update_repeat() 
{
   /* Submit for Posting */
   if (!(strcmp(choice,"Submit for Posting")))  {
      printf ("<title>Technical Report Posting Form</title>\n");
      printf ("<h1>Technical Report Posting Form</h1>\n");
      printf ("<hr>\n");
      PrintUpdatedFilledForm(C);
   } /* end of choice post */
   
   /* Preview First */
   else  {
      PreviewUpdatedReferFormat (C);
   } /*end of choice preview*/
}


void 
tr_bulkload_htmlform() 
{
   printf ("<TITLE>Technical Report BulkLoad Form</TITLE>\n");
   printf ("<H1>Technical Report BulkLoad Form</H1>\n");
   
   printf ("<HR>\n");
   
   printf ("<UL>\n");
   printf ("<LI><FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"tr-bulkload\">\n");
   
   printf ("<H2>Please enter the full path name of the file you want appended to the bibliographic database.</H2>\n");
   printf ("<H2>Please note that the file must be world readable!</H2>\n");
   printf ("<p>\n");
   printf ("<b>FILE:</b>\n");
   printf ("<INPUT NAME=\"filepath\"><p>\n");
   printf ("<p>\n");
   printf ("<INPUT TYPE=\"submit\" Value=\"Add Bibligraphic Entries\"><P>\n");
   printf ("<INPUT TYPE=\"reset\" VALUE=\"Reset\"><p>\n");
   printf ("</FORM>\n");
   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
   
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Cancel\"><P>\n");
   printf ("</FORM>\n");
}

void 
tr_single_view_htmlform()
{
   printf ("<TITLE>TR Bibliographic Info Form</TITLE>\n");
   printf ("<H1>TR Bibliographic Info Form</H1>\n");
   
   printf ("<HR>\n");
  
   printf ("<UL>\n");
   printf ("<LI><FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"tr-single-view\">\n");
   
   printf ("<H2>Please enter the TR number of the document for which you want to view bibliographic information</H2>\n");
   printf ("<p>\n");
   printf ("<b>TECHNICAL REPORT NUMBER:</b>\n");
   printf ("<INPUT NAME=\"trnum\"><p>\n");
   printf ("<p>\n");
   printf ("<INPUT TYPE=\"submit\" Value=\"View Bibliographic Info\"><p>\n");
   printf ("<INPUT TYPE=\"reset\" VALUE=\"Reset\"><p>\n");
   printf ("</FORM>\n");
   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
   
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Return to Techrep Menu\"><P>\n");
   printf ("</FORM>\n");
}


void 
trdelete_htmlform() 
{
   printf ("<TITLE>Technical Report Delete Form</TITLE>\n");
   printf ("<H1>Technical Report Delete Form</H1>\n");
   
   printf ("<HR>\n");
   
   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"trdelete\">\n");
   
   printf ("<H2>Please enter the TR number of the document you want deleted from the bibliographic database.</H2>\n");
   printf ("<p>\n");
   printf ("<b>TECHNICAL REPORT NUMBER:</b>\n");
   printf ("<INPUT NAME=\"trnum\"><p>\n");
   printf ("<p>\n");
 
   
   printf ("<b>CONTACT PERSON:</b> The email address (probably your own) of the\n");
   printf ("person who will verify the information provided here.<br>\n");
   printf ("<INPUT SIZE=50 NAME=\"email\"> <p>\n");

   printf ("<INPUT TYPE=\"submit\" Value=\"Delete Bibliographic Info\"><P>\n");
   printf ("<INPUT TYPE=\"reset\" VALUE=\"Reset\"><p>\n");
   printf ("</FORM>\n");
   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
   
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Cancel\"><P>\n");
   printf ("</FORM>\n");
}

void 
trpost_htmlform() 
{
   printf ("<TITLE>Technical Report Posting Form</TITLE>\n");
   printf ("<H1>Technical Report Posting Form</H1>\n");
   
   printf ("<HR>\n");

   /* C here is essentially empty */
   PrintFilledForm (C);
}

void 
trpost_update()
{
   register int x;
   FILE *fp;
   char tempfile[MAX_COMMANDLINE];
   char *passed_file;
  
   char list_array[N_STRINGS][STRING_SIZE];
   int listCount;
  
   char update_command[MAX_COMMANDLINE];
   char permission_command[MAX_COMMANDLINE];
   int set_temp_permissions;
   signed int tr_update_answer;
      
   /* Submit for RePosting */
   if (!(strcmp(choice,"Submit for RePosting")))  {
      
      /* if there is an error, create error header for form */
      if (error_flag != 0)  {
         error_flag= 0;		/* reset flag */
         printf ("<title>TR Posting Modification Form</title>\n");
         printf ("<h1>TR Posting Modification Form</h1><hr>\n");
         printf ("<h1>INFORMATION ERROR!</h1>\n");
         printf ("<h2>You MUST provide the TR Number, Date, Title, and Author(s) of a Technical Report to be posted!</h2>\n");
         printf ("<P>\n");
         printf ("<h3>This entry is missing the following:</h3>\n");
         printf ("<OL>");
         if (!C->trId)
            printf ("<li><h3>Technical Report Number</h3>\n");
         if (!C->date)
            printf ("<li><h3>Date Written</h3>\n");
         if (!C->title)
            printf ("<li><h3>Title of Report</h3>\n");
         if (!C->author)
            printf ("<li><h3>Author(s)</h3>\n");
         if (!emailAddress)
            printf ("<li><h3>Email address of contact person</h3>\n");
         printf ("</OL>");
         printf ("<HR>\n");
         PrintUpdatedFilledForm(C);
      } /*end of if- missing data error*/
      
      /* if no missing data, go ahead with post */
      else  {
	 char *tempname;
         /*create a temporary file to write to*/
         sprintf (tempfile, "/tmp/techrep%d.XXXXXX", getpid());
         mktemp(tempfile);
         passed_file = strdup(tempfile);
         fp=fopen(passed_file,"w");

         if (emailAddress) 
            fprintf (fp, "ContactPerson: %s\n", emailAddress);

         if (comments) 
            fprintf (fp, "Comments: %s\n", comments);

	 if (tempname=getenv("REMOTE_HOST"))
	    fprintf (fp, "Remote host: %s\n", tempname);

	 if (tempname=getenv("REMOTE_IDENT"))
	    fprintf (fp, "Remote ident: %s\n", tempname);

         fprintf (fp, "### Begin Citation ### Do not delete this line ###\n");
         write_one_citation (fp, C);
         /* okay, everything has been written to the temp file */
         fclose(fp);
	 
         /* make sure the temp file is world readable */
         chmod (passed_file, 0666);
	 
         sendTechrepRequest (UPDATE_FILE, passed_file, C->url);
         printConfirmation (UPDATE_FILE);
         unlink (passed_file);
      } /* end of else- final posting */
      
   } /* end of post choice */
   
   else  {
      PreviewUpdatedReferFormat (C);
   } /* end of preview choice */
}

void 
trupdate_htmlform()
{
   printf ("<TITLE>Technical Report Update Request</TITLE>\n");
   printf ("<H1>Technical Report Update Request</H1>\n");
   
   printf ("<HR>\n");
   
   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"trupdate\"> \n");
   
   printf ("<H2>Please enter the TR number of the document for which you want to update or modify bibliographic information.</H2>\n");
   printf ("<p>\n");
   printf ("<b>TECHNICAL REPORT NUMBER:</b>\n");
   printf ("<INPUT NAME=\"trnum\"><p>\n");
   printf ("<p>\n");
   printf ("<INPUT TYPE=\"submit\" Value=\"Update Bibliographic Info\"><P>\n");
   printf ("<INPUT TYPE=\"reset\" VALUE=\"Reset\"><p>\n");
   printf ("</FORM>\n");
   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\"> \n");
   
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Cancel\"><P>\n");
   printf ("</FORM>\n");
}

void 
trupdate() 
{
   register int x;
  
   FILE *fp;
   char tempfile[MAX_COMMANDLINE];
   char *passed_file;
   
   char get_command[MAX_COMMANDLINE];
   char permission_command[MAX_COMMANDLINE];
   int set_temp_permissions;
   signed int tr_get_answer;
   signed int tr_update_answer;
   
   /* if there is no TR num, show the form again */
   if (!C->trId) {
      printf ("<TITLE>Technical Report Update Request</TITLE>");
      printf ("<H1>Technical Report Update Request</H1>");
      printf ("<HR><UL>");
      printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
      printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"trupdate\"> \n");
      
      printf ("<H2>Please enter the TR number of the document for which you want to update or modify bibliographic information.\n<p>");
      printf ("<b>TECHNICAL REPORT NUMBER:</b>");
      printf ("<INPUT NAME=\"trnum\"><p><p>");
      printf ("<INPUT TYPE=\"submit\" Value=\"Update Bibliographic Info\">\n");
      printf ("</FORM>");
   } /* end of if no tr num entered */
   
   /* if a TR num is entered, go ahead with getting the update info from techrep */
   else  {
      /*create and open a temporary file to send to techrep*/
      sprintf (tempfile, "/tmp/techrep%d.XXXXXX", getpid());
      mktemp(tempfile);
      passed_file = strdup(tempfile);
      fp=fopen(passed_file,"w");
      /* write the trnumber to the file (in refer format) */
      fprintf (fp,"%%R %s\n\n",C->trId);
      fclose(fp);
      
      /* make sure the tempfile has world read and write permissions */
      /*      sprintf (permission_command, "%s ugo+rw %s", CHMOD_CMD, passed_file);*/
      /*      set_temp_permissions = system(permission_command);*/
      chmod (passed_file, 0666);
      
      /* CALL TECHREP */
      sprintf (get_command, "%s/bin/techrep -g %s", TR_HOME, passed_file);
      tr_get_answer = system(get_command);
      
      /* get the upper 8 bits of the returned code */
      /* and convert to negative int */
      tr_get_answer = ((~(tr_get_answer>>8)&255)+1)*(-1);
      
      
      if (tr_get_answer == DOC_DOES_NOT_EXIST)  { /*NOT IN DATABASE*/
         printf ("<title>Update Error</title>\n");
         printf ("<h1>Update Error</h1>\n");
         printf ("<h2>TR number %s is not in database\n</h2><p><p>", C->trId);
         printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
         printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\"> \n");
	 
         printf ("<INPUT TYPE=\"submit\" VALUE=\"Return to Techrep Menu\"><P>\n");
         printf ("</FORM>\n");
      } /* end of trnum not in database (on call to get) */
      
      else if (tr_get_answer == ACCESS_DENIED)  { /*CANNOT GET EXCLUSIVE ACCESS*/
         printf ("<title>Database Access Error</title>\n");
         printf ("<h1>Database Access Error</h1>\n");
         printf ("<h2>You have been denied exclusive access to the bibliographic database\n</h2>");
         printf ("<p><FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
         printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\"> \n");
         printf ("<INPUT TYPE=\"submit\" VALUE=\"Return to Techrep Menu\"><P>\n");
         printf ("</FORM>\n");
      } /* end of denied access (on call to get) */
      
      else  {			/*TRNUM IN DATABASE*/
         /* open file techrep has written to, for reading */
         fp=fopen(passed_file,"r");
         /* Create form header and parse the file (by calling Refer2Form) */
         printf ("<title>Technical Report Update Form</title>\n");
         printf ("<h1>Technical Report Update Form</h1>\n");
         printf ("<p><hr>");
         Refer2Form(passed_file);
      } /* end of go ahead with update (after call to get) */
      
      /* delete the temp file */
      unlink (passed_file);
      
   } /*end of else- no error in update entry, try to get bib info*/
}

int 
sendTechrepRequest(int methodID, char *tempFilename, char *trLocation)
{
   char methodString[STRING_SIZE];
   char mailCommand[STRING_SIZE];

   switch (methodID) {
    case POST:
    case POST_FILE:
      strcpy (methodString, "techrep: POST request");
      break;
    case UPDATE:
    case UPDATE_FILE:
      strcpy (methodString, "techrep: UPDATE request");
      break;
    case DELETE:
      strcpy (methodString, "techrep: DELETE request");
      break;
    case BULK:
      strcpy (methodString, "techrep: BULK request");
      break;
   }
   sprintf (mailCommand, "/usr/ucb/mail -s \"%s\" %s < %s",
	                 methodString, TR_ADMIN, tempFilename);
   system (mailCommand);
}

void
printConfirmation(int methodID)
{
   switch (methodID) {
     case POST:
     case POST_FILE:
       printf ("<title>Posting Confirmation</title>");
       printf ("<h1>Your POST request has been sent to the Report Administrator</h1>");
       break;
     case UPDATE:
     case UPDATE_FILE:
       printf ("<title>Update Confirmation</title>");
       printf ("<h1>Your UPDATE request has been sent to the Report Administrator</h1>");
       break;
     case DELETE:
       printf ("<title>Delete Confirmation</title>");
       printf ("<h1>Your DELETE request has been sent to the Report Administrator</h1>");
       break;
     case BULK:
       printf ("<title>Bulk Load Confirmation</title>");
       printf ("<h1>Your BULK LOAD request has been sent to the Report Administrator</h1>");
       break;
   }
   printf ("<FORM METHOD=POST ACTION=\"%s\">\n", MASTERFORM);
   printf ("<INPUT TYPE=\"hidden\" NAME=\"tr_task_data\" VALUE=\"techrep-menu-htmlform\">\n");
   printf ("<INPUT TYPE=\"submit\" VALUE=\"Return to Techrep Menu\"><P>\n");
   printf ("</FORM>\n");
}      

