/* ============================================================= */
/*  Rob Hamerling's MAXIMUS download file scan and sort utility  */
/* ============================================================= */

// #define DEBUG_MODE

#define INCL_BASE
#include <os2.h>

#include "downsort.h"

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

/* Global variables definitions all in mainline-module      */
/* Submodules will get external declarations via downsort.h */

char PROGNAME[]   = "DOWNSORT";         // program name (and filenames)
char VERSION      = 4;                  // current version
char SUBVERS      = 7;                  // current release
char SUFFIX       = ' ';                // development suffix
char PROGDESC[]   = "DOWNload file SORT and list utility";
char AUTHOR[]     = "Rob Hamerling";    // program author
char CITY[]       = "Vianen, The Netherlands";
char PHONE[]      = "Phone: 31.3473.72136";
char FIDO[]       = "Fido-net: 2:512/4.1098";
char MAX[]        = "MAXIMUS CBCS";
char MSG_MEM[]    = "\nInsufficient memory, %s terminated\n";
char MSG_SRT[]    = "\nSorting %u files in %u directories for %s";
char MSG_RST[]    = "\nRe-sorting first %u entries on filename";
char MSG_TRL[]    = "\n%sfile - %s - not found!";
char MSG_REC[]    = "\n      file entries written to: %s";
char OPEN_FAIL[]  = "\nCould not open file: %s\n";
char HS[]         = "陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳陳";
char HD[]         = "様様様様様様様様様様様様様様様様様様様様";
char FN[]         = "Filename";
char AC[]         = "Area";
char DT[]         = "Date";
char SZ[]         = "Size";
char DS[]         = "Description";
char FP[]         = "FilePath";
char EMPTY[]      = "";                 // empty string
char NDS[46]      = "--- no description available ---";
char ORPHAN[46]   = "************ ORPHAN ************";
char BAK[]        = "BAK";              // backup file extension
char CFG[]        = "CFG";              // config      "
char *priv_name[] = {"Twit"," ","Disgrace","Limited","Normal",
                    "Worthy","Privil","Favored","Extra","Clerk",
                    "AsstSysop"," ","Sysop","Hidden"};

char today[26];                         // output timestamp
char list_title[21]    = "";            // ALL/NEW/GBL list title
char *pre_title[MAXTIT]= {NULL,NULL,NULL,NULL,NULL,  // ptrs to pretitles
                          NULL,NULL,NULL,NULL,NULL};
char *sub_title[MAXTIT]= {NULL,NULL,NULL,NULL,NULL,  // ptrs to subtitles
                          NULL,NULL,NULL,NULL,NULL};
char *bot_lines[MAXTIT]= {NULL,NULL,NULL,NULL,NULL,  // ptrs to bottomlines
                          NULL,NULL,NULL,NULL,NULL};
USHORT title_lines[]   = {0,3,5,4,5};   // # of lines per title-font
                                        // See DOWNFNT.C for 'real' table.
char areadat_path[128] = "AREA.DAT";    // (default) AREA.DAT filespec
char cfg_path[128]     = "DOWNSORT.CFG";// (default) config filespec
char filesbbs_path[128]= "";            // path to FILES.bbs output
char oper_mode         = ' ';           // V: verbose: talk a lot
                                        // Q: only error messages
                                        // H: display help-screen
USHORT area_count      = 0;             // number of download directories
USHORT file_count      = 0;             // counter for number of files
ULONG  byte_count      = 0;             // counter for file sizes
int    MAX_level       = 0;             // version of MAX from AREA.DAT
struct _filechain *first_element = NULL; // ptr first chain element
struct _COUNTRYCODE c_code;             // country code buffer
struct _COUNTRYINFO c_info;             // country information buffer

struct _listparm lp[]  = {
      {                                 // BBS-list (P_BBS)
       {HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,
        HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1},
        FONT2, 65535, TIMESTAMP, TRUNC, "DOWNSORT", "BBS"},

      {                                 // FILES.BBS (P_FIL)
       {HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,
        HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1},
        FONT2, 65535, ALPHA, TRUNC, "FILES", "BBS"},

      {                                 // GBL-list (P_GBL)
       {HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,
        HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1},
        FONT2, 65535, ALPHA, WRAP, "DOWNSORT", "G~"},

      {                                 // NEW-list (P_NEW)
       {HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,
        HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1},
        FONT2, 65535, TIMESTAMP, TRUNC, "DOWNSORT", "N~"},

      {                                 // ALL-list (P_ALL)
       {HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,
        HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1},
        FONT2, 65535, ALPHA, WRAP, "DOWNSORT", "A~"},

      {                                 // ORP-list (P_ORP)
       {HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,
        HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1,HIDDEN+1},
        FONT2, 65535, ALPHA, TRUNC, "DOWNSORT", "ORP"},
         };

/* ====================== */
/*   M A I N    L I N E   */
/* ====================== */
int  main(argc,argv)
int  argc;
char *argv[];
{
  struct  _downpath  *area;             // pointer to area-info arrays
  struct  _filechain **dm;              // pointer to file-sort array
  USHORT   i;                           // counter
  long     start_time,run_time;         // for execution time measurement

  start_time = time(NULL);              // system time at start
  sprintf(list_title,"%s%d%d%c",PROGNAME,VERSION,SUBVERS,SUFFIX);
                                        // default title
  get_parm(argc,argv);                  // system and oper. parameters
                                        // and display welcome msg
  area_count = collect_down(&area);     // build arrays with area-info

  file_count = collect_file(area_count,area);  // collect all file info

  if (file_count == 0) {                // no files
    printf("\nNot a single file found in any download directory, or");
    printf("\nno download paths found at all, %s terminated\n",PROGNAME);
    exit(10);
    }

  dm = prep_sort(file_count,first_element); // make sort array

  make_orp(dm);                         // Orphan report

  if (lp[P_BBS].priv[0] <= HIDDEN)      // BBS-list
    make_bbs(dm,lp[P_BBS].max_fil,lp[P_BBS].priv[0]);

  for (i=0; i<10 && lp[P_NEW].priv[i] <= HIDDEN; i++) // NEW-list(s)
    make_new(dm,lp[P_NEW].max_fil,lp[P_NEW].priv[i]);

  for (i=0; i<10 && lp[P_GBL].priv[i] <= HIDDEN; i++) // GBL-list(s)
    make_gbl(dm,lp[P_GBL].priv[i]);

  for (i=0; i<10 && lp[P_ALL].priv[i] <= HIDDEN; i++) // ALL-list(s)
    make_all(dm,area,lp[P_ALL].priv[i]);

  if (lp[P_FIL].priv[0] <= HIDDEN)      // FILES.BBS
    make_fil(dm,lp[P_FIL].priv[0]);

  if (oper_mode != QUIET)
    printf("\n\n%s version %d.%d%c by %s ",
             PROGNAME,VERSION,SUBVERS,SUFFIX,AUTHOR);
  run_time = time(NULL) - start_time;   // execution time in seconds
  printf("completed in %ld minutes and %ld seconds.\n\n",
          run_time/60,run_time%60);     // report execution time

  return(0);                            // OS will release all storage!
  }

