/***********************************************************************
*
*  module   : Hfd
*  commande : HfdName
*  fichier  : HfdName.c
*  Auteur   : Ducloy
*  Date     : Avril 95
*  $Id: DamHfdName.c,v 1.5 2005/06/22 12:50:46 parmentf Exp $
*
************************************************************************
*
* Copyright (c) 1995 CNRS/CRIN & INRIA Lorraine
* 
************************************************************************/
#include <sys/param.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "Hfd.h"
char *getcwd();

char *pathname;

static Buffer *bufPath;
static int flagBufpath=0;
static char *movePath;


/**
   Says wether a @a name is a DamNameDir.

   @param name name of a directory to test
   @return 0 if @a name is not a Dam Directory Name 
                          (should end with .hfd (?))
 */
int HfdNameIsDir(char *name)
{
  char *suff;
  int l;

  l=strlen(name);
  suff=name+l-1;
  if (l<4)return 0;
  if (*suff-- != 'd')return 0;
  switch (*suff--)
    {
    case 'd':
    case 'h':
      if (*suff-- == '.') return 1;
      return 0;
    case 'f':
      if (*suff-- != 'h') return 0;
      if (l<5) return 0;
      if (*suff !='.') return 0;
      return 1;
    default:
      return 0;
    }
}


/**
   Says wether a @a name is a DamNameFile.

   @param name name of a file to test
   @return 0 if @a name is not a Dam File Name (should end with .df)
 */
int HfdNameIsFile(char *name)
{
  char *suff;
  int l;

  l=strlen(name);
  suff=name+l-1;
  if (l<4)return 0;
  if (*suff-- != 'f')return 0;
  if (*suff-- != 'd')return 0;
  if (*suff != '.') return 0;
  return 1;
}



void initBufPath()
{ 
  if(!flagBufpath)
    {
      char *pos1;
      char *pos2;
      char *pos3;
      char *str2;
      
      flagBufpath=1;
      bufPath=BufferCreate(MAXPATHLEN,10);

      pathname=(char *)malloc(MAXPATHLEN+2);
      pathname=getcwd(pathname, MAXPATHLEN+2);

      if ((movePath=getenv("DILIB_MOVE_PATH")))
	{
	  pos1=movePath;
	  while ((pos2=strchr(pos1,':')))
	    {
	      if (strncmp(pos1, pathname, pos2-pos1)==0)
		{
		  str2=(char *)malloc(MAXPATHLEN+2);
		  if ((pos3=strchr(pos2+1,':')))
		    {
		      strncpy(str2, pos2+1, pos3-pos2-1);
		      str2[pos3-pos2-1]='\0';
		    }
		  else
		    {
		      strcpy(str2, pos2+1); 
		    }
		  strcat(str2, pathname+(pos2-pos1));
		  free(pathname);
		  pathname=str2;
		  break;
		}
	      else
		{
		  if((pos1=strchr(pos2+1,':')))pos1++;
		  else break;
		}
	    }
	}
    }
}

/**
   Returns the absolute path matching @a s1.

   If @a s1 does not begin with @c /, adds the current working
   directory before @a s1.

   Else, it returns @a s1.

   @param s1 a filepath
   @returns @a s1 translated into an absolute path.
 */
char *HfdAbsolutePath(char *s1)
{
  char c1;
  char *s2;
  initBufPath();
  if(s1&&(c1=s1[0]))
    {
      if (c1=='/')
	{
	  s2=BufferStrcpy(bufPath,s1);
	}
      else
	{
	  BufferStrcpy(bufPath,pathname);
	  BufferStrcat(bufPath,"/");
	  s2=BufferStrcat(bufPath,s1);
	}
      return s2;
    } 
  return NULL;
}

/**
   Returns the basename of the filepath given (@a s1).
   If @a s1 contains a @c /, it returns only what is after it.
   Else, returns @a s1.

   @param s1 file path
   @return the basename of the path.
 */
char *HfdBaseName(char *s1)
{
  if(s1)
    {
      char *s2;
      char *s3;
      if ((s2=strrchr(s1,'/')))
	{
	  s3=BufferStrcpy(bufPath,s2+1);
	}
      else s3=BufferStrcpy(bufPath,s1);
      return s3;
    }
  return NULL;
}

char *HfdName(s1)
     char *s1;
{
  initBufPath();
  if(s1)
    {
      /* WARNING: There should be something here! */
    }
  else return NULL;
  return NULL;
}

char *HfdPath(s1)
     char *s1;
{
  char *s2;
  
  initBufPath();
  if((s2=HfdAbsolutePath(s1)))
    {
      if (BufferTailCmp(bufPath,".hfd")==0)return s2;
      if ((BufferTailCmp(bufPath,".hid")==0)
	  ||(BufferTailCmp(bufPath,".hcs")==0))
	return BufferTailReplace(bufPath,".hfd");
      return (BufferStrcat(bufPath,".hfd"));
      }
  else return NULL;
}

char *HfdPathRadical(s1)
     char *s1;
{
  char *s2;
  
  initBufPath();
  if((s2=HfdAbsolutePath(s1)))
    {
      if ((BufferTailCmp(bufPath,".hfd")==0)
	  ||(BufferTailCmp(bufPath,".hid")==0)
	  ||(BufferTailCmp(bufPath,".hcs")==0))
	return BufferTailCut(bufPath,4);
      return s2;
      }
  else return NULL;
}

/**
   HfdGetPath returns a pointer on an internal buffer which contains a
   name or path derived from string s1 according to codes f1 and t1.

   @param s1 absolute path to an Hfd base (ending with .bib)

   @param f1 code (d,r,h,i,c) type of the returned filepath (h for
             hfd, i for hid, or c for hcs, d for directory - after any
             @c /).

   @param t1 code (A,I,B): B for basename (relative path), A for
             absolute path, and B for basename (no @c / in path).

   @return
 */
char *HfdGetPath(char *s1, char f1, char t1)
{
  char *s2;
  char f2;
  initBufPath();
  switch(t1)
    {
    case 'A':
      if((s2=HfdAbsolutePath(s1)))break;
      return NULL;
    case 'I':
      s2=BufferStrcpy(bufPath,s1);
      break;
    case 'B':
      if((s2=HfdBaseName(s1)))break;
     return NULL;
    default:
      return NULL;
    }
 
  if(f1=='d')
    {
      static char *s3=NULL;
      char *pos2;
      if(s3)free(s3);
      s3=strdup(s2);
      if ((pos2=strrchr(s3,'/')))
	{
	  *pos2='\0';
	  return s3;
	}
      else return NULL;
    }
  if (BufferTailCmp(bufPath,".hfd")==0)f2='h';
  else if (BufferTailCmp(bufPath,".hid")==0)f2='i';
  else if (BufferTailCmp(bufPath,".hcs")==0)f2='c';
  else f2='r';
  if (f1==f2)return s2;
  switch(f2)
    {
    case 'h':
    case 'i':
    case 'c':
      s2=BufferTailCut(bufPath,4);
      break;
    }
  switch(f1)
    {
    case 'r':
      return s2;
    case 'h':
      return (BufferStrcat(bufPath,".hfd"));
    case 'i':
      return (BufferStrcat(bufPath,".hid"));
    case 'c':
      return (BufferStrcat(bufPath,".hcs"));
    }
  return NULL;
}

