/* -*- 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 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");
}

/**
    Just experimenting with fsl_xlink_listener() and friends.
 */
static int tag_xlink_f(fsl_cx * f, fsl_deck const * d, void * state){
  FCLI_V(("Crosslink callback for %s artifact [%.*s] (RID %"FSL_ID_T_PFMT")\n",
           fsl_catype_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;
  char * symToTag = NULL;
  char * tagNameReal = NULL /* tag name as entered by the user */;
  char const * tagName = NULL /* tag name adjusted to remove prefix */;
  char * tagVal = NULL;
  fsl_id_t tagrid = 0;
  char dryRun = 0;
  fsl_cx * f;
  fsl_tag_type tagType;
  fsl_db * db;
  char prefixChar = 0;
  char failCrosslink = 0;
  char const * userName;
  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;
  }
  failCrosslink = fcli_flag2("fx", "fail-xlink", NULL);
  fsl_xlink_listener( f, fcli.appName, tag_xlink_f, &failCrosslink );
  fcli_flag2("t", "tag", &tagNameReal);
  fcli_flag2("v", "value", &tagVal);
  fcli_flag2("a", "artifact", &symToTag);
  dryRun = fcli.fDryRun || fcli_flag("n",NULL);
  if(!symToTag || !*symToTag || !tagNameReal || !*tagNameReal){
    rc = fcli_err_set(FSL_RC_MISUSE, "The --tag and --artifact "
                      "flags are required. Use --help for more info.");
    goto end;
  }

  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;
  }
  
  switch(*tagNameReal){
    case '-':
      tagType = FSL_TAGTYPE_CANCEL;
      tagName = tagNameReal+1;
      prefixChar = '-';
      break;
    case '*':
      tagType = FSL_TAGTYPE_PROPAGATING;
      tagName = tagNameReal+1;
      prefixChar = '*';
      break;
    case '+':
      tagType = FSL_TAGTYPE_ADD;
      tagName = tagNameReal+1;
      prefixChar = '+';
      break;
    default:
      tagType = FSL_TAGTYPE_ADD;
      tagName = tagNameReal;
      prefixChar = '+';
  }
  
  if(dryRun) fsl_db_transaction_begin(db);
  rc = fsl_tag_sym(f, tagType, symToTag,
                        tagName,
                        tagVal, userName, 0.0,
                        &tagrid );
  if(!rc){
    f_out("Applied tag [%c%s] to [%s]. New tag rid=%"FSL_ID_T_PFMT,
           prefixChar, tagName, symToTag, tagrid);
    if(tagVal){
      f_out(" with value [%s]", tagVal);
    }
    f_out(" for user [%s].\n", userName);
  }
  if(dryRun){
    if(!rc) f_out("Dry-run mode: rolling back transaction.\n");
    fsl_db_transaction_end(db, 1);
  }

  end:
  fsl_free(symToTag);
  fsl_free(tagNameReal);
  fsl_free(tagVal);
  return fcli_end_of_main(rc);
}
