// UPDATE.C  - Updating-Utility for FiveNine
// Written by Stefan Pfeiffer, DL1ELY, 940917
// Last modification: 960529
// (C) 1994 by Stefan Pfeiffer, DL1ELY

#include <stdio.h>
#include <time.h>
#include <string.h>
#include <dos.h>
#include <dir.h>

#define STATION_NAME "STATIONS.59"
#define PREFIX_NAME  "PREFIXES.59"

#define PRGNAME "FiveNine-Update v1.30, 960529 by Stefan Pfeiffer, DL1ELY@DB0PKE"
#define STATION_HEADER "STATIONS0\r\nStation-Database for FiveNine\r\n(C) 1994-95 by Stefan Pfeiffer, DL1ELY\r\n\x1A"

#define HEADER_SIZE sizeof(struct header)
#define QSO_SIZE sizeof(struct qso)


typedef struct header
{
  unsigned char magic[5];
  unsigned char owncall[14];
  unsigned char ownloc[7];
  unsigned char desc[60];
  unsigned qso_cnt;
  unsigned invalid;
  unsigned ODX;
  unsigned char ODX_call[14];
  unsigned average;
  unsigned char countries;
  unsigned char unknown_pref;
  unsigned char squares;
  unsigned long points;
};
  
typedef struct qso
{
  struct qso *next;
  struct qso *prev;
  
  unsigned hour, min;
  unsigned nr;
  unsigned char barecall[7];
  unsigned char call[14];
  unsigned char locator[7];
  unsigned char rst_RX[8];
  unsigned char rst_TX[4];
  unsigned points;
  unsigned direction;
};

void write_station_header(FILE *f)
{
  unsigned int help = 0;
  unsigned char head[129];

  strcpy(head,STATION_HEADER);
  fwrite((void *)head,1,128,f);
  fwrite((void *)&help,1,2,f);
  fwrite((void *)&help,1,2,f);
};

char convert_stations(char *fname, unsigned char month, unsigned char year)
{
  FILE *inf, *outf;
  unsigned char head[129];
  unsigned char call[13];
  unsigned char loc[7];
  unsigned ilen;
  unsigned char info[256];

  unsigned converted = 0;
  unsigned deleted = 0;


  if((inf = fopen(fname,"rb")) == NULL)
  {
     printf("Nicht gefunden!\n");
     return(0);
  };

  fread((void *)head,1,9,inf);

  if(!strncmp(head,"STATIONS0",9))
  {
    printf("Schon konvertiert!\n");
    fclose(inf);
    return(0);
  };

  fseek(inf,0,SEEK_SET);
  fread((void *)head,1,4,inf);
  if(strncmp(head,"SYS\x1A",4))
  {
    fclose(inf);
    printf("Unbekanntes Format!\n");
    return(0);
  };

  outf = fopen("$$$$$$$$.59","wb");

  write_station_header(outf);

  printf("\n");

  while(!feof(inf))
  {
    memset((void *)call,0,13);
    fread((void *)call,1,7,inf);
    call[6] = 0;
    fread((void *)loc,1,7,inf);
    loc[6] = 0;
    fread((void *)&ilen,1,2,inf);
    info[ilen] = 0;
    if(ilen) fread((void *)info,1,ilen,inf);

    if(!call[0]) deleted++;
    else
    {
      fwrite((void *)call,1,12,outf);
      fwrite((void *)loc,1,6,outf);
      fwrite((void *)&month,1,1,outf);
      fwrite((void *)&year,1,1,outf);
      fwrite((void *)&ilen,1,1,outf);
      if(ilen) fwrite((void *)info,1,ilen,outf);
      converted++;
    };
    printf("\r%5u konvertiert, %3u gel”scht.",converted,deleted);
  };

  printf("\n");

  fseek(outf,128,SEEK_SET);
  fwrite((void *)&converted,1,2,outf);
  deleted = 0;
  fwrite((void *)&deleted,1,2,outf);

  fclose(outf);
  fclose(inf);

  unlink(fname);
  rename("$$$$$$$$.59",fname);

  return(1);
};

void convert_cnt(char *fname)
{
  FILE *inf, *outf;
  struct header h;
  struct qso q;
  char hlp[20];
  unsigned read;

  printf("%12s...",fname);

  inf = fopen(fname,"rb");

  fread((void *)&h,1,HEADER_SIZE,inf);
  if(!strncmp(h.magic,"CNT2\x1A",5))
  {
    fclose(inf);
    printf("Schon konvertiert!\n");
    return;
  };

  outf = fopen("$$$$$$$$.59","wb");

  strcpy(h.magic,"CNT2");
  h.magic[4] = 0x1A;
  fwrite((void *)&h,1,HEADER_SIZE,outf);

  while(!feof(inf))
  {
    read = fread((void *)&q,1,QSO_SIZE,inf);
    if(read == QSO_SIZE)
    {
      fwrite((void *)&q.hour,1,2,outf);
      fwrite((void *)&q.min,1,2,outf);
      fwrite((void *)&q.nr,1,2,outf);
      strcpy(hlp,q.barecall);
      fwrite((void *)hlp,1,13,outf);
      fwrite((void *)q.call,1,14,outf);
      fwrite((void *)q.locator,1,7,outf);
      fwrite((void *)q.rst_RX,1,8,outf);
      fwrite((void *)q.rst_TX,1,4,outf);
      fwrite((void *)&q.points,1,2,outf);
      fwrite((void *)&q.direction,1,2,outf);
    };
  };

  fclose(inf);
  fclose(outf);

  unlink(fname);
  rename("$$$$$$$$.59",fname);
  printf("OK!\n");
};

void make_des(char *name)
{
  char s[80];
  FILE *desf;
  FILE *cntf;
  struct header h;
  char *cp;

  strcpy(s,name);
  cp = strrchr(s,'.');
  *cp = 0;
  strcat(s,".DES");
 
  if((desf = fopen(s,"rt")) != NULL)
  {
    fclose(desf);
  }
  else
  {
    desf = fopen(s,"wt");
 
    cntf = fopen(name,"rb");
    fread((void *)&h,HEADER_SIZE,1,cntf);
    fclose(cntf);
 
    fprintf(desf,"TITLE %s\n",h.desc);
    fprintf(desf,"DATE 010195\n");
    fprintf(desf,"Band1 = %s\n",name);
    fclose(desf);
  };
};

 
void convert_all_cnt(void)
{
  struct ffblk f;
  int result;

  result = findfirst("*.CNT",&f,FA_ARCH);
  while(!result)
  {
    convert_cnt(f.ff_name);
    make_des(f.ff_name);
    result = findnext(&f);
  };
};


void main(int argc, char **argv)
{
  time_t timer;
  struct tm *tblock;

  timer = time(NULL);
  tblock = localtime(&timer);

  printf("%s\n\n",PRGNAME);
  printf("  Konvertiere %s...",STATION_NAME);

  convert_stations(STATION_NAME,tblock->tm_mon+1,tblock->tm_year);

  convert_all_cnt();

  printf("\n");
};

