/* -*- 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 'ls' for in-repo content
   (not yet filesystem-level).
*/

#include "fossil-scm/fossil-cli.h" /* Fossil App mini-framework */
#include <stdlib.h> /* atoi() */


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

  puts("Generates diff of individual blobs (not checkin versions!). Options:");
  puts("\n\t--no-color|-bw Disables color output.");
  puts("\n\t--context-lines|-c=### Specifies the number of context lines around differences.");
  puts("\n\t--html|-h Specifies HTML-format output, marked for styling via CSS.");
  puts("\n\t--invert|-v Inverts the to/from arguments for diff purposes.");
  puts("\n\t--sbs-width|-w=### specifies a side-by-side diff with the given column width.");
  puts("\nThe two versions to compare may be specified as non-flag arguments or "
       "via the -v1=x and -v2=y flags.");
}

struct ADiffOpt {
  int diffFlags;
  short sbsWidth;
  short contextLines;
  char ansiColor;
};
typedef struct ADiffOpt ADiffOpt;

/**
   
 */
static int f_adiff(fsl_id_t v1, fsl_id_t v2, const ADiffOpt * opt){
  int rc = 0;
  fsl_cx * f = fcli_cx();
  fsl_buffer lhs = fsl_buffer_empty;
  fsl_buffer rhs = fsl_buffer_empty;

  rc = fsl_content_get(f, v1, &lhs);
  if(rc) return rc;

  rc = fsl_content_get(f, v2, &rhs);
  if(rc) return rc;

  rc = fsl_diff_text(&lhs, &rhs, fsl_output_f_FILE, stdout,
                     opt->contextLines, opt->sbsWidth,
                     opt->diffFlags);
  fsl_buffer_clear(&lhs);
  fsl_buffer_clear(&rhs);
  return rc;
}

int main(int argc, char * const * argv ){
  int rc = 0;
  const char * vFrom = NULL;
  const char * vTo = NULL;
  const char * tmpVal = NULL;
  fsl_cx * f;
  fsl_id_t idFrom = 0, idTo = 0;
  ADiffOpt diffOpt = {
  0/*diffFlags*/,
  0/*sbsWidth*/,
  0/*contextLines*/,
  0/*ansiColor*/
  };

  fcli.appHelp = fcli_local_help;
  rc = fcli_setup(argc, argv);
  if(FSL_RC_BREAK==rc) /* --help */ return 0;
  else if(rc) goto end;

  /* Set up/validate args... */
  f = fcli.f;
  if(!fsl_cx_db_repo(f)){
    rc = fcli_err_set(FSL_RC_NOT_A_REPO,
                      "Requires a repository db. See --help.");
    goto end;
  }

  if(fcli_flag2("v", "invert", NULL)){
    diffOpt.diffFlags |= FSL_DIFF_INVERT;
  }
  if(fcli_flag2("l", "line-numbers", NULL)){
    diffOpt.diffFlags |= FSL_DIFF_LINENO;
  }
  if(fcli_flag2("h", "html", NULL)){
    diffOpt.diffFlags |= FSL_DIFF_HTML;
  }
  if(fsl_isatty(1)
     && !fcli_flag2("bw", "no-color", NULL)){
    diffOpt.diffFlags |= FSL_DIFF_ANSI_COLOR;
  }
  if(fcli_flag2("w", "sbs-width", &tmpVal)){
    diffOpt.diffFlags |= FSL_DIFF_SIDEBYSIDE;
    diffOpt.sbsWidth = atoi(tmpVal);
    tmpVal = NULL;
  }
  if(fcli_flag2("c", "context-lines", &tmpVal)){
    diffOpt.contextLines = atoi(tmpVal);
    tmpVal = NULL;
  }

  if( fcli_flag_or_arg( "v1", "from", &vFrom) ){
    fcli_flag_or_arg( "v2", "to", &vTo);
  }

  if(fcli_has_unused_flags(0)) goto end;

  if(!vFrom || !vTo){
    rc = fcli_err_set(FSL_RC_MISUSE, "Both of -v1 UUID and -v2 UUID are required.");
    goto end;
  }

  rc = fsl_sym_to_rid(f, vFrom, FSL_SATYPE_ANY, &idFrom);
  if(!rc){
    rc = fsl_sym_to_rid(f, vTo, FSL_SATYPE_ANY, &idTo);
  }
  if(rc) goto end;

  rc = f_adiff( idFrom, idTo, &diffOpt );

  end:
  return fcli_end_of_main(rc);
}
