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

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

/**
 ** fsl_stmt_each_f() implementation for a basic timeline view. The
 ** state parameter is ignored.
 */
int stmt_each_f_timeline1( fsl_stmt * q, void * state ){
  fsl_cx * f = fcli.f;
  char const * x;
  /* 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, "%-2s [%.*s] @ %s by [%s]",
              fsl_stmt_g_text(q,3,NULL)/*type*/,
              12, fsl_stmt_g_text(q,0,NULL)/*uuid*/,
              fsl_stmt_g_text(q,1,NULL)/*time*/,
              fsl_stmt_g_text(q,2,NULL)/*user*/
              );
  if( (x = fsl_stmt_g_text(q,4,NULL)) ){
    fsl_outputf(f, " in branch [%s]", x);
  }
  fsl_outputf(f, "\n\t%s\n\n", fsl_stmt_g_text(q,5,NULL));
  return 0;
}

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


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*/"substr(uuid,1,12) 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--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);
  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;

  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;
}
