/* tagrev.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/tag.h"



static t_uchar * program_name = "tagrev";
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_log, "l", "log FILE", 1, \
      "commit with log file FILE") \
  OP (opt_seal, 0, "seal", 0, \
      "create a version-0 revision") \
  OP (opt_fix, 0, "fix", 0, \
      "create a versionfix revision")


static t_uchar long_help[] = ("create a continuation revision (aka tag or branch)\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 * log_file = 0;
  int seal = 0;
  int fix = 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_log:
          {
            log_file = str_save (0, option->arg_string);
            break;
          }

        case opt_seal:
          {
            seal = 1;
            break;
          }

        case opt_fix:
          {
            fix = 1;
            break;
          }
	}
    }

  if (argc != 3)
    goto usage_error;

  {
    t_uchar * source_spec;
    t_uchar * tag_spec;
    t_uchar * source_archive = 0;
    t_uchar * source_revision = 0;
    t_uchar * tag_archive = 0;
    t_uchar * tag_version = 0;
    t_uchar * log = 0;
    struct arch_archive * source_arch = 0;
    struct arch_archive * tag_arch = 0;
    rel_table tag_version_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 * tag_revision = 0;
    
    if (default_archive)
      {
        if (!arch_valid_archive_name (default_archive))
           {
            safe_printfmt (2, "tagrev: invalid archive name (%s)\n", default_archive);
            exit (1);
          }
      }

    source_spec = argv[1];
    tag_spec = argv[2];

    if (!arch_valid_package_name (source_spec, arch_maybe_archive, arch_req_patch_level, 0))
      {
        safe_printfmt (2, "tagrev: invalid revision name -- %s\n", source_spec);
        exit (1);
      }
    if (!arch_valid_package_name (tag_spec, arch_maybe_archive, arch_req_version, 0))
      {
        safe_printfmt (2, "tagrev: invalid version name -- %s\n", tag_spec);
        exit (1);
      }


    source_archive = arch_parse_package_name (arch_ret_archive, default_archive, source_spec);
    source_revision = arch_parse_package_name (arch_ret_non_archive, 0, source_spec);

    tag_archive = arch_parse_package_name (arch_ret_archive, default_archive, tag_spec);
    tag_version = arch_parse_package_name (arch_ret_non_archive, default_archive, tag_spec);

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

    source_arch = arch_archive_connect (source_archive);

    if (!str_cmp (source_archive, tag_archive))
      tag_arch = source_arch;
    else
      tag_arch = arch_archive_connect (tag_archive);

    tag_version_revisions = arch_archive_revisions (tag_arch, tag_version, 0);

    if (!tag_version_revisions)
      {
        desired_level_type = arch_is_base0_level;
        desired_n = 0;
      }
    else
      {
        last_level = str_save (0, tag_version_revisions[rel_n_records (tag_version_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, "tagrev: 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, "tagrev: 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, "tagrev: version already sealed");
                  exit (2);
                }
              else if (fix)
                {
                  desired_level_type = arch_is_versionfix_level;
                  desired_n = 1;
                }
              else
                {
                  safe_printfmt (2, "tagrev: cannot commit to sealed version with --fix");
                  exit (2);
                }
              break;
            }
        
          case arch_is_versionfix_level:
            {
              if (seal)
                {
                  safe_printfmt (2, "tagrev: version already sealed");
                  exit (2);
                }
              else if (fix)
                {
                  desired_level_type = arch_is_versionfix_level;
                  desired_n = last_n + 1;
                }
              else
                {
                  safe_printfmt (2, "tagrev: cannot commit to sealed version with --fix");
                  exit (2);
                }
              break;
            }
          }
      }

    desired_level = arch_form_patch_level (desired_level_type, desired_n);
    tag_revision = str_alloc_cat_many (0, tag_version, "--", desired_level, str_end);

    arch_tag (1, tag_arch, tag_revision, source_arch, source_revision, log);

    safe_printfmt (1, "* made tag %s/%s => %s/%s \n", tag_archive, tag_revision, source_archive, source_revision);

    if (log_file)
      safe_unlink (log_file);

    lim_free (0, source_archive);
    lim_free (0, source_revision);
    lim_free (0, tag_archive);
    lim_free (0, tag_version);
    lim_free (0, log);
    if (source_arch != tag_arch)
      arch_archive_close (source_arch);
    arch_archive_close (tag_arch);
    rel_free_table (tag_version_revisions);
    lim_free (0, last_level);
    lim_free (0, desired_level);
    lim_free (0, tag_revision);
  }

  lim_free (0, default_archive);

  return 0;
}



/* tag: Tom Lord Tue May 27 23:03:10 2003 (tagrev.c)
 */
