/* -*- 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 The Libfossil Authors, see LICENSES/BSD-2-Clause.txt

  SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  SPDX-FileCopyrightText: 2021 The Libfossil Authors
  SPDX-ArtifactOfProjectName: Libfossil
  SPDX-FileType: Code
  
  Heavily indebted to the Fossil SCM project (https://fossil-scm.org).

  *****************************************************************************
  A simple tool for resolving symbolic names to UUIDs and RIDs.
*/

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


int main(int argc, char const * const * argv ){
  int rc = 0;
  const char * sym = NULL;
  fsl_cx * f;
  fsl_db * db;
  int count = 0;
  fsl_id_t rid;
  fsl_uuid_str uuid;
  bool startOfBranch = 0;
  fcli_cliflag FCliFlags[] = {
    FCLI_FLAG_BOOL("sb","start-of-branch",&startOfBranch,
                   "Expects the symbolic name to be a branch name "
                   "and resolves to the first checkin in that branch, rather than the "
                   "most recent. Results are unspecified if the branch name is used by "
                   "multiple branches."),
    fcli_cliflag_empty_m
  };
  fcli_help_info FCliHelp = {
    "Resolves symbolic version names to full-length hash IDs.",
    "symbol [...symbol]",
  NULL
  };
  fcli.cliFlags = FCliFlags;
  fcli.appHelp = &FCliHelp;
  rc = fcli_setup(argc, argv);
  if(FCLI_RC_HELP==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;
  }
  startOfBranch = fcli_flag2("sb", "start-of-branch",0);
  if(fcli_has_unused_flags(0)) goto end;

  while( (sym = fcli_next_arg(1)) ){
    uuid = NULL;
    rid = 0;
    ++count;
    rc = fsl_sym_to_uuid(f, sym,
                         startOfBranch ? FSL_SATYPE_BRANCH_START : FSL_SATYPE_ANY,
                         &uuid, &rid);
    if(!rc){
      f_out("%s %7"FSL_ID_T_PFMT" %s\n",
             uuid, rid, sym);
    }
    fsl_free(uuid);
    if(rc) goto end;
  }
  if(!count){
    rc = fcli_err_set(FSL_RC_MISUSE,
                      "No symbolic names were provided. See --help.");
  }
  end:
  return fcli_end_of_main(rc);
}
