/*
 * Routine to convert a UNIX-style path to an AmigaDOS path.
 * A UNIX-style path is understood here as follows:
 * 
 *      leading . means current dir; like "", but filled in explicitly
 *      ../ means /
 *      ..  means /
 * 
 * Written as part of UnixDirs2, a (to be written) system patch which allows
 * use of UNIX-style paths everywhere.
 * 
 * Martin W. Scott,  8 January 1993
 */
#include <exec/types.h>
#include <exec/execbase.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>
#include <pragmas/dos_lib.h>
#include <pragmas/exec_lib.h>

#include <string.h>
#include <stdio.h>

extern struct ExecBase *SysBase;

/* insert $cwd into s, return pointer to next free char */
static char *
insertcwd (char *s, LONG len)
{
  /* geta4 (); */

  if (NameFromLock (((struct Process *) (SysBase->ThisTask))->pr_CurrentDir, s, len))
    {
      while (*s)
	s++;
    }
  return s;
}

/* adjust path from UNIX-style to Amiga-style */
/* TO DO: length checking when building new path */
static BOOL
adjustpath (char *path, char *newpath, LONG len)
{
  char tmp_buf[512] = "", *s = tmp_buf, *t;

  if (path == NULL)		/* bypass */
    return FALSE;

  if (t = strchr (path, ':'))	/* check for ':' in path */
    {
      t++;			/* copy device component */
      while (path < t)
	*s++ = *path++;
    }
  else if (path[0] == '/')
    {
      path++;

      if (t = strchr (path, '/'))
	{
	  while (path < t)
	    *s++ = *path++;

	  *s++ = ':', path++;
	}
      else
	{
	  while (*path)
	    *s++ = *path++;

	  *s++ = ':';
	}
    }
  else if (path[0] == '.')
    {
      if (!path[1])		/* only "." */
	{
	  s = insertcwd (s, len);
	  path++;		/* path[0] == '\0' - STOP */
	}
      else if (path[1] == '/')	/* initial component is $cwd */
	{
	  s = insertcwd (s, len);
	  if (*(s - 1) != ':')
	    *s++ = '/';		/* copy '/', increment pointers */
	  path += 2;
	}
    }

  while (path[0])
    {
      if (path[0] == '.' && path[1] == '.')
	{
	  if (path[2] == '/')	/* just skip "..", copying '/' */
	    {
	      path += 2;
	      *s++ = *path++;
	    }
	  else if (!path[2])	/* append '/' and stop */
	    {
	      path += 2;
	      *s++ = '/';
	    }
	  else
	    *s++ = *path++;
	}
      else
	*s++ = *path++;
    }
  *s = '\0';

  strcpy (newpath, tmp_buf);

  return TRUE;
}

int
wedge ()
{
  int retval = 0;

  char *copy_of_argstring;

  if (copy_of_argstring = strdup (GetArgStr ()))
    {
      BPTR lock;

      /*
       * buffers should be big enouth to handle all but the most 
       * extreme of extreme cases
       */
      char tmp_buf1[512] = "", tmp_buf2[512] = "", *ptr = copy_of_argstring;

      /*
       * argstring is terminated with a newline character for the 
       * benifit of ReadArgs().
       */
      if (strlen (copy_of_argstring) == 1)
	{
	  free (copy_of_argstring);
	  return (retval);
	}

      if (lock = Lock (strcpy (tmp_buf1, strsep (&ptr, " \t\n")), SHARED_LOCK))
	{
	  if (NameFromLock (lock, tmp_buf1, 512))
	    {
	      FILE *fd;

	      if (fd = fopen (tmp_buf1, "r"))
		{
		  fgets (tmp_buf2, 512, fd);

		  if ((tmp_buf2[0] == '#') && (tmp_buf2[1] == '!'))
		    {
		      adjustpath (strtok (&tmp_buf2[2], " \t\n"), tmp_buf2, 512);

		      {
			char command[512];

			sprintf (command, "\"%s\" \"%s\" %s", tmp_buf2, tmp_buf1, ptr);

			SystemTags (command, 0L);
		      }

		      retval = 1;
		    }

		  fclose (fd);
		}
	    }
	  UnLock (lock);
	}
      free (copy_of_argstring);
    }
  return (retval);
}
