/* Copyright (C) 1996,1997 Robert H”hne, see COPYING.RH for details */
/* This file is part of RHIDE. */
#include <rhide.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>

static char buffer[PATH_MAX];

/*
   This function checks at first, if the file is in the current
   directory. Then it searches in $(DJDIR)/data and if not exist,
   it returns the file name as it would exist in the directory,
   where this program is located without checking if it exists.
   This is a little different to that, what SET means with the
   function name, but I prefer not to store configuration
   files in the bin directory.

   On other systems than DJGPP the following directories are searched:
   .
   $(HOME)
   /usr/local/share/rhide
   /usr/share/rhide
   /local/share/rhide
   /share/rhide
*/

char *ExpandFileNameToThePointWhereTheProgramWasLoaded(char *s)
{
  if (__file_exists(s))
  {
    strcpy(buffer,s);
    return buffer;
  }
#ifndef __DJGPP__
  // now try the home directory
  char *home = getenv("HOME");
  if (home)
  {
    strcpy(buffer,home);
    strcat(buffer,"/");
    strcat(buffer,s);
    if (__file_exists(buffer)) return buffer;
  }
  // now try in some standard places
  strcpy(buffer,"/usr/local/share/rhide/");
  strcat(buffer,s);
  if (__file_exists(buffer)) return buffer;
  strcpy(buffer,"/usr/share/rhide/");
  strcat(buffer,s);
  if (__file_exists(buffer)) return buffer;
  strcpy(buffer,"/local/share/rhide/");
  strcat(buffer,s);
  if (__file_exists(buffer)) return buffer;
  strcpy(buffer,"/share/rhide/");
  strcat(buffer,s);
  if (__file_exists(buffer)) return buffer;
#else
  char *dj = getenv("DJDIR");
  if (dj)
  {
    strcpy(buffer,dj);
    strcat(buffer,"/share/rhide/");
    strcat(buffer,s);
    if (__file_exists(buffer)) return buffer;
  }
#endif
  strcpy(buffer,RHIDE_DIR);
  strcat(buffer,s);
  return buffer;
}


