/**********************************************************************
* PROGRAM DESCRIPTION : generate input files to PC version of old2new
* INPUT : 3 character station code
* OUTPUT: file/files of filenames which begin with the 3 character code;
*         within each file, filenames are in order of increasing month
*                           based on a filename of SSSYYMM.xxx
* USAGE : group SSS
* AUTHOR : Chris Wells
* DATE   : November 14, 1989
* MACHINE DEPENDENCIES : _dos_findfirst, _dos_findnext
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <fcntl.h>
#include <dos.h>

main( argc,argv )
int argc;
char *argv[];
{
    static char template[32],
                filename[32],
                currentyear[2],
                year[2],
                outfile[32];
    FILE *tmp,
         *out;
    struct find_t DOS_info;

    /***  get the command line parameters  ***/
    if ( argc < 2 ) {
        printf("USAGE: group SSS\n");
        exit(1);
        }
    if ( strlen(argv[1]) != 3 ) {
        printf("ARGUMENT ERROR : station code not 3 characters long\n");
        exit(1);
        }

    /***  open a temporary file  ***/
    tmp = fopen( "________.TM1","w" );

    /***  construct a DOS filename template based on station code ***/
    strcpy( template,argv[1] );
    strcat( template,"*.TXT" );

    /***  find all filenames matching the SSS template ***/
    if ( _dos_findfirst( template,_A_NORMAL,&DOS_info ) != 0 ) {
        printf("no files matching the SSS template found\n");
        exit(1);
        }
    fprintf(tmp,"%s\n",DOS_info.name );
    while ( _dos_findnext(&DOS_info) == 0 )
        fprintf(tmp,"%s\n",DOS_info.name );
    fclose( tmp );

    /***  call operating system to sort the filenames ***/
    system( "sort < ________.TM1 > ________.TM2" );

    /****************************************************/
    /***  put each year of files into a separate file ***/
    /****************************************************/

    /*** initialize with the first file ***/
    tmp = fopen( "________.TM2","r" );
    fgets(filename,32,tmp);
    strncpy( currentyear,&filename[3],2 );
    sprintf( outfile,"%s.%2.2s",argv[1],currentyear );
    out = fopen( outfile,"w" );
    printf("currently writing to file %s\n",outfile );

    /*** process all files ***/
    rewind(tmp);
    while ( fgets(filename,32,tmp) ) {
        strncpy( year,&filename[3],2 );
        /***  filename is same year - put in current output file ***/
        if ( ! strncmp(year,currentyear,2) )
            fprintf( out,"%s",filename );
        else {
            /***  starting new year - close old and open new ***/
            fclose( out );
            strncpy( currentyear,&filename[3],2 );
            sprintf( outfile,"%s.%2.2s",argv[1],currentyear );
            out = fopen( outfile,"w" );
            fprintf( out,"%s",filename );
            printf("currently writing to file %s\n",outfile );
            }
        }

    fclose( tmp );
    fclose( out );
    unlink( "________.TM1" );
    unlink( "________.TM2" );
}