/* cmtrev.c
 *
 ****************************************************************
 * Copyright (C) 2003 Tom Lord
 * 
 * See the file "COPYING" for further information about
 * the copyright and warranty status of this work.
 */


#include "config-options.h"
#include "hackerlab/cmd/main.h"
#include "file-utils/libfsutils/file-contents.h"
#include "file-utils/libarch/namespace.h"
#include "file-utils/libarch/project-tree.h"
#include "file-utils/libarch/patch-logs.h"
#include "file-utils/libarch/archive.h"
#include "file-utils/libarch/proj-tree-lint.h"
#include "file-utils/libarch/commit.h"



static t_uchar * program_name = "cmtrev";
static t_uchar * usage = "[options] [[archive]/version ...]";
static t_uchar * version_string = (cfg__std__package " from regexps.com\n"
				   "\n"
				   "Copyright 2003 Tom Lord\n"
				   "\n"
				   "This is free software; see the source for copying conditions.\n"
				   "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
				   "PARTICULAR PURPOSE.\n"
				   "\n"
				   "Report bugs to " cfg__file_utils_bug_mail ".\n"
				   "\n");

#define OPTS(OP, OP2) \
  OP (opt_help_msg, "h", "help", 0, \
      "Display a help message and exit.") \
  OP (opt_long_help, "H", 0, 0, \
      "Display a verbose help message and exit.") \
  OP (opt_version, "V", "version", 0, \
      "Display a release identifier string") \
  OP2 (opt_version, 0, 0, 0, "and exit.") \
  OP (opt_archive, "A", "archive", 1, \
      "specify a default archive name") \
  OP (opt_dir, "d", "dir DIR", 1, \
      "cd to DIR first") \
  OP (opt_log, "l", "log FILE", 1, \
      "commit with log file FILE") \
  OP (opt_strict, 0, "strict", 0, \
      "strict tree-lint") \
  OP (opt_seal, 0, "seal", 0, \
      "create a version-0 revision") \
  OP (opt_fix, 0, "fix", 0, \
      "create a versionfix revision") \
  OP (opt_out_of_date, 0, "out-of-date-ok", 0, \
      "commit even if out of date")


static t_uchar long_help[] = ("archive a changeset-based revision\n");

enum options
{
  OPTS (OPT_ENUM, OPT_IGN)  
};

struct opt_desc opts[] = 
{
  OPTS (OPT_DESC, OPT_DESC)
    {-1, 0, 0, 0, 0}
};



