/* Collection of code snippets by Arne VajhÃ¸j */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
/*
 *  Author     : Arne Vajhøj
 *
 *  Programmed : july 1996
 *
 *  Purpose    : Calendar
 *
 *  Modifies   : july 1996
 *               TABLES option.
 *
 *               august 1996
 *               Control of access for other users.
 *               Script-based creation.
 *
 *               september 2000
 *               Handle 20XX years correct.
 *
 *               september 2002
 *               Small HTML correction.
 *
 */

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

#include "cgilib.h"

void replace(char *s,char *s1,char *s2);

main(int argc,char *argv[])
{
   FILE *fp;
   int l,y,m,d,i;
   char *p,url[65],fnm[65],line[257],ystr[3],mstr[3],dstr[3],tmp[257];
   time_t t;
   struct tm *ts;
   cgi_init_env(argc,argv);
   /* get username */
   p=cgi_info("PATH_INFO");
   p++;
   sprintf(url,"/~%s/cal.txt",p);
   cgi_translate_path(url,1,fnm,sizeof(fnm)-1,&l);
   fnm[l]='\0';
   /* write HTML header */
   for(i=0;i<strlen(p);i++) {
      p[i] = toupper(p[i]);
   }
   cgi_begin_output(1);
   cgi_printf("content-type: text/html\n\n");
   cgi_printf("<HTML>\n");
   cgi_printf("<HEAD>\n");
   cgi_printf("<TITLE>Calendar of %s</TITLE>\n",p);
   cgi_printf("</HEAD>\n");
   cgi_printf("<BODY BGCOLOR=\"#FFFFFF\">\n");
   cgi_printf("<H1>Calendar of %s</H1>\n",p);
   /* output files */
   fp=fopen(fnm,"r");
   if(fp==NULL) {
      cgi_printf("<P>Can not find calendar</P>\n");
      cgi_printf("<HR>\n");
      cgi_printf("[<A HREF=\"/htbin/cal10.com/%s%s\">Create calendar</A>]\n",p,fnm);
      goto fin;
   }
#ifndef TABLES
   cgi_printf("<DL>\n");
#else
   cgi_printf("<TABLE BORDER>\n");
#endif
   t=time(NULL);
   ts=localtime(&t);
   while (fgets(line,sizeof(line),fp)) {
      line[strlen(line)-1]='\0';
      strncpy(ystr,line,2);
      ystr[2]='\0';
      y=atoi(ystr);
      if(y<50) y=100+y;
      strncpy(mstr,line+2,2);
      mstr[2]='\0';
      m=atoi(mstr);
      strncpy(dstr,line+4,2);
      dstr[2]='\0';
      d=atoi(dstr);
      if((y*366+m*31+d)>=(ts->tm_year*366+(ts->tm_mon+1)*31+ts->tm_mday-7)) {
#ifndef TABLES
         cgi_printf("%s\n",line+6);
#else
         cgi_printf("<TR>\n");
         strcpy(tmp,line+6);
         replace(tmp,"<DD>","<TD>");
         replace(tmp,"<DT>","</TD><TD>");
         cgi_printf("%s",tmp);
         cgi_printf("</TD>\n</TR>\n");
#endif
      }
   }
   /* write HTML footer */
#ifndef TABLES
   cgi_printf("</DL>\n");
#else
   cgi_printf("</TABLE>\n");
#endif
   cgi_printf("<HR>\n");
   cgi_printf("[<A HREF=\"/htbin/cal2.com/%s%s\">Add entry</A>]\n",p,fnm);
   cgi_printf("[<A HREF=\"/htbin/cal4.com/%s%s\">Remove entry</A>]\n",p,fnm);
   cgi_printf("[<A HREF=\"/htbin/cal8.com/%s%s\">Access control</A>]\n",p,fnm);
fin:
   cgi_printf("</BODY>\n");
   cgi_printf("</HTML>\n");
   fclose(fp);
}
