/* -*- 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/
  
  *****************************************************************************
*/

#include "fossil-scm/fossil-cli.h" /* Fossil App mini-framework */
#include "fossil-scm/fossil-internal.h"
#include <time.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--config|-c=repo_db_file Copies parts of the "
       "configuration from the given repo.\n");
  
  puts("\t--message|-m=text Sets the commit message for the initial commit.\n");
  
  puts("\t--file|-f=repo_file_to_create. The filename may optionally "
       "be provided as the first non-flag parameter.\n");
  
}


static struct FNewApp_{
  char * comment;
  char * filename;
  char * configRepo;
} FNewApp = {
NULL/*comment*/,
NULL/*filename*/,
NULL/*configRepo*/
};

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;
  if(FNewApp.configRepo){
    f_out("Copying configuration from: %s\n", FNewApp.configRepo);
  }
  opt.allowOverwrite = force;
  opt.filename = mfile;
  if(FNewApp.comment){
    opt.commitMessage = FNewApp.comment;
  }
  opt.configRepo = FNewApp.configRepo;
  opt.username = fsl_cx_user_get(f)
    /* might be set up to -U|--user=name */;
  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);
#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");
#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;

}


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);
  force = fcli_flag("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:
  fsl_free(FNewApp.comment);
  fsl_free(FNewApp.filename);
  fsl_free(FNewApp.configRepo);
  return fcli_err_report(1) ? EXIT_FAILURE : EXIT_SUCCESS;
}
