/* ------------------------------------------------------------------------ */
/*                      SIM System-Folder (shell Client)                    */
/*                          ---------------------                           */
/*  © by Volker Graf in 1995. This Program is distributed under the terms   */
/*  of FREEWARE                                                             */
/* ------------------------------------------------------------------------ */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FILEENDINGS "s:FileEndings"

/* /// Prototypes & Global Variables */

/* ##################### Function Prototypes ####################### */
int ScanDataBase (void);
int AllocateMemory (void);
int ReadDataBase(void);
void CleanUp (void);
/* ################################################################# */

/* #####################  Global variables  ######################## */
char **FileEndings, **Comments, **DestPath;
int MAXLINES, MAXCOMMENT, MAXDESTPATH;  /* Values for File analysis */
/* ################################################################# */
/* /// */

/* ///ScanDataBase*************************************** */
int ScanDataBase ()
/*      FUNCTION:   Analyse the DataBase-File
 *      PARAMETERS:
 *         INPUT: -
 *         OUTPUT: 0 (O.K.)  -1 (FAIL)
 * ****************************************************** */
{
   FILE *FP;
   char c, DUMMY[1000];         /* Source of ERROR !!!! */
   int comment, ocomment, destpath, odestpath, lines;

   comment = destpath = lines = 0;

   FP = fopen (FILEENDINGS, "r");

   if (FP == NULL)
      return (-1);              /* ERROR Could NOT open Database */

   while ((fscanf (FP, "%s", &DUMMY)) != EOF) {         /* Analyse the DataBase File */
      lines++;                  /* Lines *//* lines will be the # of entries */

      ocomment = odestpath = 0;

      while ((c = fgetc (FP)) != EOF && (c != '#'))     /* comment will be the max length of */
         ocomment++;            /*   chars for the comment line */
      if (ocomment > comment)
         comment = ocomment;
      while ((c = fgetc (FP)) != EOF && (c != '\n'))    /* destpath will be the max. length of */
         odestpath++;           /*   chars of the dest. path description */
      if (odestpath > destpath)
         destpath = odestpath;
   }
   MAXLINES = lines;
   MAXCOMMENT = comment;
   MAXDESTPATH = destpath;

   fclose (FP);
}
/* /// */

/* /// AllocateMemory************************************* */
int AllocateMemory ()
{
/*      FUNCTION: Allocates Memory for the DataBase entries
 *      PARAMETERS:
 *         INPUT:  -
 *         OUTPUT: 0 (O.K.) -1 (FAIL)
 * ******************************************************** */

   int i;

   FileEndings = (char **) malloc (MAXLINES * sizeof (char *));
   if (FileEndings == NULL)
      return (-1);

   for (i = 0; i < MAXLINES; i++) {
      FileEndings[i] = (char *) malloc (5 * sizeof (char));
      if (FileEndings[i] == NULL)
         return (-1);
   }                            /* End FOR */

   Comments = (char **) malloc (MAXLINES * sizeof (char *));
   if (Comments == NULL)
      return (-1);

   for (i = 0; i < MAXLINES; i++) {
      Comments[i] = (char *) malloc (MAXCOMMENT * sizeof (char));
      if (Comments[i] == NULL)
         return (-1);
   }                            /* End FOR */

   DestPath = (char **) malloc (MAXLINES * sizeof (char *));
   if (DestPath == NULL)
      return (-1);

   for (i = 0; i < MAXLINES; i++) {
      DestPath[i] = (char *) malloc (MAXDESTPATH * sizeof (char));
      if (DestPath[i] == NULL)
         return (-1);
   }                            /* End FOR */
}

/* /// */

