/*------------------------------------------------------------
 CarcHTML v1.1  Mark Weller (c)1999,2000.
 Creates an HTML page for archive directories for downloading.
 You can use this source code - no restrictions.
--------------------------------------------------------------*/


#include <sys/dir.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

#define CAH_PROGRAM             "CarcHTML v1.1 - Mark Weller (c)1999,2000"
#define CAH_MAXPATH         255
#define CAH_MAXFILE         31
#define CAH_MAXSHORT        100
#define CAH_README          ".readme"
#define CAH_HTMLNAME        "index.html"
#define CAH_ON              1
#define CAH_OFF             0

#define CAH_EXT             10

char *extension_types[] =
{
 "readme","README","htm","HTM","html","HTML","shtml","SHTML","info","INFO",""
};

char *defbackcol[] =
{
 "FFFFFF",""
};

int IsFileValid(char *);
int IsDir(char *,char *);
char *GetFileSize(char *,char *,char *);
char *GetShort(char *,char *,char *);
void Usage(char *);
char *GetReadme(char *,char *);

void main(int argc,char *argv[])
{
 DIR           *dirp;
 struct direct *dp;
 char          *name;

 char          fsize[30];

 char gdir[CAH_MAXPATH];
 char readme_file[CAH_MAXFILE];
 char short_desc[CAH_MAXSHORT];
 char create_dwnld[CAH_MAXPATH];
 char *tback_col_1;
 char *tback_col_2;
 char *tback_col_3;
 char *pback_col;

 int cah_result = 0;
 int temp_cnt   = 0;

 FILE *download;

/*-command line switches-*/
 int v_switch   = CAH_OFF; /*-verbose            -*/
 int a_switch   = CAH_OFF; /*-aminet short       -*/
 int b_switch   = CAH_OFF; /*-back/table colours -*/
 int t_switch   = CAH_OFF; /*-page title         -*/

        gdir[0] ='\0';

        /*-set default background colours-*/

        tback_col_1 = defbackcol[0];
        tback_col_2 = defbackcol[0];
        tback_col_3 = defbackcol[0];
        pback_col = defbackcol[0];

        /*-process command line arguments-*/

        if(argc > 8) Usage(NULL);

        switch(argc)
              {
               case 1 : Usage(NULL);     break;
               case 2 : name = argv[1];  break;
               case 3 :
               case 4 :
               case 7 :
               case 8 :   if(argv[1][0] != '-')
                            {
                             Usage(NULL);
                            }
                          else
                            {
                             for(temp_cnt = 0;temp_cnt != strlen(argv[1]);temp_cnt++)
                                {
                                 switch(argv[1][temp_cnt])
                                       {
                                        case '-' : break;
                                        case 'a' : a_switch = CAH_ON; break;
                                        case 'v' : v_switch = CAH_ON; break;
                                        case 't' : switch(argc)
                                                         {
                                                          case 4  : t_switch = 3; break; /*argv[3]*/
                                                          case 8  : t_switch = 7; break; /*argv[7]*/
                                                          default : Usage(NULL);  break;
                                                         }
                                                   break;
                                        case 'b' : if(argc == 7 || argc == 8)
                                                     {
                                                      b_switch = CAH_ON;
                                                      pback_col = argv[3];
                                                      tback_col_1 = argv[4];
                                                      tback_col_2 = argv[5];
                                                      tback_col_3 = argv[6];

                                                     }
                                                    else
                                                     {
                                                      Usage(NULL);
                                                     }
                                                    break;
                                       }
                                }
                             name = argv[2];
                            }
                          break;
               default :  Usage(NULL); break;
              }


    /*-open requested directory-*/

        if ((dirp = opendir(name)) == NULL) {
                Usage("Can't open directory ?");
                }

    /*-create index.html -*/
        strcpy(create_dwnld,name);
        AddPart(create_dwnld,CAH_HTMLNAME);

        if(!(download = fopen(create_dwnld,"w")))  /*html.file*/
          {
           closedir(dirp);
           Usage("ERR: Can not create HTML file ?");
          }
    /*-create header for index.html-*/

        fprintf(download,"<HTML>\n");
        fprintf(download," <HEAD>\n");
        if(t_switch == CAH_OFF)
          {
           fprintf(download,"  <TITLE>Downloads</TITLE>\n");
          }
        else
          {
           fprintf(download,"  <TITLE>%s</TITLE>\n",argv[t_switch]);
          }
        fprintf(download," </HEAD>\n\n");
        fprintf(download," <BODY BGCOLOR=%c%c%s%c>\n",'"','#',pback_col,'"');
        fprintf(download,"  <!-- Created using %s  -->\n",CAH_PROGRAM);
        fprintf(download,"  <!-- ARCHIVE INDEX STARTS HERE -->\n\n");

        if(t_switch == CAH_OFF)
          {
           fprintf(download,"  <H3><HR ALIGN=LEFT>Downloads</H3>\n");
          }
        else
          {
           fprintf(download,"  <H3><HR ALIGN=LEFT>%s</H3>\n",argv[t_switch]);
          }
        fprintf(download,"  <TABLE BORDER=0 WIDTH=100%%>\n");

        if(v_switch == CAH_ON)
          {
           printf("------------------------------------------------\n");
           printf("%s\n",CAH_PROGRAM);
           printf("------------------------------------------------\n");
           printf("Archive Directory      = %s\n\n",name);

           if(t_switch == CAH_OFF)
             { printf("Title = Download (default)\n"); }
           else { printf("Title = %s USED\n",argv[t_switch]); }

           if(b_switch == CAH_ON)
             {
              printf("Page Background colour = %s USED\n",pback_col);
              printf("FileName column colour = %s USED\n",tback_col_1);
              printf("FileSize column colour = %s USED\n",tback_col_2);
              printf("Readme   column colour = %s USED\n",tback_col_3);
             }
           else
             {
              printf("Default Colours  = USED\n");
             }

          }

        /*-process archive files in directory-*/

        for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
             {
              if(IsDir(name,dp->d_name) == 1)  /*-Not Sub Directory-*/
                {
                 if(IsFileValid(dp->d_name) == 1)
                   {
                    fsize[0] ='\0';
                    GetFileSize(name,dp->d_name,fsize);
                    readme_file[0] = '\0';
                    GetReadme(dp->d_name,readme_file);

                    if(a_switch == CAH_ON)
                      {
                       short_desc[0] = '\0';
                       GetShort(name,readme_file,short_desc);
                      }
                    fprintf(download,"   <TR>\n");
                    fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=40%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_1,'"','"',dp->d_name,'"',dp->d_name);
                    fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=10%%><CENTER>%s</TD>\n",'"','#',tback_col_2,'"',fsize);

                    if(a_switch == CAH_ON)
                      {
                       if(strlen(short_desc) == 0)  /*- no short found -*/
                         fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=50%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_3,'"','"',readme_file,'"',readme_file);
                      else
                         fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=50%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_3,'"','"',readme_file,'"',short_desc);
                      }
                    else
                      {
                       fprintf(download,"    <TD BGCOLOR=%c%c%s%c WIDTH=50%%><LEFT><A HREF=%c%s%c>%s</A></TD>\n",'"','#',tback_col_3,'"','"',readme_file,'"',readme_file);
                      }
                    fprintf(download,"   </TR>\n");

                    /*-process verbose option-*/
                    if(v_switch == CAH_ON)
                      {
                       if(a_switch == CAH_ON)
                         {
                          if(strlen(short_desc) == 0)  /*- no short found -*/
                             printf("%25s - %6s - %s SHORT NOT FOUND\n",dp->d_name,fsize,readme_file);
                          else
                             printf("%25s - %6s - %s SHORT USED\n",dp->d_name,fsize,readme_file);
                         }
                       else
                         {
                          printf("%25s - %6s - %s \n",dp->d_name,fsize,readme_file);
                         }
                      }
                   }
                }
             }

        closedir(dirp);

        if(v_switch == CAH_ON)
           printf("------------------------------------------------\n");

    /*- Create end for index.html -*/
        fprintf(download,"  </TABLE>\n");
        fprintf(download,"  <!-- END OF INDEX ARCHIVE -->\n");
        fprintf(download,"  <H3><HR ALIGN=LEFT></H3>\n");
        fprintf(download," </BODY>\n");
        fprintf(download,"</HTML>\n");
        fclose(download);

        exit(0);
}

