#include "filedoor_structs_defines.h"
#include "filedoor_protos.h"

#include <proto/exec.h>
#include <proto/dos.h>
#include <exec/memory.h>
#include <strings.h>
#include <stdio.h>
#include <ctype.h>

/* Download taggede filer */
void Download(void)
{
static __far struct Areamem *lista[100];
static __far struct Filemem *listf[100];
static __far int             listi[100];
static __far char *          listd[100];

struct Filemem *file;

static char key[4];
__far static char tempstr[80];
char *tempstr2;
char *namebuf;
int i,j,k;
int sum=0;
int actualfiles;

char *prefs;

int filesfordl=0;

  SaveAttrs();
  prefs=FindAttr('R');
  if(namebuf=AllocVec(50*100,MEMF_ANY))
  {
    if(taggedfiles==0) return;
    msg(FG7"\xc""Files tagged for download:\n\n");
    j=0;
    for(i=0;i<NumAttrs;i++)
    {
      if(Attrs[i][0]=='T')
      {
        if(!strnicmp(GetNthOccur(Attrs[i],':',2)+1,superarea,strlen(superarea)))
        {
          /* Skaf de informationer vi skal bruge */
          lista[j]=AttrPtrs[i];
          FindFiles(lista[j]);
          listi[j]=i;
          strncpy(tempstr,Attrs[i]+1,32);
          for(k=0;tempstr[k]!=':';k++);
          tempstr[k]=0;
          listf[j]=FindFileInArea(tempstr,lista[j]);
          listd[j]=namebuf+100*j;
          strcpy(listd[j],lista[j]->area.path);
          AddPart(listd[j],listf[j]->f.name,100);
          filesfordl++;
          /* Udregn en lille sum af bytes */
          sum+=listf[j]->f.siz;
          /* Skriv filnavnet */
          sprintf(tempstr,FG3"%-32.32s : "FG2"%-32.32s\n",lista[j]->area.name,listf[j]->f.name);
          msg(tempstr);
          /* Videre */
          j++;
          /* Marker evt en readme fil osse */
          if(toupper(prefs[0])=='Y' && listf[j-1]->f.flags&FILEFLAG_LONGFILE)
          {   
            listf[j]=NULL;
            lista[j]=NULL;
            listi[j]=0;
            listd[j]=namebuf+100*j;
            strcpy(listd[j],lista[j-1]->area.path);
            AddPart(listd[j],listf[j-1]->f.longdesc,100);
            filesfordl++;
          }
        }
      }
    }
    sprintf(tempstr,FG7"\nTotally %i bytes in %i files\n",sum,taggedfiles);
    msg(tempstr);
    if(toupper(prefs[0])=='Y')
    {
      msg("Downloading long descriptions\n");
    }
    else
    {
      msg("Not downloading long descriptions\n");
    }
    msg("Using protocol ");
    tempstr2=FindAttr('X');
    if(tempstr2==NULL) tempstr2="xprzmodem.library";
    msg(tempstr2);
    sprintf(tempstr,"\nDownloads allowed before upload: %i\n\n",availfiles);
    msg(tempstr);
    if(taggedfiles>availfiles)
    {
      hotkey("Whooooooops! Better upload!",key);
      key[0]='A';
    }
    else
    {
      hotkey(MENU"Select: "KNAP"(d)"MENU" Download "KNAP"(A)"MENU" Abort",key);
    }
    if(key[0]=='D' || key[0]=='d')
    {
      msg(FG7 "\nDownloading.\n");
      PurgeAllFiles();
      actualfiles=DoXprDl(filesfordl,listd);
      downloadedfiles+=actualfiles;
      availfiles-=actualfiles;
      changeuserint(10,downloadedfiles);
      msg("\xc""Updating download counters");
      if(!hetwittedus)
      {
        for(i=taggedfiles-1;i>=0;i--)
        {
          msg(".");
          if(lista[i]!=NULL && i<actualfiles)
          {
            file=FindFileInArea(FilePart(listd[i]),lista[i]);
            file->f.dl++;
            lista[i]->dirty=TRUE;
            RemAttr(listi[i]);
            taggedfiles--;
          }
        }
        PurgeAllFiles();
        msg("done!\n");
        AnyKey();
      }
    }
  }
  FreeVec(namebuf);
  FindFiles(activearea);
}
