/*************************************************************************************************
 * dpmgr
 *                                                       Copyright(c) 2000-2003 Mikio Hirabayashi
 *************************************************************************************************/


#include <depot.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#undef TRUE
#define TRUE           1                 /* boolean true */
#undef FALSE
#define FALSE          0                 /* boolean false */

#define ALIGNSIZ       32                /* basic size of alignment */


/* global variables */
const char *progname;                    /* program name */


/* function prototypes */
int main(int argc, char **argv);
void usage(void);
char *mystrdup(const char *str);
char *hextoobj(const char *str, int *sp);
char *readfile(const char *name, int *sp);
int runcreate(int argc, char **argv);
int runput(int argc, char **argv);
int runout(int argc, char **argv);
int runget(int argc, char **argv);
int runlist(int argc, char **argv);
int runoptimize(int argc, char **argv);
int runinform(int argc, char **argv);
int runremove(int argc, char **argv);
void pdperror(const char *name);
void printobj(const char *obj, int size);
void printobjhex(const char *obj, int size);
int docreate(const char *name, int bnum);
int doput(const char *name, const char *kbuf, int ksiz, const char *vbuf, int vsiz,
          int dmode, int align);
int doout(const char *name, const char *kbuf, int ksiz);
int doget(const char *name, const char *kbuf, int ksiz, int start, int max, int ox, int nb);
int dolist(const char *name, int ox);
int dooptimize(const char *name, int bnum, int align);
int doinform(const char *name);
int doremove(const char *name);


/* main routine */
int main(int argc, char **argv){
  int rv;
  progname = argv[0];
  if(argc < 2) usage();
  rv = 0;
  if(!strcmp(argv[1], "create")){
    rv = runcreate(argc, argv);
  } else if(!strcmp(argv[1], "put")){
    rv = runput(argc, argv);
  } else if(!strcmp(argv[1], "out")){
    rv = runout(argc, argv);
  } else if(!strcmp(argv[1], "get")){
    rv = runget(argc, argv);
  } else if(!strcmp(argv[1], "list")){
    rv = runlist(argc, argv);
  } else if(!strcmp(argv[1], "optimize")){
    rv = runoptimize(argc, argv);
  } else if(!strcmp(argv[1], "inform")){
    rv = runinform(argc, argv);
  } else if(!strcmp(argv[1], "remove")){
    rv = runremove(argc, argv);
  } else if(!strcmp(argv[1], "version") || !strcmp(argv[1], "--version")){
    printf("Powered by QDBM version %s\n", dpversion);
    printf("Copyright (c) 2000-2003 Mikio Hirabayashi\n");
    rv = 0;
  } else {
    usage();
  }
  return rv;
}


/* print the usage and exit */
void usage(void){
  fprintf(stderr, "%s: administration utility for Depot\n", progname);
  fprintf(stderr, "\n");
  fprintf(stderr, "usage:\n");
  fprintf(stderr, "  %s create [-v] [-bnum num] name\n", progname);
  fprintf(stderr, "  %s put [-v] [-kx] [-vx] [-vf] [-keep] [-cat] [-na] name key val\n", progname);
  fprintf(stderr, "  %s out [-v] [-kx] name key\n", progname);
  fprintf(stderr, "  %s get [-v] [-kx] [-start num] [-max num] [-ox] [-n] name key\n", progname);
  fprintf(stderr, "  %s list [-v] [-ox] name\n", progname);
  fprintf(stderr, "  %s optimize [-v] [-bnum num] [-na] name\n", progname);
  fprintf(stderr, "  %s inform [-v] name\n", progname);
  fprintf(stderr, "  %s remove [-v] name\n", progname);
  fprintf(stderr, "  %s version\n", progname);
  exit(1);
}


/* duplicate a string */
char *mystrdup(const char *str){
  int len;
  char *buf;
  len = strlen(str);
  if(!(buf = malloc(len + 1))) return NULL;
  memcpy(buf, str, len + 1);
  return buf;
}


