/* -*- 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 holds some basic sanity test code.
*/

#include "fossil-scm/fossil-cli.h" /* Fossil App mini-framework */
#include "fossil-scm/fossil-internal.h"
#include <string.h>

static void fcli_local_help(){
  printf("Usage:\n\t%s file1 file2\n\n", fcli.appName);

  puts("Outputs a fossil-format delta of the two files.");

}

int main(int argc, char * const * argv ){
  int rc = 0;
  char * f1 = NULL;
  char * f2 = NULL;
  fsl_buffer b1 = fsl_buffer_empty,
    b2 = fsl_buffer_empty,
    d12 = fsl_buffer_empty
    ;
  fcli.appHelp = fcli_local_help;
  rc = fcli_setup(argc, argv);
  if(FSL_RC_BREAK==rc) /* --help */return 0;
  else if(rc) goto end;

  f1 = fcli_next_arg(1);
  f2 = fcli_next_arg(1);
  if(!f2){
    rc = fcli_err_set(FSL_RC_MISUSE,
                      "Usage: %s file1 file2",
                      fcli.appName);
    goto end;
  }
  
  rc = fsl_buffer_fill_from_filename(&b1, f1);
  if(rc){
    rc = fcli_err_set(rc, "Could not open file: %s\n", f1);
    goto end;
  }
  rc = fsl_buffer_fill_from_filename(&b2, f2);
  if(rc){
    rc = fcli_err_set(rc, "Could not open file: %s\n", f2);
    goto end;
  }
  rc = fsl_buffer_delta_create(&b1, &b2, &d12);
  assert(!rc);
  rc = fsl_buffer_delta_apply(&b1, &d12, &b1);
  assert(0==fsl_buffer_compare(&b1, &b2));
  
  f_out("%b\n", &d12);
  end:
  fsl_free(f1);
  fsl_free(f2);
  fsl_buffer_clear(&b1);
  fsl_buffer_clear(&b2);
  fsl_buffer_clear(&d12);
  return (fcli_err_report(0)||rc)
    ? EXIT_FAILURE : EXIT_SUCCESS;
}
