/* -*- 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.
  
  *****************************************************************************
  A test/demo app for creating new fossil repository DBs using the libfossil
  API.
*/

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

#ifndef _WIN32
#include <unistd.h> /*isatty()*/
#endif

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

  puts("Creates a new, empty repository db. Options:\n");

  puts("\t-c|--config=repo_db_file Copies parts of the "
       "configuration from the given repo.\n");

  puts("\t-m|--message=text Sets the commit message for the initial commit.\n");

  puts("\t-N|--mimetype=text Sets the commit message mimetype for the initial commit. "
       "This is permitted because fossil technically allows it, but fossil has never "
       "applied it when rendering, so its use is discouraged. (-N corresponds to the "
       "manifest's N-card.)\n");

  puts("\t-f|--file=repo_file_to_create. The filename may optionally "
       "be provided as the first non-flag parameter.\n");

  puts("\t-h|--hash=POLICY Sets the repo's hash policy to one of: "
       "(sha1, sha3, shun-sha1, sha3-only, auto). Default is sha3.\n");

  puts("\t-F|--force will overwrite any existing repo with the given name.\n");
}


static struct FNewApp_{
  const char * comment;
  const char * commentMimetype;
  const char * filename;
  const char * configRepo;
  fsl_hashpolicy_e hashPolicy;
} FNewApp = {
NULL/*comment*/,
NULL/*commentMimetype*/,
NULL/*filename*/,
NULL/*configRepo*/,
FSL_HPOLICY_SHA3/*hashPolicy*/
};

static int f_create_repo(char force){
  int rc;
  fsl_cx * f = fcli.f;
  fsl_repo_create_opt opt = fsl_repo_create_opt_empty;
  char const * mfile = FNewApp.filename;
  char * userName = 0;
  if(FNewApp.configRepo){
    f_out("Copying configuration from: %s\n", FNewApp.configRepo);
  }
  opt.allowOverwrite = force;
  opt.filename = mfile;
  opt.commitMessage = FNewApp.comment;
  opt.commitMessageMimetype = FNewApp.commentMimetype;
  opt.configRepo = FNewApp.configRepo;
  {
    /* Problem: when creating the repo, the fsl_cx gets closed, which will
       free its user string. Thus we need our own copy to ensure that it survives
       the repo setup process. */
    const char * u = fsl_cx_user_get(f)
      /* might be set up to -U|--user=name */;
    if(u){
      userName = fsl_strdup(u);
      fcli_fax(userName);
    }
  }
  opt.username = userName;
  rc = fsl_repo_create(f, &opt);
  if(!rc){
    fsl_db * db = fsl_cx_db_repo(f);
    fsl_stmt st = fsl_stmt_empty;
    char * s;
    f_out("Created repository: %s\n", mfile);
    assert(db);
    fsl_cx_hash_policy_set(f, FNewApp.hashPolicy);
#define CONF(KEY) s = fsl_config_get_text( f, FSL_CONFDB_REPO, KEY, NULL); \
    f_out("%-15s= %s\n", KEY, s); \
    fsl_free(s)
    CONF("server-code");
    CONF("project-code");
    CONF("hash-policy");
#undef CONF
    rc = fsl_db_prepare(db, &st,
                        "SELECT login,pw FROM user WHERE uid=1");
    assert(!rc);
    rc = fsl_stmt_step(&st);
    assert(FSL_RC_STEP_ROW==rc);
    if(FSL_RC_STEP_ROW==rc){
      rc = 0;
      f_out("%-15s= %s (password=%s)\n",
             "admin-user",
             fsl_stmt_g_text(&st, 0, NULL),
             fsl_stmt_g_text(&st, 1, NULL)
             );
    }
    fsl_stmt_finalize(&st);
    if(db->error.code){
      fsl_cx_uplift_db_error(f, db);
    }
  }
  /* fcli_err_report(0); */
  return rc;
}

static int handle_hash_policy(){
  const char * zPol = 0;
  if(!fcli_flag2("h","hash",&zPol)){
    return 0;
  }
#define P(S,T) if(0==strcmp(S,zPol)){ \
    FNewApp.hashPolicy = T; return 0; \
  }
  P("sha3",FSL_HPOLICY_SHA3);
  P("sha3-only",FSL_HPOLICY_SHA3_ONLY);
  P("auto",FSL_HPOLICY_SHA3/*yes, SHA3*/);
  P("sha1",FSL_HPOLICY_SHA1);
  P("shun-sha1",FSL_HPOLICY_SHA3);
#undef P
  return fcli_err_set(FSL_RC_MISUSE,"Invalid --hash policy value.");
}

int main(int argc, char * const * argv ){
  int rc = 0;
  fsl_cx * f;
  char force;
  fcli.checkoutDir = NULL /* Same effect as -C/--no-checkout */;
  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;
  assert(!fsl_cx_db_repo(f));
  fcli_flag2("c", "config", &FNewApp.configRepo);
  fcli_flag2("m", "message", &FNewApp.comment);
  fcli_flag2("N", "mimetype", &FNewApp.commentMimetype);
  rc = handle_hash_policy();
  if(rc) goto end;

  force = fcli_flag2("F", "force", NULL);
  fcli_flag_or_arg("f", "file", &FNewApp.filename);
  if(!FNewApp.filename){
    rc = fcli_err_set(FSL_RC_MISUSE,
                      "Missing filename argument. "
                      "Try --help.");
    goto end;
  }
  if(fcli_has_unused_flags(0)) goto end;
  rc = f_create_repo(force);
  end:
  return fcli_err_report(1) ? EXIT_FAILURE : EXIT_SUCCESS;
}
