/*
 *  MAIN.C
 */

/*
 * (c)Copyright 1992-93 by Tobias Ferber.
 *
 * This file is part of CPDIST.
 *
 * CPDIST 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 1 of the License,
 * or (at your option) any later version.
 *
 * CPDIST 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 CPDIST; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "cpdist.h"
#include "filecopy.h"

/* globals */
unsigned long global_opts; /* command-line options */
long global_buffersize;    /* size of our copy buffer */

void display_version_information(void)
{
  static char license[]=
    "CPDIST is free software; you can redistribute it and/or modify\n"
    "it under the terms of the GNU General Public License as published\n"
    "by the Free Software Foundation; either version 1 of the License,\n"
    "or (at your option) any later version.\n"
    "\n"
    "CPDIST is distributed in the hope that it will be useful,\n"
    "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
    "GNU General Public License for more details.\n"
    "\n"
    "You should have received a copy of the GNU General Public License\n"
    "along with CPDIST; see the file COPYING.  If not, write to the\n"
    "Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n"
    ;

  puts("CPDIST Version " VERSION " (compiled " __DATE__ ", " __TIME__ ")\n"
       "(c)Copyright 1992-93 by Tobias Ferber, ukjg@dkauni2.bitnet\n");

  puts(license);
}

main(int argc, char *argv[])
{
  int badopt= 0;

  /* these are safe in here */
  char distfile[MAXIMUM_PATHNAME_LENGTH];
  char destination[MAXIMUM_PATHNAME_LENGTH];

  int haveapath= 0;

#ifdef __MSDOS__

  /* filenames on MS-DOS systems look very ugly: all uppercase and
   * backslashes.  Perform some cosmetics. */

  whoami= "cpdist";

#else /* !__MSDOS__ */
  whoami= argv[0];

#endif /* __MSDOS__ */

  /* this is where the echo()d messages go to */
  ferr= stderr;

  strcpy(distfile, DEFAULT_DISTFILE);

  /* init the globals */
  global_opts= 0L;
  global_buffersize= MAXIMUM_BUFFERSIZE;

  while(--argc>0 && !badopt)
  {
    char *arg= *(++argv);

    if(*arg==OPTION_PREFIX)
    {
      if(arg[1]==OPTION_PREFIX)
	arg= convert_args(*argv);

      switch(*++arg)
      {

/*-a*/  case 'a':
	  global_opts |= OPT_REPLACEALL;
	  break;

/*-b*/  case 'b':
	  if(arg[1]) ++arg;
	  else arg= (--argc > 0) ? *(++argv) : (char *)0L;

	  if(arg && *arg)
	  { global_buffersize= atol(arg);
	    if(global_buffersize < 0L)
	    { echo("Illegal buffer size %ld (must be non-negative)",
		global_buffersize);
	      badopt= 10;
	    }
#if defined(__TURBOC__)
	    if(global_buffersize > MAXIMUM_BUFFERSIZE)
	    { echo("Buffer size is limited to %dK.  Will use this upper limit.",
		MAXIMUM_BUFFERSIZE/1024);
	    }
#endif /* __TURBOC__ */
	  }
	  else
	  { echo("Missing buffer size after '%s' option",*argv);
	    badopt= 10;
	  }
	  break;

/*-c*/  case 'c':
	  global_opts |= OPT_CHECKEXISTS;
	  break;

/*-D*/  case 'D':
	  if(arg[1]) ++arg;
	  else arg= (--argc > 0) ? *(++argv) : (char *)0L;

	  if(arg && *arg)
	  { if(!ln_addnode(arg))
	    { echo("Not enough bits in the system free pool!");
	      badopt= 10;
	    }
	  }
	  else
	  { echo("Missing distribution key after '%s' option.",*argv);
	    badopt= 10;
	  }
	  break;

/*-E*/  case 'E':
	  if(arg[1]) ++arg;
	  else arg= (--argc > 0) ? *(++argv) : (char *)0L;

	  if(arg && *arg)
	  { FILE *fp;
	    if( fp= fopen(arg,"w") )
	    { if(ferr && ferr!=stderr)
		fclose(ferr);
	      ferr= fp;
	    }
	    else
	    { perror(whoami);
	      echo("Can't redirect output to `%s'.",arg);
	      badopt= 10;
	    }
	  }
	  else
	  { echo("Missing filename or device after '%s' option.",*argv);
	    badopt= 10;
	  }
	  break;

/*-f*/  case 'f':
	  if(arg[1]) ++arg;
	  else arg= (--argc > 0) ? *(++argv) : (char *)0L;
	   if(arg && *arg)
	    strcpy(distfile,arg);
	  else
	  { echo("Missing filename after '%s' option", *argv);
	    badopt= 10;
	  }
	  break;

/*-?*/  case '?':
/*-h*/  case 'h': case 'H':
	  display_args();
	  puts("\nRefer to the docs for further information!");
	  badopt= 1; /* hack: means exit. */
	  break;

/*-i*/  case 'i':
	  global_opts |= OPT_IGNOREERRORS;
	  break;

/*-k*/  case 'k':
	  global_opts |= OPT_KEEPGOING;
	  break;

/*-n*/  case 'n':
	  global_opts |= OPT_DRYRUN;
	  break;

/*-q*/  case 'q':
/*-s*/  case 's':
	  global_opts |= OPT_SILENT;
	  break;

/*-v*/  case 'v':
	  display_version_information();
	  break;

/*..*/  default:
	  echo("bad option '-%s'.",arg);
	  badopt= 10;
	  break;
      }
    }
    else /* pathname */
    {
      if(*arg)
      {
        int l= strlen(arg)-1;

        if(arg[l]==':' || arg[l]==PSLASH)
          strcpy(destination, arg);
        else
          sprintf(destination,"%s%c",arg,PSLASH);
      }
      else destination[0]= '\0';

      if(haveapath)
      { echo("More than one destination path in your command line.");
        echo("Will use %s now.", *destination ? destination : "current directory");
      }
      else haveapath++;
    }
  } /* wend */

  if(!badopt)
  {
    if(!haveapath && CP_DRYRUN)
      destination[haveapath++]= '\0';

    if(haveapath) /* let's roll! */
      badopt= prepare_distribution(distfile, destination);
    else
    { echo("Missing destination path.");
      badopt= 10;
    }
  }
  ln_purge();
  exit(badopt);
}