/* ///CleanUp******************************************** */
void CleanUp ()
{
/*  FUNCTION: Cleans up all the allocated memory
 *  PARAMETERS:
 *     INPUT: -
 *     OUTPUT: -
 * ************************************************ */

   int i;

   if (FileEndings) {
      for (i = 0; i < MAXLINES; i++) {
         if (FileEndings[i])
            free (FileEndings[i]);
      }
      free (FileEndings);
   }

   if (Comments) {
      for (i = 0; i < MAXLINES; i++) {
         if (Comments[i])
            free (Comments[i]);
      }
      free (Comments);
   }

   if (DestPath) {
      for (i = 0; i < MAXLINES; i++) {
         if (DestPath[i])
            free (DestPath[i]);
      }
      free (DestPath);
   }
}
/* /// */

/* ///ReadDataBase*************************************** */
int ReadDataBase ()
{
/*  FUNCTION: Read the DataBase and assign data to "FileEndings", "Comments" and "DestPath"
 *  PARAMETERS:
 *     INPUT:  -   (e.g. later FileName)
 *     OUTPUT: 0 (O.K.)    -1 (FAIL)
 * ************************************************** */
   FILE *FP;
   int i, x;

   i = x = 0;
   FP = fopen (FILEENDINGS, "r");

   if (FP == NULL)
      return (-1);

   while ((fscanf (FP, "%s", FileEndings[i])) != EOF) {
      while (((Comments[i][x] = fgetc (FP)) != EOF) && (Comments[i][x] != '#'))
         x++;
      Comments[i][x]= '\0';
      x = 0;
      while (((DestPath[i][x] = fgetc (FP)) != EOF) && (DestPath[i][x] != '\n'))
         x++;
      DestPath[i][x]= '\0';
      x = 0;
      i++;
   }
  fclose (FP);
}
/* /// */

/* ///Analyze******************************************** */
int Analyze (char *INPUT){
/*  FUNCTION: Read the Input and check for Ending
        PARAMETERS:
            INPUT: char *INPUT
            OUTPUT: 0(O.K.)  -1(FAIL)
    ***************************************************** */
    int x,y,i,z;
    char *STRING, COMMANDSTRING[1000];  /* ERROR SOURCE !!!!!! */


    i = z = 0;
    x = strlen(INPUT);
    y = x;

    STRING = (char *) malloc (x * sizeof(char));


    x--; /* Last Letter */

    while((INPUT[x] != '.') && (x >= 0))
        x--;
    if (x==0)   /* is there a '.' in the Filename ? */
        return(-1);

    for (i=x;i<y;i++)
        STRING[z++]=INPUT[i];
    STRING[z++] = '\0';
    /* Now we created the ENDING .. now let's look if we have it in our DataBase */

    for (i=0;i<MAXLINES;i++){
        if ((strcmp(STRING,FileEndings[i])) == 0){   /* An Entry an Entry an Entry !!!! */
            strcpy(COMMANDSTRING,"copy \"");
            strcat(COMMANDSTRING,INPUT);
            strcat(COMMANDSTRING,"\" \"");
            strcat(COMMANDSTRING,DestPath[i]);
            strcat(COMMANDSTRING,"\"");
            system(COMMANDSTRING);
            }
    }


}
/* /// */


/* ################################################################################
 * 
 * 
 * 
 *                                    M A I N
 * 
 * 
 * 
 * ################################################################################ */

main (int argc, char *argv[])
{

   int i, res;

   res = ScanDataBase ();

   if (res == -1) {             /* FAIL */
      fprintf (stderr, "Could NOT open Database File.. exiting ..\n");
      exit (1);
   }

   res = AllocateMemory ();

   if (res == -1) {             /* FAIL */
      CleanUp ();
      exit (1);
   }
   /* Memory Allocated */
   res = ReadDataBase ();

   if (res == -1) {
      CleanUp ();
      exit (1);
   }

   for (i=0; i < MAXLINES; i++)
        fprintf(stderr," %s\t %s\t %s\t\n",FileEndings[i], Comments[i], DestPath[i]);

   res = Analyze(argv[1]);


   CleanUp();

}