int
main (int argc, char * argv[])
{
  int o;
  struct opt_parsed * option;
  t_uchar * default_archive = 0;
  t_uchar * dir = 0;
  t_uchar * log_file = 0;
  int strict = 0;
  int seal = 0;
  int fix = 0;
  int out_of_date_ok = 0;
  
  dir = str_save (0, ".");

  safe_buffer_fd (1, 0, O_WRONLY, 0);

  option = 0;

  while (1)
    {
      o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, program_name, usage, version_string, long_help, opt_help_msg, opt_long_help, opt_version);
      if (o == opt_none)
	break;
      switch (o)
	{
	default:
	  safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
	  panic ("internal error parsing arguments");

	usage_error:
	  opt_usage (2, argv[0], program_name, usage, 1);
	  exit (1);

          /* bogus_arg: */
	  safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", option->opt_string, option->arg_string);
	  goto usage_error;

        case opt_archive:
          {
            default_archive = str_save (0, option->arg_string);
            break;
          }

        case opt_dir:
          {
            dir = str_save (0, option->arg_string);
            break;
          }

        case opt_log:
          {
            log_file = str_save (0, option->arg_string);
            break;
          }

        case opt_strict:
          {
            strict = 1;
            break;
          }

        case opt_seal:
          {
            seal = 1;
            break;
          }

        case opt_fix:
          {
            fix = 1;
            break;
          }

        case opt_out_of_date:
          {
            out_of_date_ok = 1;
            break;
          }

          
	}
    }

  if (argc > 2)
    goto usage_error;

  {
    t_uchar * log = 0;
    t_uchar * vsnspec;
    t_uchar * tree_root = 0;
    t_uchar * archive = 0;
    t_uchar * version = 0;
    rel_table revisions = 0;
    t_uchar * last_level = 0;
    enum arch_patch_level_type last_level_type;
    enum arch_patch_level_type desired_level_type;
    t_ulong last_n;
    t_ulong desired_n;
    t_uchar * desired_level = 0;
    t_uchar * revision = 0;
    struct arch_archive * arch = 0;
    
    if (default_archive)
      {
        if (!arch_valid_archive_name (default_archive))
          {
            safe_printfmt (2, "cmtrev: invalid archive name (%s)\n", default_archive);
            exit (1);
          }
      }

    tree_root = arch_tree_root (0, dir, 0);

    if (!tree_root)
      {
        safe_printfmt (2, "cmtrev: not in project tree (%s)\n", dir);
        exit (1);
      }

    if (argc == 2)
      vsnspec = argv[1];
    else
      {
        vsnspec = arch_tree_version (tree_root);
        if (!vsnspec)
          {
            safe_printfmt (2, "cmtrev: project tree has no default version\n  tree: %s\n", tree_root);
            exit (1);
          }
      }

    if (!arch_valid_package_name (vsnspec, arch_maybe_archive, arch_req_version, 0))
      {
        safe_printfmt (2, "cmtrev: invalid version name -- %s\n", vsnspec);
        exit (1);
      }

    archive = arch_parse_package_name (arch_ret_archive, default_archive, vsnspec);
    version = arch_parse_package_name (arch_ret_non_archive, 0, vsnspec);

    if (!log_file)
      {
        log_file = arch_make_log_file (tree_root, archive, version);
        if (safe_access (log_file, F_OK))
          {
            safe_printfmt (2, "cmtrev: no log file provided (see `make-log')\n");
            exit (1);
          }
      }

    log = file_contents (log_file);
    if (!arch_valid_log_file (log))
      {
        safe_printfmt (2, "cmtrev: invalid log file (%s)\n", log_file);
        exit (1);
      }

    
    if (!arch_has_patch_log (tree_root, archive, version))
      {
        safe_printfmt (2, "cmtrev: tree has no patch log for version\n    tree: %s\n    version: %s/%s\n", tree_root, archive, version);
        exit (1);
      }

    {
      rel_table log_entries = 0;

      log_entries = arch_log_ls (tree_root, archive, version, 0);

      if (!log_entries)
        {
          safe_printfmt (2, "cmtrev: tree already has no patch log entries for version\n    tree: %s\n    version: %s/%s", tree_root, archive, version);
          exit (1);
        }
    }


    {
      struct arch_tree_lint_result * lint = 0;
      int status;

      lint = arch_tree_lint (tree_root);
      status = arch_print_tree_lint_report (2, lint);

      if ((status < 0) || (strict && status))
        {
          safe_printfmt (2, "cmtrev.c: commit aborted\n");
          exit (1);
        }
    }

    arch = arch_archive_connect (archive);

    revisions = arch_archive_revisions (arch, version, 0);

    if (!revisions)
      {
        safe_printfmt (2, "cmtrev: version has no revisions -- use revimport instead\n");
        exit (1);
      }

    last_level = str_save (0, revisions[rel_n_records (revisions) - 1][0]);

    last_level_type = arch_analyze_patch_level (&last_n, last_level);

    switch (last_level_type)
      {
      default:
        panic ("NOT IMPLEMENTED YET");
        panic ("internal error");
        break;

      case arch_is_base0_level:
        {
          if (seal)
            {
              desired_level_type = arch_is_version_level;
              desired_n = 0;
            }
          else if (fix)
            {
              safe_printfmt (2, "cmtrev: can not --fix before --seal");
              exit (2);
            }
          else
            {
              desired_level_type = arch_is_patch_level;
              desired_n = 1;
            }
        break;
        }
        
      case arch_is_patch_level:
        {
          if (seal)
            {
              desired_level_type = arch_is_version_level;
              desired_n = 0;
            }
          else if (fix)
            {
              safe_printfmt (2, "cmtrev: can not --fix before --seal");
              exit (2);
            }
          else
            {
              desired_level_type = arch_is_patch_level;
              desired_n = last_n + 1;
            }
          break;
        }
        
      case arch_is_version_level:
        {
          if (seal)
            {
              safe_printfmt (2, "cmtrev: version already sealed");
              exit (2);
            }
          else if (fix)
            {
              desired_level_type = arch_is_versionfix_level;
              desired_n = 1;
            }
          else
            {
              safe_printfmt (2, "cmtrev: cannot commit to sealed version with --fix");
              exit (2);
            }
          break;
        }
        
      case arch_is_versionfix_level:
        {
          if (seal)
            {
              safe_printfmt (2, "cmtrev: version already sealed");
              exit (2);
            }
          else if (fix)
            {
              desired_level_type = arch_is_versionfix_level;
              desired_n = last_n + 1;
            }
          else
            {
              safe_printfmt (2, "cmtrev: cannot commit to sealed version with --fix");
              exit (2);
            }
          break;
        }
      }

    if (!out_of_date_ok)
      {
        t_uchar * prev_revision = 0;
        t_uchar * prev_log_path = 0;

        prev_revision = str_alloc_cat_many (0, version, "--", last_level, str_end);
        prev_log_path = arch_log_file (tree_root, archive, prev_revision);

        if (safe_access (prev_log_path, F_OK))
          {
            safe_printfmt (2, "cmtrev: tree is not up-to-date (missing latest revision is %s/%s)\n", archive, prev_revision);
            exit (1);
          }
        
        lim_free (0, prev_revision);
        lim_free (0, prev_log_path);
      }

    desired_level = arch_form_patch_level (desired_level_type, desired_n);
    revision = str_alloc_cat_many (0, version, "--", desired_level, str_end);

    arch_commit (1, arch, revision, tree_root, log);

    safe_printfmt (1, "* commited %s/%s\n", archive, revision);

    safe_unlink (log_file);

    lim_free (0, log);
    lim_free (0, tree_root);
    lim_free (0, archive);
    lim_free (0, version);
    rel_free_table (revisions);
    lim_free (0, last_level);
    lim_free (0, desired_level);
    lim_free (0, revision);

  }

  lim_free (0, default_archive);
  lim_free (0, dir);

  return 0;
}




/* tag: Tom Lord Mon May 26 21:33:18 2003 (revcmt.c)
 */
