/* -*- 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-2014 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/
**
*******************************************************************************
** This file implements a basic timeline [test] app using the
** libfossil API.
*/

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


static struct TLApp_ {
  int limit;
  char showFiles;
  char utc;
  char const * ckoutUuid;
} TLApp = {
-1/*limit*/,
0/*showFiles*/,
0/*utc*/,
NULL/*ckoutUuid*/
};


/**
 ** fsl_stmt_each_f() implementation for a basic timeline view. The
 ** state parameter is ignored.
 */
static int stmt_each_f_timeline1( fsl_stmt * q, void * state ){
  fsl_cx * f = fcli.f;
  char const * x;
  char const * uuid = fsl_stmt_g_text(q,0/*uuid*/,NULL);
  char const * type = fsl_stmt_g_text(q,3/*type*/,NULL);
  char isCheckin = 0;
  switch(*type){
    case 'c': type = "checkin"; isCheckin = 1; break;
    case 'w': type = "wiki"; break;
    case 'g': type = "tag"; break;
    case 'e': type = "event"; break;
    case 't': type = "ticket"; break;
  };
  /* Tip to copy/pasters: q->rowCount can be used to determine what
     row number we're on. It starts counting at 1, not 0.
  */
  fsl_outputf(f, "%-8s[%.*s] @ %s by [%s]",
              type, 12, uuid,
              fsl_stmt_g_text(q,1/*time*/,NULL),
              fsl_stmt_g_text(q,2/*user*/,NULL)
              );
  if( (x = fsl_stmt_g_text(q,4/*branch*/,NULL)) ){
    fsl_outputf(f, " branch [%s]", x);
  }
  if(isCheckin && TLApp.ckoutUuid && 0==fsl_uuidcmp(uuid,TLApp.ckoutUuid)){
    fsl_outputf(f," *CURRENT*");
  }
  fsl_outputf(f, "\n\t%s\n",
              fsl_stmt_g_text(q,5/*comment*/,NULL));
  if(isCheckin && TLApp.showFiles){
    fsl_stmt * st = NULL;
    fsl_db * db = fsl_cx_db_repo(f);
    int rc;
    char doneHead = 0;
    assert(db);
    rc = fsl_db_prepare_cached(db, &st,
                               "SELECT bf.uuid, filename.name fname, bf.size "
                               "FROM mlink, filename, "
                               "blob bf, -- FILE blob\n"
                               "blob bm -- MANIFEST/checkin blob\n"
                               "WHERE "
                               "bm.uuid = ? "
                               "AND filename.fnid=mlink.fnid "
                               "AND bf.rid=mlink.fid "
                               "AND bm.rid=mlink.mid "
                               "ORDER BY filename.name %s",
                               fsl_cx_filename_collation(f));
    if(rc){
      return fsl_cx_uplift_db_error(f, db);
    }
    rc = fsl_stmt_bind_text(st, 1, uuid, FSL_UUID_STRLEN, 0);
    assert(0==rc);
    while(FSL_RC_STEP_ROW==(rc=fsl_stmt_step(st))){
      if(!doneHead){
        doneHead = 1;
        fsl_outputf(f,"\t%s %13s Name\n", "File UUID", "Size");
      }
      fsl_outputf(f,"\t%.*s %10"FSL_INT64_T_PFMT" %s\n",
                  12, fsl_stmt_g_text(st, 0, NULL),
                  (fsl_int64_t)fsl_stmt_g_int64(st, 2),
                  fsl_stmt_g_text(st, 1, NULL));
    }
    fsl_stmt_cached_yield(st);
  }
  fsl_output(f, "\n", 1);
  return 0;
}

static int my_timeline(){
  fcli_t * a = &fcli;
  fsl_buffer sql = fsl_buffer_empty;
  int rc;
  fsl_db * db = fsl_cx_db_repo(a->f);
  rc = fsl_buffer_appendf(&sql, "SELECT "
                         /*0*/"uuid AS uuid, "
                         /*1*/"datetime(event.mtime%s) AS timestampString, "
                         /*2*/"coalesce(euser, user) AS user, "
                         /*3*/"event.type AS eventType, "
                         /*4*/"(SELECT group_concat(substr(tagname,5), ',') FROM tag, tagxref "
                         "WHERE tagname GLOB 'sym-*' AND tag.tagid=tagxref.tagid "
                         "AND tagxref.rid=blob.rid AND tagxref.tagtype>0) as tags, "
                         /*5*/"coalesce(ecomment, comment) AS comment "
                         "FROM event JOIN blob "
                         "WHERE blob.rid=event.objid "
                         "ORDER BY event.mtime DESC",
                          TLApp.utc ? "" : ", 'localtime'");
  if(rc) goto end;
  if(TLApp.limit > 0){
    rc = fsl_buffer_appendf(&sql, " LIMIT %d", TLApp.limit);
    if(rc) goto end;
  }
  rc = fsl_db_each(db, stmt_each_f_timeline1, NULL,
                   "%b", &sql);
  fsl_flush(a->f);
  end:
  fsl_buffer_clear(&sql);
  if(rc && db->error.code){
    rc = fsl_cx_uplift_db_error(a->f, db);
  }
  return rc;
    
}

static void fcli_local_help(){
  printf("Usage:\n\t%s [options]\n\n", fcli.appName);
  puts("Displays timeline information for a Fossil repository db.");
  puts("Options:\n");

  puts("\t-n|-limit=NUMBER limits the results to that many. "
       "n=0 means unlimited. Negative values are ignored "
       "and use the default limit.\n");
  puts("\t--show-files|-f enables the listing of the files changed for "
       "each commit.\n");

  puts("\t--utc Changes time values from local time to UTC.\n");
}

int main(int argc, char * const * argv ){
  int rc = 0;
  char * zLimit = NULL;
  fsl_cx * f = NULL;
  fcli.appHelp = fcli_local_help;
  rc = fcli_setup(argc, argv);
  if(FSL_RC_BREAK==rc) return 0;
  else if(rc) goto end;
  f = fcli.f;

  TLApp.utc = fcli_flag("utc", NULL);
  TLApp.showFiles = fcli_flag2("f", "show-files", NULL);
  fcli_flag2("n", "limit", &zLimit);
  if(zLimit){
    TLApp.limit = atoi(zLimit);
    fsl_free(zLimit);
    zLimit = NULL;
  }
  if(TLApp.limit<0) TLApp.limit = 5;

  if(fcli_has_unused_flags(0)) goto end;

  fsl_checkout_version_info(f, &TLApp.ckoutUuid, NULL);
    

  if(!fsl_cx_db_repo(f)){
    rc = fsl_cx_err_set(f, FSL_RC_MISUSE, "Repo db required.");
    goto end;
  }
  rc = my_timeline();

  end:
  return (fcli_err_report(0) || rc)
    ? EXIT_FAILURE : EXIT_SUCCESS;
}
