/* apccomm_all.c
   Part of "APCComm" 
   Copyright (C) 2000,2001 Ralf Hoffmann
   Contact: ralf.hoffmann@epost.de

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
*/
/* $Id: apccomm_all.c,v 1.5 2002/06/11 00:48:46 ralf Exp $ */

#include "apccomm_all.h"

#if defined AMIGA && defined GUI
#include "apccomm_gui_includes.h"
#include "apccomm_gui.h"
#include "apccomm_am.h"
#endif

unsigned char *outblock = NULL, *inblock = NULL;
pc_file_t outfile;
pc_file_t infile;

char *basedir = NULL;
int verbose = 0;
int ignore_prot = 0;

int init_transferblocks( void )
{
  int i;

  outblock=(unsigned char*)malloc(TRANSFERBLOCKSIZE);
  inblock=(unsigned char*)malloc(TRANSFERBLOCKSIZE);
  if(outblock!=NULL)
    for(i=0;i<TRANSFERBLOCKSIZE;i++) outblock[i]=0;
  if(inblock!=NULL)
    for(i=0;i<TRANSFERBLOCKSIZE;i++) inblock[i]=0;
  if((inblock==NULL)||(outblock==NULL)) return 1;
  return 0;
}

int init_outgoing_file(const char*name)
{
#ifdef USE_OPEN
  int fp;
#else
  FILE *fp;
#endif
  FILE *tfp;
  int size;
  
  outfile.size=0;
  outfile.checksum=0;
#ifdef USE_OPEN
  outfile.fp=0;
#else
  outfile.fp=NULL;
#endif
  
  tfp=fopen(name,"r");
  if(tfp!=NULL) {
    fseek(tfp,0,SEEK_END);
    size=ftell(tfp);
    fseek(tfp,0,SEEK_SET);
    outfile.size=size;
#ifdef USE_OPEN
    fp=open(name,O_RDONLY);
    fclose(tfp);
#else
    fp=tfp;
#endif
    outfile.fp=fp;
  } else return 1;
  outfile.name=strdup(name);
  return 0;
}

int init_incoming_file(const char*name)
{
  FILE *tfp;
#ifdef USE_OPEN
  int fp;
#else
  FILE *fp;
#endif
  char newname[1024],*basename,*tstr=NULL;
#ifndef GUI
  char choose[16];
#endif
  int fileok;
#if defined AMIGA && defined GUI
  int res;
#endif
  
  infile.size=0;
  infile.checksum=0;
#ifdef USE_OPEN
  infile.fp=0;
#else
  infile.fp=NULL;
#endif

  if(basedir!=NULL) {
    basename=(char*)malloc(strlen(basedir)+1);
    strcpy(basename,basedir);
  } else {
    basename=strdup("");
  }
  
  strcpy(newname,name);
  fileok=0;
  do {
    if(tstr!=NULL) free(tstr);
    tstr=(char*)malloc(strlen(basename)+strlen(newname)+1);
    sprintf(tstr,"%s%s",basename,newname);
    tfp=fopen(tstr,"r");
    if(tfp!=NULL) {
      fclose(tfp);
#if defined AMIGA && defined GUI
      res = rtEZRequest( "File %s already exists!", "Overwrite|Change name|Skip|Abort", NULL, (struct TagItem *)rttags, tstr );
      if ( res == 1 ) {
        fileok = 1;
      } else if ( res == 3 ) {
        fileok = -1;
      } else if ( res == 0 ) {
        fileok = -2;
      } else {
        if ( ! rtGetStringA( newname, 256, "Enter new name:", NULL, (struct TagItem *)rttags ) ) {
          fileok = -2;
        }
      }
#else
      printf("File %s already exists!\nOverwrite (o), change (c) name, skip (s) or abort (a):",tstr);
      fflush(stdout);
      scanf("%s",choose);
      if(choose[0]=='o') {
        fileok=1;
      } else if ( choose[0] == 's' ) {
	fileok = -1;
      } else if ( choose[0] == 'a' ) {
	fileok = -2;
      } else {
        printf("Enter new name:");
        fflush(stdout);
        scanf("%s",newname);
      }
#endif
    } else fileok=1;
  } while(fileok==0);
  free( basename );
  if ( fileok < 0 ) {
    free( tstr );
    return fileok;
  }
#ifdef USE_OPEN
  fp=open(tstr,O_WRONLY|O_CREAT|O_TRUNC,0644);
  infile.fp=fp;
  if(fp<0) {
    free(tstr);
    return 1;
  }
#else
  fp=fopen(tstr,"w");
  infile.fp=fp;
  if(fp==NULL) {
    free(tstr);
    return 1;
  }
#endif
  infile.name=strdup(newname);
  free(tstr);
  return 0;
}

void close_outgoing_file( void )
{
#ifdef USE_OPEN
  if(outfile.fp!=0) close(outfile.fp);
#else
  if(outfile.fp!=NULL) fclose(outfile.fp);
#endif
}

void close_incoming_file( void )
{
#ifdef USE_OPEN
  if(infile.fp!=0) close(infile.fp);
#else
  if(infile.fp!=NULL) fclose(infile.fp);
#endif
}

void putint(unsigned char *buffer,int value)
{
  int b3,b2,b1,b0;
  b0=value&0xff;
  value>>=8;
  b1=value&0xff;
  value>>=8;
  b2=value&0xff;
  value>>=8;
  b3=value&0xff;
  *buffer++=(unsigned char)b3;
  *buffer++=(unsigned char)b2;
  *buffer++=(unsigned char)b1;
  *buffer++=(unsigned char)b0;
}

int getint(unsigned char *buffer)
{
  int value;
  value=(int)*buffer++;
  value<<=8;
  value|=(int)*buffer++;
  value<<=8;
  value|=(int)*buffer++;
  value<<=8;
  value|=(int)*buffer++;
  return value;
}

int readtobuffer(unsigned char* buffer,int maxsize,int *return_checksum)
{
  int readed,checksum,i;
#ifdef USE_OPEN
  readed=read(outfile.fp,buffer,maxsize);
#else
  readed=fread(buffer,1,maxsize,outfile.fp);
#endif
  checksum=0;
  for(i=0;i<readed;i++) checksum+=(int)buffer[i];
  outfile.checksum+=checksum;
  if(return_checksum!=NULL) *return_checksum=checksum;
  return readed;
}

int writefrombuffer(unsigned char *buffer,int size,int *return_checksum)
{
  int writed,checksum,i;
#ifdef USE_OPEN
  writed=write(infile.fp,buffer,size);
#else
  writed=fwrite(buffer,1,size,infile.fp);
#endif
  checksum=0;
  for(i=0;i<size;i++) checksum+=buffer[i];
  infile.checksum+=checksum;
  if(return_checksum!=NULL) *return_checksum=checksum;
  return writed;
}

int min(int a, int b)
{
  if ( a < b ) return a;
  return b;
}
