/* 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.7 2002/06/11 00:53:34 ralf Exp $ */

#include "apccomm_pc.h"
#include "apccomm_pc_tr.h"
#include <getopt.h>

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

  while ( 1 ) {
    int option_index;
    static struct option long_options[] = {
      { "help", no_argument, NULL, 'h' },
      { "version", no_argument, NULL, 'v' },
      { "verbose", no_argument, NULL, 'V' },
      { "quiet", no_argument, NULL, 'q' },
      { "ignore_protection", no_argument, NULL, 'i' },
      { "port", required_argument, NULL, 0 },
      { NULL, 0, NULL, 0 }
    };

    c = getopt_long( argc, argv, "hvVqi", long_options, &option_index );
    if ( c == -1 ) break;

    switch( c ) {
    case 0:
      if ( strcmp( long_options[option_index].name, "port" ) == 0 ) {
	if ( optarg != NULL ) {
	  if ( strcmp( optarg, "lpt1" ) == 0 ) useport = LPT1;
	  else if ( strcmp( optarg, "lpt2" ) == 0 ) useport = LPT2;
	  else {
	    fprintf( stderr, "No valid port, possibilities are lpt1 or lpt2\n" );
	    exit( 1 );
	  }
	}
      }
      break;
    case 'h':
      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\t\tShow this help\n" );
      printf( "   -v, --version\t\tShow program-version\n" );
      printf( "   --port=<port>\t\tuse parallel port <port>\n" );
      printf( "   \t\t\t\t<port> can be lpt1 or lpt2\n" );
      printf( "   -V, --verbose\t\tverbose output\n" );
      printf( "   -q, --quiet\t\t\tno output (except errors)\n" );
      printf( "   -i, --ignore_protection\tignore file protection for receiving\n" );
      printf( "  When starting without args, APCComm will receive files from the Amiga\n" );
      exit( 0 );
      break;
    case 'v':
      printf( "\nAPCComm by Ralf Hoffmann\n  Version %d.%d.%d\n", MAJOR, MINOR, PATCH );
      printf( "\n  Program for transfering files between Amiga and PC\n" );
      printf( "  Contact: ralf.hoffmann@epost.de\n" );
      exit( 0 );
      break;
    case 'V':
      verbose = 1;
      break;
    case 'q':
      verbose = -1;
      break;
    case 'i':
      ignore_prot = 1;
      break;
    case '?':
      break;
    case ':':
      break;
    default:
      break;
    }
  }

  realargs = (short*)malloc( sizeof( short ) * argc );
  for(s1=0;s1<argc;s1++) {
    realargs[s1] = 0;
  }

  while ( optind < argc ) {
    sendargs++;
    realargs[optind++] = 1;
  }

  if(init_transferblocks()!=0) {
    fprintf(stderr,"Not enough mem in main()!\nExiting\n");
    free( realargs );
    exit(1);
  }
  
  if (pin_init_user(useport) < 0) {
    fprintf( stderr, "can't initialize parallelport!\n" );
    free( realargs );
    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);
    free( realargs );
    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");
    free( realargs );
    exit(1);
  }
  
  if( (o1==0) && (sendargs==0) ) {
    printf("Nothing to do!\n");
    free( realargs );
    exit(1);
  }
  
  if(sendargs>0) {
    send(argc,argv,realargs);
  } else {
    receive();
  }

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