/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 
/* vim: set ts=2 et sw=2 tw=80: */
/*
** Copyright (c) 2013 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the Simplified BSD License (also
** known as the "2-Clause License" or "FreeBSD License".)
**
** 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.
**
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*******************************************************************************
** A simple tool for dumping blobs to stdout.
*/

#include "fossil-scm/fossil-cli.h"
#include "fossil-scm/fossil-internal.h"

static void fcli_local_help(){
  printf("Usage:\n\t%s [options] artifact_id\n\n", fcli.appName);

  puts("Outputs raw artifacts to stdout.\n");
}

int main(int argc, char * const * argv ){
  int rc = 0;
  char * sym = NULL;
  fsl_cx * f;
  fsl_db * db;
  fsl_id_t rid = 0;
  fsl_uuid_str uuid = NULL;
  fsl_buffer blob = fsl_buffer_empty;
  fcli.appHelp = fcli_local_help;
  rc = fcli_setup(argc, argv);
  if(FSL_RC_BREAK==rc) /* --help */ return 0;
  else if(rc) goto end;
  f = fcli.f;
  db = fsl_cx_db_repo(f);
  if(!db){
    rc = fsl_cx_err_set(f, FSL_RC_MISUSE,
                        "This app requires a repository db.");
    goto end;
  }
  if(fcli_has_unused_flags(0)) goto end;

  sym = fcli_next_arg(1);
  if(!sym){
    fcli_help();
    goto end;
  }

  rc = fsl_sym_to_uuid(f, sym, FSL_ATYPE_ANY, &uuid, &rid);
  if(rc) goto end;
  VERBOSE(("Symbol [%s] resolved to rid %"FSL_ID_T_PFMT" uuid %.*s\n",
           sym, rid, 12, uuid));
  rc = fsl_content_get(f, rid, &blob);
  if(!rc){
    fwrite(blob.mem, blob.used, 1, stdout);
  }
  end:
  fsl_buffer_clear(&blob);
  fsl_free(sym);
  fsl_free(uuid);
  return fcli_err_report(1);
}