/* create a binary object from a hexadecimal string */
char *hextoobj(const char *str, int *sp){
  char *buf, mbuf[3];
  int len, i, j;
  len = strlen(str);
  if(!(buf = malloc(len + 1))) return NULL;
  j = 0;
  for(i = 0; i < len; i += 2){
    while(strchr(" \n\r\t\f\v", str[i])){
      i++;
    }
    if((mbuf[0] = str[i]) == '\0') break;
    if((mbuf[1] = str[i+1]) == '\0') break;
    mbuf[2] = '\0';
    buf[j++] = strtol(mbuf, NULL, 16);
  }
  buf[j] = '\0';
  *sp = j;
  return buf;
}


/* read data from file */
char *readfile(const char *name, int *sp){
  FILE *IN;
  int i, c;
  char *buf;
  if(!(IN = fopen(name, "rb"))) return NULL;
  buf = NULL;
  for(i = 0; (c = fgetc(IN)) != EOF; i++){
    if(!(buf = realloc(buf, i + 1))){
      fclose(IN);
      return NULL;
    }
    buf[i] = c;
  }
  fclose(IN);
  *sp = i;
  return buf;
}


/* parse arguments of create command */
int runcreate(int argc, char **argv){
  char *name;
  int i, bnum, rv;
  name = NULL;
  bnum = -1;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-v")){
        dpdbgfd = 2;
      } else if(!strcmp(argv[i], "-bnum")){
        if(++i >= argc) usage();
        bnum = atoi(argv[i]);
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else {
      usage();
    }
  }
  if(!name) usage();
  rv = docreate(name, bnum);
  return rv;
}


/* parse arguments of put command */
int runput(int argc, char **argv){
  char *name, *key, *val, *kbuf, *vbuf;
  int i, kx, vx, vf, align, ksiz, vsiz, rv;
  int dmode;
  name = NULL;
  kx = FALSE;
  vx = FALSE;
  vf = FALSE;
  align = ALIGNSIZ;
  key = NULL;
  val = NULL;
  dmode = DP_DOVER;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-v")){
        dpdbgfd = 2;
      } else if(!strcmp(argv[i], "-kx")){
        kx = TRUE;
      } else if(!strcmp(argv[i], "-vx")){
        vx = TRUE;
      } else if(!strcmp(argv[i], "-vf")){
        vf = TRUE;
      } else if(!strcmp(argv[i], "-keep")){
        dmode = DP_DKEEP;
      } else if(!strcmp(argv[i], "-cat")){
        dmode = DP_DCAT;
      } else if(!strcmp(argv[i], "-na")){
        align = 0;
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else if(!key){
      key = argv[i];
    } else if(!val){
      val = argv[i];
    } else {
      usage();
    }
  }
  if(!name || !key || !val) usage();
  if(kx){
    kbuf = hextoobj(key, &ksiz);
  } else {
    kbuf = mystrdup(key);
    ksiz = -1;
  }
  if(vx){
    vbuf = hextoobj(val, &vsiz);
  } else if(vf){
    vbuf = readfile(val, &vsiz);
  } else {
    vbuf = mystrdup(val);
    vsiz = -1;
  }
  if(kbuf && vbuf){
    rv = doput(name, kbuf, ksiz, vbuf, vsiz, dmode, align);
  } else {
    if(vf){
      fprintf(stderr, "%s: %s: cannot read\n", progname, val);
    } else {
      fprintf(stderr, "%s: out of memory\n", progname);
    }
    rv = 1;
  }
  free(kbuf);
  free(vbuf);
  return rv;
}