char *GetShort(char *dir,char *readme,char *s_desc)
{
char temp_path[CAH_MAXPATH];
char temp_sdes[CAH_MAXSHORT];
char temp_sdes2[CAH_MAXSHORT];

int des_len = 0;
int flag = 0;
int counter = 5;
FILE *read;

    temp_path[0] = '\0';
    temp_sdes[0] = '\0';
    temp_sdes2[0] = '\0';

    strcpy(temp_path,dir);
    AddPart(temp_path,readme);

    if(!(read = fopen(temp_path,"r")))  /*text file*/
      {
       sprintf(s_desc,"%s",temp_sdes);
      }
    else
      {
       fgets(temp_sdes,CAH_MAXSHORT,read); /*- get first line - should be Short: -*/
       fclose(read);

       /*- check for Short: -*/

        if(temp_sdes[0] == 'S' || temp_sdes[0] == 's' ) flag += 1;
        if(temp_sdes[1] == 'H' || temp_sdes[1] == 'h' ) flag += 1;
        if(temp_sdes[2] == 'O' || temp_sdes[2] == 'o' ) flag += 1;
        if(temp_sdes[3] == 'R' || temp_sdes[3] == 'r' ) flag += 1;
        if(temp_sdes[4] == 'T' || temp_sdes[4] == 't' ) flag += 1;
        if(temp_sdes[5] == ':') flag += 1;

        if(flag == 6)  /*- found Short: -*/
          {
           flag = 0;
           des_len = strlen(temp_sdes); des_len--;

           if(temp_sdes[des_len] == '\n') temp_sdes[des_len] = '\0';

           des_len = strlen(temp_sdes); des_len--;

           for(counter = 6;counter != des_len + 1;counter++)
              {
               temp_sdes2[counter - 6] = temp_sdes[counter];
              }

           sprintf(s_desc,"%s",temp_sdes2);

          }
        else
          {
           temp_sdes[0] = '\0';
           sprintf(s_desc,"%s",temp_sdes);
          }
      }


}


