/*
 * 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 *s, *t;

  /*  geta4 (); */

  s = newpath;

  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] == '.')
    {
/*** translate '.' to $cwd ***/
      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])
    {
/*** copy remainder of path ***/
      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';

  return TRUE;
}

int
wedge ()
{
  int retval = 0;

  char *arg_str, *copy_of_arg_str;

  if (copy_of_arg_str = strdup (arg_str = GetArgStr ()))
    {
      char *script, *ptr = copy_of_arg_str;

      if (script = strsep (&ptr, " \t\n"))
	{
	  FILE *fd;

	  if (fd = fopen (script, "r"))
	    {
	      /* buffers should handle all but the most extreme of extreme cases */
	      char tmp_buf1[512] = "", tmp_buf2[512] = "";

	      fgets (tmp_buf1, 512, fd);

	      fclose (fd);

	      if ((tmp_buf1[0] == '#') && (tmp_buf1[1] == '!'))
		{
		  char *interpretor, *ptr = &tmp_buf1[2];

		  if (interpretor = strsep (&ptr, " \t\n"))
		    {
		      adjustpath (tmp_buf1, tmp_buf2, 512);

		      printf (tmp_buf1, "%s %s", tmp_buf2, arg_str); 

		      retval = 1;
		    }
		}
	    }
	}
      free (copy_of_arg_str);
    }
  return (retval);
}