/* parse arguments of out command */
int runout(int argc, char **argv){
  char *name, *key, *kbuf;
  int i, kx, ksiz, rv;
  name = NULL;
  kx = FALSE;
  key = NULL;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-v")){
        dpdbgfd = 2;
      } else if(!strcmp(argv[i], "-kx")){
        kx = TRUE;
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else if(!key){
      key = argv[i];
    } else {
      usage();
    }
  }
  if(!name || !key) usage();
  if(kx){
    kbuf = hextoobj(key, &ksiz);
  } else {
    kbuf = mystrdup(key);
    ksiz = -1;
  }
  if(kbuf){
    rv = doout(name, kbuf, ksiz);
  } else {
    fprintf(stderr, "%s: out of memory\n", progname);
    rv = 1;
  }
  free(kbuf);
  return rv;
}


/* parse arguments of get command */
int runget(int argc, char **argv){
  char *name, *key, *kbuf;
  int i, kx, ox, nb, start, max, ksiz, rv;
  name = NULL;
  kx = FALSE;
  ox = FALSE;
  nb = FALSE;
  start = 0;
  max = -1;
  key = NULL;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-v")){
        dpdbgfd = 2;
      } else if(!strcmp(argv[i], "-kx")){
        kx = TRUE;
      } else if(!strcmp(argv[i], "-ox")){
        ox = TRUE;
      } else if(!strcmp(argv[i], "-n")){
        nb = TRUE;
      } else if(!strcmp(argv[i], "-start")){
        if(++i >= argc) usage();
        start = atoi(argv[i]);
      } else if(!strcmp(argv[i], "-max")){
        if(++i >= argc) usage();
        max = atoi(argv[i]);
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else if(!key){
      key = argv[i];
    } else {
      usage();
    }
  }
  if(!name || !key || start < 0) usage();
  if(kx){
    kbuf = hextoobj(key, &ksiz);
  } else {
    kbuf = mystrdup(key);
    ksiz = -1;
  }
  if(kbuf){
    rv = doget(name, kbuf, ksiz, start, max, ox, nb);
  } else {
    fprintf(stderr, "%s: out of memory\n", progname);
    rv = 1;
  }
  free(kbuf);
  return rv;
}


/* parse arguments of list command */
int runlist(int argc, char **argv){
  char *name;
  int i, ox, rv;
  name = NULL;
  ox = FALSE;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-v")){
        dpdbgfd = 2;
      } else if(!strcmp(argv[i], "-ox")){
        ox = TRUE;
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else {
      usage();
    }
  }
  if(!name) usage();
  rv = dolist(name, ox);
  return rv;
}


/* parse arguments of optimize command */
int runoptimize(int argc, char **argv){
  char *name;
  int i, bnum, align, rv;
  name = NULL;
  bnum = -1;
  align = ALIGNSIZ;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-v")){
        dpdbgfd = 2;
      } else if(!strcmp(argv[i], "-bnum")){
        if(++i >= argc) usage();
        bnum = atoi(argv[i]);
      } else if(!strcmp(argv[i], "-na")){
        align = 0;
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else {
      usage();
    }
  }
  if(!name) usage();
  rv = dooptimize(name, bnum, align);
  return rv;
}


/* parse arguments of inform command */
int runinform(int argc, char **argv){
  char *name;
  int i, rv;
  name = NULL;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-v")){
        dpdbgfd = 2;
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else {
      usage();
    }
  }
  if(!name) usage();
  rv = doinform(name);
  return rv;
}


/* parse arguments of remove command */
int runremove(int argc, char **argv){
  char *name;
  int i, rv;
  name = NULL;
  for(i = 2; i < argc; i++){
    if(!name && argv[i][0] == '-'){
      if(!strcmp(argv[i], "-v")){
        dpdbgfd = 2;
      } else {
        usage();
      }
    } else if(!name){
      name = argv[i];
    } else {
      usage();
    }
  }
  if(!name) usage();
  rv = doremove(name);
  return rv;
}


/* print an error message */
void pdperror(const char *name){
  fprintf(stderr, "%s: %s: %s\n", progname, name, dperrmsg(dpecode));
}


/* print an object */
void printobj(const char *obj, int size){
  int i;
  for(i = 0; i < size; i++){
    putchar(obj[i]);
  }
}


