// SplitIndex.c
//
// Program to split aminet index into several subindexes, like so:
//
// Index => out.biz, out.comm, ... out.wb
//
// only runs from CLI, if run from WB, returns without doing anything

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

#define LINESIZE    81          // max size of line in index
#define DIRSIZE     5           // max size of aminent directory +1 - e.g.:
                                // MODS, DIR, WB, COMM

int main (int argc, char *argv[]);
void __regargs _CXBRK(void);    // redefine SAS/C ctrl-c handling


FILE    * indexfile, * outfile = NULL;

int main(int argc, char *argv[])
{
    char    line [LINESIZE], dirold [DIRSIZE], dirnew [DIRSIZE], filename[255];
    char    *q;
    int     ok =0, i;
    extern  int __buffsize;     // SAS/C data name - size of io buffer

    /* make the buffer size high as this program is IO bound */
    __buffsize = 65536;

    /* Check if we were started from WB */
    if (argc == 0)
        /***** Started from WB *****/
        exit(10);

    if(((!argv[1]) || (!argv[2])) || argv[3])
    {
        /* incorrect arguments - display command format! */
        printf("\nUsage: SplitIndex <INDEXFILE> <OUTFILE>\n\n");
        printf("Where INDEXFILE is the name of the aminet index to split and\n");
        printf("OUTFILE is the prefix for the sub indexes to be created, eg:\n\n");
        printf("SplitIndex INDEX output/List\n\n");
        exit(10);
    }

    if (!(indexfile=fopen(argv[1],"r")))
    {
        printf ("\nError!  I couldn't find the Index file...\n\n");
        exit(10);
    }

    /* Move file pointer past aminet list header info */
    fgets (line,LINESIZE,indexfile);

    while (*line=='|')
    {
        fgets (line,LINESIZE,indexfile);
    }

    /* Now pointing to start of aminet listings */

    printf("\n");
    while (!ok)  // loop thru list line by line
    {
        fgets (line,LINESIZE,indexfile);

        /* end of file? */
        if (! feof(indexfile))
        {
            /* copy DIR type (comm,dir,wb etc...) to dirnew */

            /* NB: the check for the space is there because I discovered an **
            ** INDEX file with an entry under "gfx" only, not "gfx/xxx", which **
            ** caused an error... The check stops this occurance from crashing **
            ** the machine, or producing another list.gfx file due to the anomoly */
            for (i=0, q=&line[19]; !(*q=='/' || *q==' ') ; q++, i++)
            {
                dirnew[i]=*q;
            }
            dirnew[i]=NULL;     // add NULL-terminator
        }
        else
            ok = 1;    // quit while loop

        /* different DIR type? */
        if (strcmp(dirnew,dirold) != 0)
        {
            /* close old output file */
            if (outfile) fclose(outfile);

            /* make new filename */
            strcpy (filename,argv[2]);
            strcat (filename,".");
            strcat (filename,dirnew);

            printf("Processing output for %s\n",filename);

            /* open new file */
            if(!(outfile=fopen(filename,"w")))
            {
                /* can't open output file - cleanup & exit */
                printf("\nError!  I Couldn't open file %s\n\n",filename);
                fclose(indexfile);
                exit(10);
            }
            /* make dirold = dirnew */
            strcpy(dirold,dirnew);
        }
        /* write item to outfile */
        fprintf(outfile,"%s",line);
    }

    /* cleanup */
    fclose(indexfile);
    printf("\nAll done!\n");
    exit(0);
}


/*
** redefine ctrl-c handling
*/
void __regargs _CXBRK(void)
{
    /* break! */
    if (outfile)
        fclose(outfile);

    if (indexfile)
        fclose(indexfile);

    printf ("\n*** BREAK ***\n\n");

    exit(5);
}
