/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 
/* vim: set ts=2 et sw=2 tw=80: */
/*
  Copyright 2013-2021 Stephan Beal (https://wanderinghorse.net).

  Derived heavily from previous work:

  Copyright (c) 2013 D. Richard Hipp (https://www.hwaci.com/drh/)

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

#include "fossil-scm/fossil-cli.h" /* Fossil App mini-framework */

static void tag_help(){
  char const * uname;
  char * zName = NULL;
  printf("Usage:\n\t%s [options]\n\n", fcli.appName);

  puts("Sets tags on Fossil repository artifacts. Options:\n");

  puts("\t--artifact|-a=ARTIFACT_TO_TAG (uuid or symbolic name)\n");
  puts("\t--tag|-t=TAG_NAME Use name prefix '+' to add a tag, '-' to cancel, "
       "and '*' to propagate. Default is to add (+).\n");
  puts("\t--value|-v=TAG_VALUE optional tag value\n");
  uname = fsl_cx_user_get(fcli.f);
  if(!uname){
    zName = fsl_guess_user_name();
    uname = zName;
  }
  assert(uname);
  printf("\t--user|-u=USER (current: %s)\n\n", uname);
  fsl_free(zName);
  puts("\t--dry-run|-n runs the operation in a transaction and then rolls it back.\n");
  puts("\t--test implies --dry-run and dumps the generated manifest to stdout.\n");

  puts("NOTE: multiple tags can be set by providing pairs of -t/-v flags. "
       "Values in the list cannot be skipped but can be set to an empty "
       "value with -v='', which has the same effect. e.g.:\n");
  f_out("\t%s -a current -t tag1 -v val1 -t tag2 -v='' -t tag3 -v val3\n\n"
        "As a special case, the -v flag is optional for the LAST tag in the list.\n",
        fcli.appName);

}

/**
    Just experimenting with fsl_xlink_listener() and friends.
 */
static int tag_xlink_f(fsl_deck * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact [%.*s] (RID %"FSL_ID_T_PFMT")\n",
           fsl_satype_cstr(d->type), 8, d->uuid, d->rid));
  return *((char const *)state) /* demonstrate what happens when crosslinking fails. */
    ? FSL_RC_IO
    : 0;
}


int main(int argc, char * const * argv ){
  int rc = 0;
  const char * symToTag = NULL;
  char dryRun = 0;
  fsl_cx * f = 0;
  fsl_db * db = 0;
  char failCrosslink = 0;
  char const * userName = 0;
  fsl_list liTags = fsl_list_empty;
  fsl_list liVals = fsl_list_empty;
  fsl_size_t i;
  fsl_deck mf = fsl_deck_empty;
  char inTrans = 0;
  fsl_uuid_str uuid = NULL;
  int vbose = 0;
  char testMode;
  fcli.appHelp = tag_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;
  }
  vbose = fcli_is_verbose();

  testMode = fcli_flag("test", NULL);
  failCrosslink = fcli_flag2("fx", "fail-xlink", NULL);
  fsl_xlink_listener( f, fcli.appName, tag_xlink_f, &failCrosslink );
  fcli_flag2("a", "artifact", &symToTag);

#if 1
  /* LOL: if we short-circuit the -n check based on testMode then
     fcli_has_unused_flags() reports -n as unknown/unused. So we
     order the booleans in a non-optimal way...
  */
  dryRun = fcli_flag("n",NULL) || fcli.fDryRun || testMode;
#else
  dryRun = 1 /* only while dev'ing */;
#endif

  if(!symToTag || !*symToTag){
    missingArgs:
    rc = fcli_err_set(FSL_RC_MISUSE, "The --tag and --artifact "
                      "flags are required. Use --help for more info.");
    goto end;
  }

  rc = fsl_sym_to_uuid(f, symToTag, FSL_SATYPE_ANY, &uuid, NULL);
  if(rc) goto end;
  assert(fsl_is_uuid(uuid));

  while(1){
    const char * t = NULL;
    const char * v = NULL;
    fcli_flag2("t", "tag", &t);
    if(!t) break;
    fcli_flag2("v", "value", &v);
    rc = fsl_list_append(&liTags, fsl_strdup(t));
    if(!rc) rc = fsl_list_append(&liVals, fsl_strdup(v));
    if(rc) goto end;
  }

  if(!liTags.used) goto missingArgs;
  else if(fcli_has_unused_flags(0)) goto end;

  userName = fsl_cx_user_get(f) /* set up by fcli */;
  if(!userName || !*userName){
    rc = fcli_err_set(FSL_RC_NOT_FOUND,
                      "Could not determine fossil user name. "
                      "Please specify %sone with --user|-U=name.",
                      userName ? "a non-empty " : "");
    goto end;
  }
  assert(liTags.used == liVals.used);

  fsl_deck_init(f, &mf, FSL_SATYPE_CONTROL);

  rc = fsl_deck_U_set(&mf, userName, -1);
  if(!rc) rc = fsl_deck_D_set(&mf, fsl_julian_now());
  if(rc) goto end;

  for( i = 0; i < liTags.used; ++i ){
    char const * t = (char const *)liTags.list[i];
    char const * v = (char const *)liVals.list[i];
    char prefixChar;
    fsl_tagtype_e tagType;
    assert(NULL != t);
    switch(*t){
      case '-':
        tagType = FSL_TAGTYPE_CANCEL;
        t += 1;
        prefixChar = '-';
        break;
      case '*':
        tagType = FSL_TAGTYPE_PROPAGATING;
        t += 1;
        prefixChar = '*';
        break;
      case '+':
        tagType = FSL_TAGTYPE_ADD;
        t += 1;
        prefixChar = '+';
        break;
      default:
        tagType = FSL_TAGTYPE_ADD;
        prefixChar = '+';
    }
    rc = fsl_deck_T_add( &mf, tagType, uuid, t, v );
    if(rc) goto end;
    if(vbose){
      f_out("Adding tag %c[%s] to %.12s%s", prefixChar, t, uuid, (v&&*v) ? "" : " with no value.\n");
      if(v && *v) f_out(" with value: %s\n", v);
    }
  }/*for-each tag loop*/
  
  fsl_db_transaction_begin(db);
  inTrans = 1;

  if(testMode){
    rc = fsl_deck_unshuffle(&mf, 0);
    if(rc) goto end;
    rc = fsl_deck_output(&mf, fsl_output_f_FILE, stdout);
    if(rc) goto end;
  }else{
    rc = fsl_deck_save( &mf, 0 );
  }

  if(!rc && vbose){
    f_out("Applied tags to [%s] for user [%s]. New tag: %.12s (RID %"FSL_ID_T_PFMT")\n",
          symToTag, userName, mf.uuid, mf.rid);
  }


  end:
  if(rc
     && !fsl_cx_err_get(f, NULL, NULL)
     && mf.error.code ){
    fsl_cx_err_set_e(f, &mf.error);
  }

  if(inTrans){
    if(dryRun && vbose) f_out("Dry-run mode. Rolling back transaction.\n");
    fsl_db_transaction_end(db, dryRun || rc);
    inTrans = 0;
  }

  fsl_deck_finalize(&mf);
  fsl_list_visit_free(&liTags, 1);
  fsl_list_visit_free(&liVals, 1);
  fsl_free(uuid);
  return fcli_end_of_main(rc);
}
