/* apccomm_am_main.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_am_main.c,v 1.4 2001/08/26 00:29:37 ralf Exp $ */

#include "apccomm_am.h"
#include "apccomm_am_tr.h"
#include "apccomm_all.h"

int main(int argc,char **argv)
{
  int s1;
  int pos=0;
  int o1,o2,o3;
  short *realargs;
  
  int sendargs=argc-1;      /* Wenn es mal Argumente gibt, die nicht das
                               das Ende des Programmes verursachen... */

  realargs=(short*)malloc(sizeof(short)*argc);
  for(s1=0;s1<argc;s1++) {
    realargs[s1]=1;
  }
  if(argc>1) {
    for(s1=1;s1<argc;s1++) {
      if((strncmp(argv[s1],"-h",2)==0)||(strncmp(argv[s1],"--help",6)==0)) {
        printf("\nAPCComm by Ralf Hoffmann\n");
        printf("  Usage: %s [Option]... [<filename>]...\n  Transfer given files to the PC\n\n",argv[0]);
        printf("   -h, --help\t\tShow this help\n");
        printf("   -v, --version\tShow program-version\n");
        printf("   -V, --verbose\tverbose output\n");
        printf("   -q, --quiet\t\tno output (except errors)\n");
        printf("   -ip\t\t\tignore file protection for receiving\n");
        printf("  When starting without args, APCComm will only receive a file from the PC\n");
        exit(0);
      } else if((strncmp(argv[s1],"-v",2)==0)||(strncmp(argv[s1],"--version",6)==0)) {
        printf("\nAPCComm by Ralf Hoffmann\n  Executable for Amiga-Side\n  Version %d.%d.%d\n",MAJOR,MINOR,PATCH);
        printf("\n  Programm for transfering files between Amiga and PC\n");
        printf("  Contact: ralf.hoffmann@epost.de\n");
        exit(0);
      } else if( ( strncmp(argv[s1],"--verbose",strlen("--verbose"))==0 ) ||
		 ( strncmp(argv[s1],"-V",strlen("-V"))==0 ) ) {
	verbose = 1;
	sendargs--;
	realargs[s1]=0;
      } else if( ( strncmp(argv[s1],"--quiet",strlen("--quiet"))==0 ) ||
		 ( strncmp(argv[s1],"-q",strlen("-q"))==0 ) ) {
	verbose = -1;
	sendargs--;
	realargs[s1]=0;
      } else if( strncmp(argv[s1],"-ip",strlen("-ip"))==0 ) {
	ignore_prot = 1;
	sendargs--;
	realargs[s1]=0;
      }
    }
  }

  if(init_transferblocks()!=0) {
    fprintf(stderr,"Not enough mem in main()!\nExiting\n");
    exit(1);
  }

  /* Parallelport initieren */
  *ddrb=0xff;
  *ddra=0x00;
  if ( ! BE_QUIET ) printf("Waiting for PC:");
  fflush(stdout);
  synchro();
  if ( ! BE_QUIET ) printf("\nConnection established\n");

  /* Prepare the outblock */
  putint(outblock,MAJOR);
  putint(outblock+4,MINOR);
  putint(outblock+8,PATCH);
  if(sendargs>0) putint(outblock+12,1);
  else putint(outblock+12,0);

  /* Inform the other side */
  transferblock();
  
  /* Now check the other version */
  o1=getint(inblock);
  o2=getint(inblock+4);
  o3=getint(inblock+8);
  if( (o1!=MAJOR) || (o2!=MINOR) || (o3!=PATCH) ) {
    printf("Wrong version at the other side!\n");
    printf("  Expected %d.%d.%d ,  got %d.%d.%d\n",MAJOR,MINOR,PATCH,o1,o2,o3);
    exit(1);
  } 
  
  /* Version ok, sending conflict ? */
  o1=getint(inblock+12);
  if( (o1==1) && (sendargs>0) ) {
  	printf("Both sides want to send!\n");
  	printf("  This is not supported!\n");
  	exit(1);
  }
  
  if( (o1==0) && (sendargs==0) ) {
  	printf("Nothing to do!\n");
  	exit(1);
  }
  
  if(sendargs>0) {
    send(argc,argv,realargs);
  } else {
    receive();
  }

  free(inblock);
  free(outblock);
  free(realargs);
  return 0;
}