char *GetReadme(char *fname,char *readme)
{
char temp_buff[CAH_MAXFILE];
int name_len = 0;
int flag = 0;

    temp_buff[0] = '\0';
    strcpy(temp_buff,fname);
    name_len = (strlen(temp_buff) - 1);

    while(flag != 1)
         {
          if(temp_buff[name_len] == '.' || name_len == 0)
            {
             temp_buff[name_len] = '\0';
             flag = 1;
            }
          else
            {
             temp_buff[name_len] = '\0';
            }
          name_len--;
         }

     if(name_len == -1)
        sprintf(readme,"%s%s",fname,CAH_README); /*no extension*/
     else
        sprintf(readme,"%s%s",temp_buff,CAH_README);
}

void Usage(char *mess)
{
 printf("----------------------------------------\n");

 if(mess)
   {
    printf("mess:%s\n",mess);
    printf("----------------------------------------\n");
   }

  printf("%s\n",CAH_PROGRAM);
  printf("----------------------------------------\n");
  printf("USAGE:\n");
  printf(" CarcHTML <-vatb> [arch directory] [[pagecol] [col1] [col2] [col3]] [title]\n\n");
  printf("options: -\n");
  printf("           v   = verbose - displays files being indexed\n");
  printf("           a   = aminet  - uses the 'Short:' description\n");
  printf("                 in readme file instead of readme file name\n");
  printf("           t   = title (page + above archive list \n");
  printf("           b   = page + table background colors\n\n");
  printf("example: CarcHTML -vb Work:myarchive FFFFFF 888888 FFFF00 0000FF\n");
  printf("           -----------*----------\n");
  printf(" This will create a HTML file called 'index.html' inside\n");
  printf("  Work:myarchive, which will display a table with an entry\n");
  printf("  like this:-\n\n");
  printf(" myprogs.lha  1.2MB  myprogs.readme\n\n");
  printf(" The size of the file will be placed in table which is auto\n");
  printf("  calculated.\n");
  printf(" When browsed the page color will be WHITE\n");
  printf("  Column 1 = GREY Column 2 = YELLOW Column 3 = BLUE\n");
  printf("           -----------*----------\n");
  printf("See CarcHTML.readme for more information.\n");
  printf("E-Mail: mark@kontumnam.freeserve.co.uk\n");
  printf("WWW   : www.kontumnam.freeserve.co.uk\n");
  printf("----------------------------------------\n");

  exit(0);
}

char *GetFileSize(char *dname,char *fname,char *fsize)
{

struct stat load_stat;
char gfile_buff[255];
int size = 0;
int rem  = 0;
int ssize= 0;

   gfile_buff[0]='\0';
   strcpy(gfile_buff,dname);
   AddPart(gfile_buff,fname);

   stat(gfile_buff,&load_stat);

   ssize = (int)load_stat.st_size;

   if(ssize < 1024)
     {
      sprintf(fsize,"%db",ssize);             /*bytes*/
     }
   else
     {
      size = ssize / 1024;
      if(size < 1024)
        {
         sprintf(fsize,"%dKb",size);               /*kbytes*/
        }
      else
        {
         rem = size % 1024;
         if(rem < 100) rem = 1; else rem /= 100;

         sprintf(fsize,"%d.%dMb",size / 1024,rem); /*Mbytes*/
        }
     }

}

int IsDir(char *dname,char *fname)
{

struct stat load_stat;
char gfile_buff[255];

   gfile_buff[0]='\0';
   strcpy(gfile_buff,dname);
   AddPart(gfile_buff,fname);

   stat(gfile_buff,&load_stat);

   if(S_ISDIR(load_stat.st_mode) != 0)
     return(0);
   else
     return(1);
}


IsFileValid(char *fname)
{
 char temp_name[31];
 char ext_buff[31];
 int name_length = 0;
 int len_cnt = 0;
 int ext_cnt = 0;
 int check = 0;
 int result = 1;

   temp_name[0] = '\0';
   ext_buff[0] = '\0';

   strcpy(temp_name,fname);
   name_length = strlen(temp_name);

   /*--get file extension--*/
   for(len_cnt = 0;len_cnt != name_length;len_cnt++)
      {
       if(temp_name[len_cnt] == '.') /*-extension start-*/
         {
          if(check == 1)   /*-reset because file must 'my.file.???'-*/
            {
             ext_buff[0] = '\0';
             ext_cnt = 0;
            }
          else
            {
             check = 1;
            }
         }
       else
         {
          if(check == 1) /* copy extension */
            {
             ext_buff[ext_cnt] = temp_name[len_cnt];
             ext_cnt++;
            }
         }
      }
    ext_buff[ext_cnt] = '\0';

    ext_cnt = 0;
    for(ext_cnt = 0;ext_cnt != CAH_EXT;ext_cnt++)
       {
        if(strcmp(ext_buff,extension_types[ext_cnt]) == 0)
           result = 0;
       }
 return(result);
}
