/* -*- 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 */
/* #include "fossil-scm/fossil-internal.h" */

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

  puts("Lists all files in a repository. Currently does no introspection "
       "vs. the local checkout. "
       "Options:\n");

  puts("\t--own|-o lists only files actually contained in the "
       "given version's manifest. The result will differ only "
       "for delta manifests.\n");
  
  puts("\t--version|-v=VERSION (uuid or symbolic name). "
       "Default is 'current' if there is an opened checkout, "
       "else 'trunk'.\n");

  puts("\t--verbose|-V enables more details.");
}

/**
   A fsl_card_F_visitor_f() implementation which outputs
   state from fc to the fossil output channel.
*/
static int ls_F_card_v(fsl_card_F const * fc, void * state){
  char perm;
  if(FSL_MF_PERM_EXE & fc->perms) perm = 'x';
  else if(FSL_MF_PERM_LINK & fc->perms) perm = 'L';
  else perm = '-';
  if(fcli.verbose){
    f_out("%-8"FSL_ID_T_PFMT,
           fsl_uuid_to_rid(fcli.f, fc->uuid));
  }
  f_out("%.*s %c %s\n", 8, fc->uuid, perm, fc->name);
  return 0;
}

static int ls_ls( fsl_deck * d, char onlyFromSelf ){
  /* fsl_cx * f = fcli.f; */
  int rc;
  if(fcli.verbose){
    f_out("RID     ");
  }
  f_out("%-8s P Name\n", "UUID");
  rc = fsl_deck_F_foreach(d, onlyFromSelf ? 0 : 1,
                          ls_F_card_v, NULL);
  return rc;
}

int main(int argc, char * const * argv ){
  int rc = 0;
  char * lsVersion = NULL;
  fsl_cx * f;
  fsl_id_t vid = 0;
  fsl_deck deck = fsl_deck_empty;
  fsl_deck * d = &deck;
  char onlyFromSelf = 0;
  /* fsl_db * db; */
  fcli.appHelp = fcli_local_help;
  rc = fcli_setup(argc, argv);
  if(FSL_RC_BREAK==rc) /* --help */ return 0;
  else if(rc) goto end;
  f = fcli.f;

  onlyFromSelf = fcli_flag2("o", "own", NULL);
  if(!fcli_flag2("v","version", &lsVersion)){
    lsVersion = fsl_strdup(fsl_cx_db_checkout(f) ? "current" : "trunk");
  }
  assert(lsVersion);

  rc = fsl_sym_to_rid(f, lsVersion, FSL_ATYPE_CHECKIN, &vid);
  if(rc) goto end;
  rc = fsl_deck_load_rid( f, vid, d, FSL_CATYPE_MANIFEST );
  if(rc) goto end;
  VERBOSE(("File list from manifest version [%s] "
           "(RID %"FSL_ID_T_PFMT")%s:\n",
           lsVersion, vid,
           onlyFromSelf ? " (only files from _this_ manifest)" : ""));
  rc = ls_ls(d, onlyFromSelf);
  
  end:
  fsl_free(lsVersion);
  fsl_deck_finalize(d);
  return (fcli_err_report(0)||rc) ? EXIT_FAILURE : EXIT_SUCCESS;
}
