/* apccomm_pc_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_pc_main.c,v 1.5 2001/08/26 00:29:57 ralf Exp $ */

#include "apccomm_pc.h"
#include "apccomm_pc_tr.h"

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

  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 amiga\n\n",argv[0]);
        printf("   -h, --help\t\tShow this help\n");
        printf("   -v, --version\tShow program-version\n");
	printf("   --port=<port>\tuse parallel port <port>\n");
	printf("   \t\t\t<port> can be lpt1 or lpt2\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 Amiga\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 PC-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],"--port=",strlen("--port="))==0) {
        if(strcmp(argv[s1],"--port=lpt1")==0) useport=LPT1;
	else if(strcmp(argv[s1],"--port=lpt2")==0) useport=LPT2;
	else {
	  printf("No valid port, possibilities are lpt1 or lpt2\n");
	  exit(0);
	}
	sendargs--;
	realargs[s1]=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;
      } else {
        outarg=argv[s1];
      }
    }
  }

  if(init_transferblocks()!=0) {
    fprintf(stderr,"Not enough mem in main()!\nExiting\n");
    exit(1);
  }
  
  if (pin_init_user(useport) < 0) {
    fprintf( stderr, "can't initialize parallelport!\n" );
    exit(0);
  }
  /* drop root privileges needed for access to parport */
  setuid(getuid());
  setgid(getgid());

  pin_output_mode(LP_DATA_PINS);
  if ( ! BE_QUIET ) printf("Waiting for Amiga:");
  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);
  return 0;
}

