/**********************************************************************
 *
 * db2txt.cpp -- 
 * Copyright (C) 1999  The New Zealand Digital Library Project
 *
 * A component of the Greenstone digital library software
 * from the New Zealand Digital Library Project at the
 * University of Waikato, New Zealand.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *********************************************************************/

#ifdef __WIN32__
#include "autoconf.h"
#include "systems.h"
#include "gdbmconst.h"
#include "gdbm.h"

#else
#include <gdbm.h>
#endif

#include "gsdlconf.h"
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>

#if defined(GSDL_USE_OBJECTSPACE)
#include <ospace\std\iostream>
#elif defined(GSDL_USE_IOS_H)
#include <iostream.h>
#else
#include <iostream>
#endif

// use the standard namespace
#if !defined (GSDL_NAMESPACE_BROKEN)
#if defined(GSDL_USE_OBJECTSPACE)
using namespace ospace::std;
#else
using namespace std;
#endif
#endif

void print_usage (char *program_name) {
  cerr << "usage: " << program_name << " database-name" << endl << endl;
}

// lock a file on linux
// [hs, 2 july 2010]
// - modified to create a locl file local to the collection [jmt12]
int lock ()
{
  string file_path ("");
  char *collect_dir = getenv ("GSDLCOLLECTDIR");
  if (collect_dir != NULL)
  {
    file_path += collect_dir;
  }
  file_path += "/tmp";
  if ( access( file_path.c_str(), 00 ) != 0 )
  {
    mkdir(file_path.c_str(), 00777);
  }
  file_path += "/gdb.lock";
  ///out << "txt2dbl::lock(" << file_path << ") => ";
  int fd2 = open (file_path.c_str(), O_CREAT|O_RDWR, 00644);
  close (fd2);
  int fd = open (file_path.c_str(), O_RDWR);
  flock lock = {F_WRLCK, SEEK_SET, 0, 0, 0};
  fcntl (fd, F_SETLKW, &lock);
  ///out << "locked!" << endl;
  return fd;
}

// unlock a file on linux
// [hs, 2 july 2010]
int unlock ( int fd )
{
  flock lock1 = {F_UNLCK, SEEK_SET, 0, 0, 0};
  fcntl (fd, F_SETLKW, &lock1);
  return 0;
}

int main (int argc, char *argv[]) {
  int block_size = 0, i;
  GDBM_FILE dbf;
  datum key, value, nextkey;
      
  // sanity check
  if (argc != 2) {
    print_usage (argv[0]);
    exit (0);
  }
  
  // open the database
  // - acquire a lock over the database so we can read from it.
  int thelock = lock();
#ifdef __WIN32__
  dbf = gdbm_open (argv[1], block_size, GDBM_READER, 00664, NULL, 0);
#else
  dbf = gdbm_open  (argv[1], block_size, GDBM_READER, 00664, NULL);
#endif
  if (dbf == NULL) {
    cerr << argv[0] << ": couldn't open " << argv[1] << endl;
    exit (0);
  }

  key = gdbm_firstkey (dbf);
  while (key.dptr != NULL) {
    cout << "[";
    for (i = 0; i < key.dsize; ++i)
      cout << key.dptr[i];
    cout << "]" << endl;
    value = gdbm_fetch (dbf, key);
    for (i = 0; i < value.dsize; ++i)
      cout << value.dptr[i];
    cout << endl << "----------------------------------------------------------------------" << endl;
    free(value.dptr);

    /* get next key */
    nextkey = gdbm_nextkey (dbf, key);

    /* free old key's dptr, otherwise causes memory leak */
    free(key.dptr); 
    
    /* can now safely copy content of nextkey into key */
    key = nextkey;
  }
  
  gdbm_close (dbf);

  // Unlock now that we've written the entire database
  unlock (thelock);

  return 0;
}