/* print an object as a hexadecimal string */
void printobjhex(const char *obj, int size){
  int i;
  for(i = 0; i < size; i++){
    printf("%s%02X", i > 0 ? " " : "", ((const unsigned char *)obj)[i]);
  }
}


/* perform create command */
int docreate(const char *name, int bnum){
  DEPOT *depot;
  if(!(depot = dpopen(name, DP_OWRITER | DP_OCREAT | DP_OTRUNC, bnum))){
    pdperror(name);
    return 1;
  }
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  return 0;
}


/* perform put command */
int doput(const char *name, const char *kbuf, int ksiz, const char *vbuf, int vsiz,
          int dmode, int align){
  DEPOT *depot;
  if(!(depot = dpopen(name, DP_OWRITER, -1))){
    pdperror(name);
    return 1;
  }
  if(align > 0 && !dpsetalign(depot, align)){
    pdperror(name);
    dpclose(depot);
    return 1;
  }
  if(!dpput(depot, kbuf, ksiz, vbuf, vsiz, dmode)){
    pdperror(name);
    dpclose(depot);
    return 1;
  }
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  return 0;
}


/* perform out command */
int doout(const char *name, const char *kbuf, int ksiz){
  DEPOT *depot;
  if(!(depot = dpopen(name, DP_OWRITER, -1))){
    pdperror(name);
    return 1;
  }
  if(!dpout(depot, kbuf, ksiz)){
    pdperror(name);
    dpclose(depot);
    return 1;
  }
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  return 0;
}


/* perform get command */
int doget(const char *name, const char *kbuf, int ksiz, int start, int max, int ox, int nb){
  DEPOT *depot;
  char *vbuf;
  int vsiz;
  if(!(depot = dpopen(name, DP_OREADER, -1))){
    pdperror(name);
    return 1;
  }
  if(!(vbuf = dpget(depot, kbuf, ksiz, start, max, &vsiz))){
    pdperror(name);
    dpclose(depot);
    return 1;
  }
  if(ox){
    printobjhex(vbuf, vsiz);
  } else {
    printobj(vbuf, vsiz);
  }
  free(vbuf);
  if(!nb) putchar('\n');
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  return 0;
}


/* perform list command */
int dolist(const char *name, int ox){
  DEPOT *depot;
  char *kbuf;
  int ksiz;
  if(!(depot = dpopen(name, DP_OREADER, -1))){
    pdperror(name);
    return 1;
  }
  dpiterinit(depot);
  while((kbuf = dpiternext(depot, &ksiz)) != NULL){
    if(ox){
      printobjhex(kbuf, ksiz);
    } else {
      printobj(kbuf, ksiz);
    }
    free(kbuf);
    putchar('\n');
  }
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  return 0;
}


/* perform optimize command */
int dooptimize(const char *name, int bnum, int align){
  DEPOT *depot;
  if(!(depot = dpopen(name, DP_OWRITER, -1))){
    pdperror(name);
    return 1;
  }
  if(align > 0 && !dpsetalign(depot, align)){
    pdperror(name);
    dpclose(depot);
    return 1;
  }
  if(!dpoptimize(depot, bnum)){
    pdperror(name);
    dpclose(depot);
    return 1;
  }
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  return 0;
}


/* perform inform command */
int doinform(const char *name){
  DEPOT *depot;
  if(!(depot = dpopen(name, DP_OREADER, -1))){
    pdperror(name);
    return 1;
  }
  printf("name: %s\n", dpname(depot));
  printf("file size: %d\n", dpfsiz(depot));
  printf("all buckets: %d\n", dpbnum(depot));
  printf("used buckets: %d\n", dpbusenum(depot));
  printf("records: %d\n", dprnum(depot));
  printf("inode number: %d\n", dpinode(depot));
  if(!dpclose(depot)){
    pdperror(name);
    return 1;
  }
  return 0;
}


/* perform remove command */
int doremove(const char *name){
  if(!dpremove(name)){
    pdperror(name);
    return 1;
  }
  return 0;
}



/* END OF FILE */
