//===========================================================================
//  @(#) $Name: arts++-1-1-a12 $
//  @(#) $Id: artsprotoagg.cc,v 1.2 2004/04/21 23:51:24 kkeys Exp $
//===========================================================================
//  Copyright Notice
//
//  By accessing this software, arts++, you are duly informed
//  of and agree to be bound by the conditions described below in this
//  notice:
//
//  This software product, arts++, is developed by Daniel W. McRobb, and
//  copyrighted(C) 1998 by the University of California, San Diego
//  (UCSD), with all rights reserved.  UCSD administers the CAIDA grant,
//  NCR-9711092, under which part of this code was developed.
//
//  There is no charge for arts++ software.  You can redistribute it
//  and/or modify it under the terms of the GNU Lesser General Public
//  License, Version 2.1, February 1999, which is incorporated by
//  reference herein.
//
//  arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF
//  MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use
//  of it will not infringe on any third party's intellectual
//  property rights.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with arts++.  Copies can also be obtained from:
//
//    http://www.gnu.org/copyleft/lesser.html
//
//  or by writing to:
//
//  Free Software Foundation, Inc.
//  59 Temple Place, Suite 330
//  Boston, MA 02111-1307
//  USA
//
//  Or contact:
//
//    info@caida.org
//===========================================================================

extern "C" {
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
}

#ifdef HAVE_FSTREAM
#include <fstream.h>
#else
#include <fstream>
#endif

#include <string>

#include "ArtsFileUtil.hh"
#include "ArtsPackageVersion.hh"
#ifndef NDEBUG
  #include "ArtsDebug.hh"
#endif

extern char *tzname[2];
extern int   errno;

using namespace std;

static const string rcsid = "@(#) $Name: arts++-1-1-a12 $ $Id: artsprotoagg.cc,v 1.2 2004/04/21 23:51:24 kkeys Exp $";
static const ArtsPackageVersion    k_artsVersion = ArtsPackageVersion(rcsid);

//-------------------------------------------------------------------------
//                      void Usage(const char *argv0)                      
//.........................................................................
//  
//-------------------------------------------------------------------------
void Usage(const char *argv0)
{
  cerr << "Usage: " << argv0 << " [-v] [-i ifIndexList] [-I ifIndexList]"
       << " [-q] [-o] [-h hours] outfile infile(s)"
       << endl;
  return;
}

//-------------------------------------------------------------------------
//                     int main(int argc, char *argv[])                    
//.........................................................................
//  
//-------------------------------------------------------------------------
int main(int argc, char *argv[])
{
  extern int      optind;
  int             optChar;
  float           hours = -1.0;
  bool            quiet = false;
  bool            overwrite = false;
  string          outFilename;
  vector<string>  inFilenames;
  ArtsFileUtil    *artsFileUtil = new ArtsFileUtil();
  
  while ((optChar = getopt(argc,argv,"I:h:i:qov")) != EOF) {
    switch (optChar) {
      case 'I':
        artsFileUtil->IfIndexCombineSet(optarg);
        break;
      case 'h':
        hours = atof(optarg);
        break;
      case 'i':
        artsFileUtil->IfIndexSelectionSet(optarg);
        break;
      case 'q':
        quiet = true;
        break;
      case 'o':
        overwrite = true;
        break;
      case 'v':
        cerr << k_artsVersion.Name() << "  " << k_artsVersion.Id() << endl;
        exit(0);
        break;
      case '?':
      default:
        Usage(argv[0]);
        exit(1);
        break;
    }
  }

  if (optind >= (argc-1)) {
    Usage(argv[0]);
    exit(1);
  }

  outFilename = string(argv[optind]);
  optind++;

  for (int fileNum = optind; fileNum < argc; fileNum++) {
    inFilenames.push_back(argv[fileNum]);
  }

  artsFileUtil->ObjectTypeSelectionSet("proto");
  artsFileUtil->Aggregate(outFilename,inFilenames,hours,overwrite,quiet);

  delete(artsFileUtil);
  
  #ifndef NDEBUG
    atexit(ShowArtsObjectsNotFreed);
  #endif

  exit(0);
}
