/*
 * repo_products.c
 *
 * Parses all files below 'proddir'
 * See http://en.opensuse.org/Product_Management/Code11
 *
 *
 * Copyright (c) 2008, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <dirent.h>
#include <ctype.h>

#include "pool.h"
#include "repo.h"
#include "util.h"
#define DISABLE_SPLIT
#include "tools_util.h"
#include "repo_releasefile_products.h"

#define BUFF_SIZE 8192

static void
add_releasefile_product(Repo *repo, FILE *fp)
{
  Pool *pool = repo->pool;
  char buf[BUFF_SIZE];
  Id name = 0;
  Id arch = 0;
  Id version = 0;
  int lnum = 0; /* line number */
  char *ptr, *ptr1;

  /* parse /etc/<xyz>-release file */
  while (fgets(buf, sizeof(buf), fp))
    {
      /* remove trailing \n */
      int l = strlen(buf);
      if (l && buf[l - 1] == '\n')
	buf[--l] = 0;
      ++lnum;

      if (lnum == 1)
	{
	  /* 1st line, <name> [(<arch>)] */
	  ptr = strchr(buf, '(');
	  if (ptr)
	    {
	      ptr1 = ptr - 1;
	      *ptr++ = 0;
	    }
	  else
	    ptr1 = buf + l - 1;

	  /* track back until non-blank, non-digit */
	  while (ptr1 > buf
		 && (*ptr1 == ' ' || isdigit(*ptr1) || *ptr1 == '.'))
	    --ptr1;
	  *(++ptr1) = 0;
	  name = str2id(pool, join2("product", ":", buf), 1);

	  if (ptr)
	    {
	      /* have arch */
	      char *ptr1 = strchr(ptr, ')');
	      if (ptr1)
		{
		  *ptr1 = 0;
		  /* downcase arch */
		  ptr1 = ptr;
		  while (*ptr1)
		    {
		      if (isupper(*ptr1))
			 *ptr1 = tolower(*ptr1);
		      ++ptr1;
		    }
		  arch = str2id(pool, ptr, 1);
		}
	    }
	}
      else if (strncmp(buf, "VERSION", 7) == 0)
	{
	  ptr = strchr(buf + 7, '=');
	  if (ptr)
	    {
	      while (*++ptr == ' ')
		;
	      version = makeevr(pool, ptr);
	    }
	}
    }
  if (name)
    {
      Solvable *s = pool_id2solvable(pool, repo_add_solvable(repo));
      s->name = name;
      s->evr = version ? version : ID_EMPTY;
      s->arch = arch ? arch : ARCH_NOARCH;
      if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
	s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
    }
}


void
repo_add_releasefile_products(Repo *repo, const char *dirpath, int flags)
{
  DIR *dir;
  struct dirent *entry;
  FILE *fp;
  char *fullpath;

  dir = opendir(dirpath);
  if (!dir)
    return;

  while ((entry = readdir(dir)))
    {
      int len = strlen(entry->d_name);
      if (len > 8 && !strcmp(entry->d_name + len - 8, "-release"))
        {
	  /* skip /etc/lsb-release, thats not a product per-se */
	  if (strcmp(entry->d_name, "lsb-release") == 0)
	    continue;
	  fullpath = join2(dirpath, "/", entry->d_name);
	  if ((fp = fopen(fullpath, "r")) == 0)
	    {
	      perror(fullpath);
	      continue;
	    }
	  add_releasefile_product(repo, fp);
	  fclose(fp);
	}
    }
  closedir(dir);
  join_freemem();

  if (!(flags & REPO_NO_INTERNALIZE) && (flags & REPO_REUSE_REPODATA) != 0)
    repodata_internalize(repo_last_repodata(repo));
}

