/* $Id: riscos-csd 3.2.0.1 1998/07/26 10:23:14 stoklund Exp stoklund $ */
/* riscos-csd.c: Handle current directory when multitasking.

   Copyright (C) 1998 Jakob Stoklund Olesen

   This library is free software; you can redistribute it and / or modify
   it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.

   This library is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 - 1307,
   USA. */

#include "kpathsea:config.h"
#include "kpathsea:absolute.h"

#include "kpathsea:riscos.h"

const_string riscos_prefix = NULL;
int riscos_prefix_length = 0;
boolean riscos_desktop_flag = false;

/*
   The currently set directory is one of (in priority order):
   Directory holding basename(file) (if it is absolute)
   <Prefix$Dir> --- set by the DDEUtils module.
   riscos_canonicalise("@") --- to remain constant in a task window.
   "@" --- if canonicalisation fails.
 */

void
riscos_initialise_prefix (const_string file)
{
  /* Should we use the directory part of file? */
  if (riscos_desktop_flag && file != NULL)
    {
      string full;
      const_string base;

      if (riscos_prefix != NULL)
	{
	  full = riscos_canonicalise (file, riscos_prefix);
	  free ((string) riscos_prefix);
	}
      else
	full = riscos_canonicalise (file, "<Prefix$Dir>.,");
      base = basename (full);
      riscos_prefix_length = base - full;
      full[riscos_prefix_length] = '\0';
      riscos_prefix = xrealloc (full, riscos_prefix_length + 1);
      /* Don't do this again */
      riscos_desktop_flag = false;
    }
  /* Otherwise set it from Prefix$Dir / @ */
  else if (riscos_prefix == NULL)
    {
      string prefix = getenv ("Prefix$Dir");
      string full;

      if (!prefix || !*prefix)
	prefix = "@";
      full = riscos_canonicalise (prefix, NULL);
      /* remove the 'x' leafname */
      riscos_prefix_length = strlen (full);
      if (full[riscos_prefix_length - 1] != ':')
	{
	  full[riscos_prefix_length++] = '.';
	  full = xrealloc (full, riscos_prefix_length + 1);
	  full[riscos_prefix_length] = '\0';
	}
      riscos_prefix = full;
    }

#ifdef KPSE_DEBUG
  if (KPSE_DEBUG_P (KPSE_DEBUG_PATHS))
    {
      DEBUGF1 ("riscos_prefix = %s\n", riscos_prefix);
    }
#endif
}

/*
   Ensure that NAME is an absolute filename and return it in fresh memory.
 */
string
riscos_ensure_prefix (const_string name)
{
  assert (name != NULL);

  /* Absolute filenames are fine */
  if (kpse_absolute_p (name, false))
    return xstrdup (name);

  /* This is a bit unfortunate if desktop_flag is set, but we can't do
     anything. */
  if (riscos_prefix == NULL)
    {
      riscos_initialise_prefix (NULL);
      if (riscos_desktop_flag)
	WARNING2 ("using prefix `%s' for `%s'", riscos_prefix, name);
    }

  /* Should we canonicalise? */
  if (name[0] == '@' && name[1] == '.')
    return concat (riscos_prefix, name + 2);
  else
    return concat (riscos_prefix, name);
}
