/*  BuildRECENT.c   --  Copyright © Jan. 20, 1995 by Travis Pascoe

Program will build a 'RECENT' index file from the master AmiNET
INDEX file according to the args provided.  WARNING:  The regular
INDEX file (pub/aminet/INDEX) will NOT work!  Use the file
pub/aminet/info/index/INDEX because it contains a 'days old'
column.

Usage:  BuildRECENT <AmiNET_indexfile> <x> [<y>]

 <x>    Program will output all files uploaded less than or equal
        to <x> days ago.  If <x> is negative, only files uploaded
        exactly <x> days ago will be output.

[<y>]   Only optional parameter.  If <y> is specified only files
        uploaded between <x> and <y> days ago are output.
   
*/

#include <stdio.h>

main(int ac, char **av)
{
    FILE *fp;
    char line[300];
    int  days_x, days_y, days_old, i;

    if ((ac != 3) && (ac != 4)) {
      printf("BuildRECENT  Copyright © Jan. 20, 1995 by Travis Pascoe\n");
      printf("Usage:  %s <AmiNET_indexfile> <x> [<y>]\n", av[0]);
      printf("\nProgram will build a 'RECENT' index file from the master AmiNET\n");
      printf("INDEX file according to the args provided.  WARNING:  The regular\n");
      printf("INDEX file (pub/aminet/INDEX) will NOT work!  Use the file\n");
      printf("pub/aminet/info/index/INDEX because it contains a 'days old'\n");
      printf("column.\n\n");
      printf("Usage:  %s <AmiNET_indexfile> <x> [<y>]\n\n", av[0]);
      printf(" <x>    Program will output all files uploaded less than or equal\n");
      printf("        to <x> days ago.  If <x> is negative, only files uploaded\n");
      printf("        exactly <x> days ago will be output.\n\n");
      printf("[<y>]   Only optional parameter.  If <y> is specified only files\n");
      printf("        uploaded between <x> and <y> days ago are output.\n\n");
      exit(10);
    }
    fp = fopen(av[1], "r");
    if (fp == NULL) {
      printf("ERROR:  Unable to open index file:  %s\n", av[1]);
      exit(10);
    }
    for(i = 0; i <= 6; i++) fscanf(fp, "%*[^\n]");   /*skip header*/
    sscanf(av[2], "%d", &days_x);
    if (ac == 4) {
      sscanf(av[3], "%d", &days_y);
      days_y = abs(days_y);
      days_x = abs(days_x);
      if (days_y < days_x) {
        i = days_y;
        days_y = days_x;
        days_x = i;
      }
      while (fscanf(fp, "%[^\n]", line) == 1) {
        sscanf(line, "%*37c%d", &days_old);
        if ((days_old >= days_x)&&(days_old <= days_y)) printf("%s\n", line);
      }
      fclose(fp);
      exit(0);
    }
    if (days_x >= 0)
      while (fscanf(fp, "%[^\n]", line) == 1) {
        sscanf(line, "%*37c%d", &days_old);
        if (days_old <= days_x) printf("%s\n", line);
      }
    else {
      days_x = abs(days_x);
      while (fscanf(fp, "%[^\n]", line) == 1) {
        sscanf(line, "%*37c%d", &days_old);
        if (days_old == days_x) printf("%s\n", line);
      }
    }
    fclose(fp);
}
