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

#include "hackerlab/bugs/panic.h"
#include "hackerlab/char/char-class.h"
#include "hackerlab/char/str.h"
#include "hackerlab/fs/file-names.h"
#include "hackerlab/fs/cwd.h"
#include "hackerlab/vu/safe.h"
#include "file-utils/libfsutils/ensure-dir.h"
#include "file-utils/libfsutils/safety.h"
#include "file-utils/libfsutils/read-line.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/configs.h"



t_uchar *
arch_config_path (t_uchar * tree_root, t_uchar * config_name)
{
  t_uchar * rel = 0;
  t_uchar * answer = 0;

  invariant (arch_valid_config_name (config_name));


  rel = file_name_in_vicinity (0, "configs", config_name);
  answer = file_name_in_vicinity (0, tree_root, rel);

  lim_free (0, rel);
  return answer;
}


rel_table
arch_read_config (t_uchar * tree_root, t_uchar * config_name)
{
  t_uchar * config_path = 0;
  int in_fd;
  rel_table answer = 0;

  invariant (arch_valid_config_name (config_name));

  config_path = arch_config_path (tree_root, config_name);

  in_fd = safe_open (config_path, O_RDONLY, 0);

  while (1)
    {
      t_uchar * line = 0;
      t_uchar * pos;

      line = read_line_from_fd (in_fd);

      if (!line)
        break;

      for (pos = line; char_is_space (*pos); ++pos)
        ;

      if (*pos && (*pos != '#'))
        {
          t_uchar * end;
          t_uchar * loc = 0;
          t_uchar * rev = 0;

          for (pos = line; char_is_space (*pos); ++pos)
            ;

          for (end = pos; *end && !char_is_space (*end); ++end)
            ;

          if (end == pos)
            {
              safe_printfmt (2, "arch_read_config:  illegal config file (%s)\n", config_name);
              exit (2);
            }

          loc = str_save_n (0, pos, end - pos);

          for (pos = end; char_is_space (*pos); ++pos)
            ;

          ;for (end = pos; *end && !char_is_space (*end); ++end)
            ;

          if (end == pos)
            {
              safe_printfmt (2, "arch_read_config:  illegal config file (%s)\n", config_name);
              exit (2);
            }

          rev = str_save_n (0, pos, end - pos);
          
          if (!is_non_upwards_relative_path (loc) || !arch_valid_package_name (rev, arch_maybe_archive, arch_req_package, 1))
            {
              safe_printfmt (2, "arch_read_config:  illegal config file (%s)\n", config_name);
              exit (2);
            }

          rel_add_records (&answer, rel_make_record (loc, rev, 0), 0);

          lim_free (0, loc);
          lim_free (0, rev);
        }

      lim_free (0, line);
    }

  lim_free (0, config_path);

  return answer;
}


rel_table
arch_config_from_tree (t_uchar * tree_root, rel_table config_in)
{
  int here_fd;
  int x;
  rel_table answer = 0;

  here_fd = safe_open (".", O_RDONLY, 0);

  for (x = 0; x < rel_n_records (config_in); ++x)
    {
      t_uchar * subtree_path_spec = 0;
      t_uchar * subtree_path = 0;
      t_uchar * archive = 0;
      t_uchar * version = 0;
      t_uchar * subtree_root = 0;
      t_uchar * level = 0;
      t_uchar * revision = 0;
      t_uchar * fqr = 0;

      subtree_path_spec = file_name_in_vicinity (0, tree_root, config_in[x][0]);
      safe_chdir (subtree_path_spec);
      subtree_path = safe_current_working_directory ();
      safe_fchdir (here_fd);

      subtree_root = arch_tree_root (0, subtree_path, 0);
      invariant (subtree_root && !str_cmp (subtree_root, subtree_path));

      archive = arch_parse_package_name (arch_ret_archive, 0, config_in[x][1]);
      if (arch_valid_package_name (config_in[x][1], arch_maybe_archive, arch_req_version, 1))
        version = arch_parse_package_name (arch_ret_package_version, 0, config_in[x][1]);
      else
        {
          t_uchar * package = 0;

          package = arch_parse_package_name (arch_ret_package, 0, config_in[x][1]);
          version = arch_latest_logged_vesion (subtree_root, archive, package);

          lim_free (0, package);
        }

      level = arch_highest_patch_level (subtree_root, archive, version);

      if (level)
        {
          revision = str_alloc_cat_many (0, version, "--", level, str_end);
          fqr = arch_fully_qualify (archive, revision);
        }
      else
        fqr = arch_fully_qualify (archive, version);

      rel_add_records (&answer, rel_make_record (config_in[x][0], fqr, 0), 0);

      lim_free (0, subtree_path_spec);
      lim_free (0, subtree_path);
      lim_free (0, archive);
      lim_free (0, version);
      lim_free (0, subtree_root);
      lim_free (0, level);
      lim_free (0, revision);
      lim_free (0, fqr);
    }


  safe_close (here_fd);

  return answer;
}


int
arch_begin_new_config (t_uchar * tree_root, t_uchar * name, int force)
{
  int ign;
  t_uchar * config_file = 0;
  t_uchar * config_dir = 0;
  t_uchar * name_tail = 0;
  t_uchar * tmp_tail = 0;
  t_uchar * config_tmp = 0;
  int answer;

  invariant (arch_valid_config_name (name));

  config_file = arch_config_path (tree_root, name);
  config_dir = file_name_directory_file (0, config_file);
  name_tail = file_name_tail (0, name);
  tmp_tail = str_alloc_cat (0, ",,", name_tail);
  config_tmp = file_name_in_vicinity (0, config_dir, tmp_tail);

  if (!force && !safe_access (config_file, F_OK))
    {
      safe_printfmt (2, "arch_begin_new_config: config already exists (%s)\n", name);
      exit (2);
    }

  ensure_directory_exists (config_dir);

  vu_unlink (&ign, config_tmp);
  answer = safe_open (config_tmp, O_WRONLY | O_CREAT | O_EXCL, 0666);

  lim_free (0, config_file);
  lim_free (0, config_dir);
  lim_free (0, name_tail);
  lim_free (0, tmp_tail);
  lim_free (0, config_tmp);

  return answer;
}


void
arch_finish_new_config (int fd, t_uchar * tree_root, t_uchar * name, int force)
{
  t_uchar * config_file = 0;
  t_uchar * config_dir = 0;
  t_uchar * name_tail = 0;
  t_uchar * tmp_tail = 0;
  t_uchar * config_tmp = 0;

  invariant (arch_valid_config_name (name));

  config_file = arch_config_path (tree_root, name);
  config_dir = file_name_directory_file (0, config_file);
  name_tail = file_name_tail (0, name);
  tmp_tail = str_alloc_cat (0, ",,", name_tail);
  config_tmp = file_name_in_vicinity (0, config_dir, tmp_tail);

  safe_close (fd);

  if (!force && !safe_access (config_file, F_OK))
    {
      safe_printfmt (2, "arch_begin_new_config: config already exists (%s)\n", name);
      exit (2);
    }

  safe_rename (config_tmp, config_file);

  lim_free (0, config_file);
  lim_free (0, config_dir);
  lim_free (0, name_tail);
  lim_free (0, tmp_tail);
  lim_free (0, config_tmp);
}



/* tag: Tom Lord Fri May 30 00:05:24 2003 (configs.c)
 */
