/* $VER: DiaBTag V1.3 (26 Mar 1994) */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dialog/dlg_pragmas.h>
#include <dialog/user.h>
#include <dialog/file.h>

#define __USE_SYSBASE

#include <proto/exec.h>
#include <proto/dos.h>

#define MINOSVER 37

#define VERSION_NAG \
        "\nYou have neglected your responsibility to upgrade\n"\
        "your system software to version 2.04 or greater!\n\n"

#define DLGERROR \
        "\nDiaMail Error: Can not open dlg.library!\n"

#define PORTERROR \
        "\nDiaMail Error: Can`t find port!\n\n"

#define USERERROR \
        "\nDiaMail Error: Can not find user!\n\n"

#define HEADER \
        "\x1b[1m[1;36mDiaBTag V1.3\x1b[0m -- Move tagged files to FidoNet poll\n"\
        "Written by Janne Saarme 1993/1994\n\n"\
        "Please!!! Read manual again!!\n\n"

__aligned struct FileInfoBlock fib;

struct Batch batchdata;
struct Ram_File rammies;
struct DateTime dat;

static struct Batch buf[100]; /* bufferi */

APTR   DLGBase;

void sulkemiset(void);
void poista(void);

BPTR console, f1, lock;
char c, port[4]="???", title[80], filename[40], point[30], save[40];
long total = 0, f, n, datainbuf;

void main(int argc, char **argv)
{
char daybuf[10], datebuf[10], timebuf[9];

dat.dat_Format=FORMAT_DOS;
dat.dat_StrDay=daybuf;
dat.dat_StrDate=datebuf;
dat.dat_StrTime=timebuf;

if(console=Open("*", MODE_OLDFILE))
   
if(((struct Library *)SysBase)->lib_Version<MINOSVER ||
   DOSBase->dl_lib.lib_Version<MINOSVER) {
     Write(console, VERSION_NAG, sizeof(VERSION_NAG));
     if(console) Close(console); console=0;
     exit(0);
     }
   
if(argc>1) {
   Write(console, HEADER, sizeof(HEADER));
   if(console) Close(console); console=0;
   exit(0);
   }

if (!(DLGBase = OpenLibrary("dlg.library", 0))) {
    Write(console, DLGERROR, sizeof(DLGERROR));
    if(console) Close(console); console=0;
    exit(0);
    }

if (GetDevName(port)==-1) {
    Write(console, PORTERROR, sizeof(PORTERROR));
    sulkemiset();
    }

if(!ReadRam(&rammies, port)) {
   Write(console, USERERROR, sizeof(USERERROR));
   sulkemiset();
   }

PutStr("\fDiaBTag V1.3 -- Written by Janne Saarme 1993/1994");

TGetTitle(title, port);     
TTitle("DiaBTag V1.3 - (C) by Janne Saarme 1993/1994", port);

UnderScore(rammies.Name);
sprintf(save, "USER:%s/Batch.File", rammies.Name);

if(!(f1 = Open(save, MODE_OLDFILE))) {
    PutStr("\n\nNo tagged file(s).\n\n");
    sulkemiset();
    }

PutStr("\n\nThese files have been tagged for batch download:\n\n");

do
{
   for(datainbuf=0, f=1; f && datainbuf<sizeof buf; datainbuf+=f)
      if((f=Read(f1, (char *)buf+datainbuf, sizeof buf-datainbuf))<0)
	 sulkemiset(); /* I/O virhe */

   for(n=0; n<datainbuf/sizeof *buf; n++) {
      total = total + buf[n].size;
        if (buf[n].free == 1) 
          Printf("\033[33m%-16.16s\033[0m (Free)     \033[36m%ld bytes.\033[0m\n", buf[n].filename, buf[n].size);
        else
           {
           Printf("\033[33m%-16.16s\033[0m (Ratio)    \033[36m%ld bytes.\033[0m\n", buf[n].filename, buf[n].size);
           }
      if(CheckSignal(SIGBREAKF_CTRL_C)) sulkemiset();
      }

    if(CheckSignal(SIGBREAKF_CTRL_C)) sulkemiset();
}
while(datainbuf==sizeof buf); /* jos bufferi jäi vajaaksi, niin file on lopussa */

if (f1) Close(f1); f1=0;

Printf("\n\033[34m                     Total: %ld bytes.\033[0m\n", total);

FPuts(console, "\n\033[33mGive your node address (like 2.220.550.0) or return QUIT:\033[37m ");
FGets(console, point, 28); 
point[strlen(point)-1] ='\0';
if (strlen(point) < 5) poista();   
sprintf(filename, "Outbound:%s.HLO", point);

if(Exists(filename)) {
  if(lock=Lock(filename, ACCESS_READ)) {
    if(Examine(lock, &fib)) {
       dat.dat_Stamp=fib.fib_Date;
       if(DateToStr(&dat))
       Printf("\n\033[35mYou have outgoing packet(s). Creating date is \033[36m%-9.9s.\033[0m\n", datebuf);
       }
    UnLock(lock);
  }
   
   FPuts(console, "\n\033[33mDelete ALL outgoing packet(s) (before tagged files) (y/N)?\033[37m ");
   c = FGetC(console); 
     switch(c) {
       case 'y':
       DeleteFile(filename);
       break; 
      }

}

if(!(f1 = Open(filename, MODE_READWRITE))) sulkemiset();

Seek(f1, 0, OFFSET_END);

for(n=0; n<datainbuf/sizeof *buf; n++) FPrintf(f1, "%s%s\n", buf[n].path, buf[n].filename); 

poista();
}

void sulkemiset(void)
{
TTitle(title, port);
if (f1) Close(f1);
if (DLGBase) CloseLibrary(DLGBase);
if (console) Close(console);
exit(0);
}

void poista()
{
FPuts(console, "\n\033[33mDo you want to delete tagged file(s) (y/N)?\033[37m ");
c = FGetC(console); 
    switch(c) {
      case 'y':
      DeleteFile(save);
      if(CheckSignal(SIGBREAKF_CTRL_C)) sulkemiset();
      break; 
      }
    
PutStr("\n"); 
sulkemiset();
}

