//===========================================================================
//  @(#) $Name: arts++-1-1-a12 $
//  @(#) $Id: artstoc.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 <time.h>
#include <errno.h>
}

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

#include <string>

#include "Arts.hh"
#include "ArtsTimeIntervalSelectionSet.hh"
#include "ArtsPackageVersion.hh"
#ifndef NDEBUG
  #include "ArtsDebug.hh"
#endif

extern char *tzname[2];

using namespace std;

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

//-------------------------------------------------------------------------
//                  void PrintTocLine(const Arts & arts)                  
//.........................................................................
//  
//-------------------------------------------------------------------------
static void PrintTocLine(const Arts & arts)
{
  struct tm       *localTm;
  time_t           periodTimes[2];
  struct in_addr   inAddr;
  
  vector<ArtsAttribute>::const_iterator  periodAttribute;
  vector<ArtsAttribute>::const_iterator  hostAttribute;
  vector<ArtsAttribute>::const_iterator  creationAttribute;
  vector<ArtsAttribute>::const_iterator  attribute;
  
  periodAttribute = arts.FindPeriodAttribute();
  if (periodAttribute != arts.Attributes().end()) {
    if (g_timeIntervalSet.size() > 0) {
      if ((! g_timeIntervalSet.Matches(periodAttribute->Period()[0])) ||
          (! g_timeIntervalSet.Matches(periodAttribute->Period()[1]))) {
        return;
      }
    }
  }
  
  hostAttribute = arts.FindHostAttribute();
  if (hostAttribute != arts.Attributes().end()) {
    inAddr.s_addr = hostAttribute->Host();
    cout << setw(15) << inet_ntoa(inAddr) << "  ";
  }

  attribute = arts.FindHostPairAttribute();
  if (attribute != arts.Attributes().end()) {
    inAddr.s_addr = attribute->HostPair()[0];
    string  buf(string("(") + inet_ntoa(inAddr) + ", ");
    inAddr.s_addr = attribute->HostPair()[1];
    buf += inet_ntoa(inAddr) + string(")");
    cout << setiosflags(ios::left) << setw(33) << buf.c_str() << "  "
         << resetiosflags(ios::left);
  }
    
  if (periodAttribute != arts.Attributes().end()) {
    memcpy(periodTimes,periodAttribute->Period(),8);
    localTm = localtime(&(periodTimes[0]));
    cout << setfill('0')
         << setw(2) << (int)localTm->tm_mon+1 << "/"
         << setw(2) << localTm->tm_mday << "/"
         << setw(4) << localTm->tm_year + 1900 << " "
         << setw(2) << localTm->tm_hour << ":"
         << setw(2) << localTm->tm_min << ":"
         << setw(2) << localTm->tm_sec << " - ";
    localTm = localtime(&(periodTimes[1]));
    cout << setw(2) << (int)localTm->tm_mon+1 << "/"
         << setw(2) << localTm->tm_mday << "/"
         << setw(4) << localTm->tm_year + 1900 << " "
         << setw(2) << localTm->tm_hour << ":"
         << setw(2) << localTm->tm_min << ":"
         << setw(2) << localTm->tm_sec << " " << tzname[localTm->tm_isdst]
         << "  ";
  }
  cout << setfill(' ');

  creationAttribute = arts.FindCreationAttribute();
  if (creationAttribute != arts.Attributes().end()) {
    periodTimes[0] = creationAttribute->Creation();
    localTm = localtime(&(periodTimes[0]));
    cout << setfill('0')
         << setw(2) << (int)localTm->tm_mon+1 << "/"
         << setw(2) << localTm->tm_mday << "/"
         << setw(4) << localTm->tm_year + 1900 << " "
         << setw(2) << localTm->tm_hour << ":"
         << setw(2) << localTm->tm_min << ":"
         << setw(2) << localTm->tm_sec << " "
         << tzname[localTm->tm_isdst] << "  ";
  }
  cout << setfill(' ');

  cout << arts.Header().IdentifierName() << endl;
  
  return;
}
  
//-------------------------------------------------------------------------
//                      void Usage(const char *argv0)                      
//.........................................................................
//  
//-------------------------------------------------------------------------
void Usage(const char *argv0)
{
  cerr << "Usage: " << argv0 << " [-v] [-b beginTime] [-e endTime] file(s)"
       << endl;
  return;
}

//-------------------------------------------------------------------------
//                     int main(int argc, char *argv[])                    
//.........................................................................
//  
//-------------------------------------------------------------------------
int main(int argc, char *argv[])
{
  Arts        *arts;
  ifstream    *artsStream;
  extern char *optarg;
  extern int   optind;
  int          optChar;
  char                     *startTimeExpression = (char *)0;
  char                     *endTimeExpression = (char *)0;
      
  if (argc < 2) {
    Usage(argv[0]);
    exit(1);
  }

  while ((optChar = getopt(argc,argv,"b:e:v")) != EOF) {
    switch (optChar) {
      case 'b':
        startTimeExpression = (char *)strdup(optarg);
        break;
      case 'e':
        endTimeExpression = (char *)strdup(optarg);
        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) {
    Usage(argv[0]);
    exit(1);
  }

  if (startTimeExpression || endTimeExpression)
    g_timeIntervalSet.Load(startTimeExpression,endTimeExpression);
  
  for (int fileNum = optind; fileNum < argc; fileNum++) {
    artsStream = new ifstream(argv[fileNum]);
    if ((! artsStream) || (! *artsStream)) {
      cerr << "[E] unable to open input file '" << argv[fileNum] << "': "
           << strerror(errno) << endl;
      continue;
    }
    
    while (*artsStream) {
      arts = new Arts();
      assert(arts != (Arts *)0);
      arts->Header().read(*artsStream);
      arts->Attributes().read(*artsStream,arts->Header().NumAttributes());
      if (! *artsStream) {
        delete(arts);
        break;
      }
      PrintTocLine(*arts);
      artsStream->seekg(arts->Header().DataLength(),ios::cur);
      delete(arts);
    }
    artsStream->close();
  }

  #ifndef NDEBUG
    atexit(ShowArtsObjectsNotFreed);
  #endif

  exit(0);
}
