package org.griphyn.vdl.karajan.lib;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.globus.cog.karajan.arguments.Arg;
import org.globus.cog.karajan.stack.VariableStack;
import org.globus.cog.karajan.util.TypeUtil;
import org.globus.cog.karajan.workflow.ExecutionException;
import org.globus.cog.karajan.workflow.nodes.functions.FunctionsCollection;
import org.griphyn.vdl.mapping.AbsFile;

public class PathUtils extends FunctionsCollection {

	public static final Arg PATH = new Arg.Positional("path");

	static {
		setArguments("vdl_dirname", new Arg[] { PATH });
	}

	public String vdl_dirname(VariableStack stack) throws ExecutionException {
		String path = TypeUtil.toString(PATH.getValue(stack));
		return new AbsFile(path).getDir();
	}
    
    static {
        setArguments("vdl_reldirname", new Arg[] { PATH });
    }

    public String vdl_reldirname(VariableStack stack) throws ExecutionException {
        String path = TypeUtil.toString(PATH.getValue(stack));
        String dir = new AbsFile(path).getDir();
        if (dir.startsWith("/")) {
            return dir.substring(1);
        }
        else {
            return dir;
        }
    }
    
    static {
        setArguments("vdl_basename", new Arg[] { PATH });
    }

    public String vdl_basename(VariableStack stack) throws ExecutionException {
    	String path = TypeUtil.toString(PATH.getValue(stack));
        return new AbsFile(path).getName();
    }
    
    static {
        setArguments("vdl_provider", new Arg[] { PATH });
    }

    public String vdl_provider(VariableStack stack) throws ExecutionException {
    	String path = TypeUtil.toString(PATH.getValue(stack));
        return new AbsFile(path).getProtocol();
    }
    
    static {
        setArguments("vdl_hostname", new Arg[] { PATH });
    }

    public String vdl_hostname(VariableStack stack) throws ExecutionException {
    	String path = TypeUtil.toString(PATH.getValue(stack));
        return new AbsFile(path).getHost();
    }
    
    public static final Arg DIR = new Arg.Positional("dir");
    static {
        setArguments("vdl_dircat", new Arg[] { DIR, PATH });
    }

    public String vdl_dircat(VariableStack stack) throws ExecutionException {
        String dir = TypeUtil.toString(DIR.getValue(stack));
    	String path = TypeUtil.toString(PATH.getValue(stack));
        if (dir.equals("")) {
            return path;
        }
        else if (dir.endsWith("/")) {
            return dir + path;
        }
        else {
            return dir + '/' + path;
        }
    }
    
    public static final Arg FILES = new Arg.Positional("files");
    static {
        setArguments("vdl_pathnames", new Arg[] { FILES });
    }

    public Object[] vdl_pathnames(VariableStack stack) throws ExecutionException {
        List l = new ArrayList();
        Iterator i = TypeUtil.toIterator(FILES.getValue(stack));
        while (i.hasNext()) {
        	l.add(new AbsFile((String) i.next()).getPath());
        }
        return l.toArray(new String[0]);
    }
}

@@ -382,24 +382,62 @@
 Collection;%0A
+import org.griphyn.common.classes.Os;%0A
 import org.g
@@ -2119,24 +2119,86 @@
 nal(%22dir%22);%0A
+    public static final Arg OS = new Arg.Optional(%22os%22);%0A    %0A
     static %7B
@@ -2250,24 +2250,28 @@
  %7B DIR, PATH
+, OS
  %7D);%0A    %7D%0A%0A
@@ -2461,32 +2461,179 @@
 tValue(stack));%0A
+    %09boolean windows = false;%0A    %09if (OS.isPresent(stack)) %7B%0A    %09%09Os os = (Os) OS.getValue(stack);%0A    %09%09windows = Os.WINDOWS.equals(os);%0A    %09%7D%0A
         if (dir.
@@ -2665,20 +2665,41 @@
  return 
-path
+windowsify(path, windows)
 ;%0A      
@@ -2752,29 +2752,47 @@
     
-    
+%09
 return 
-dir + path
+windowsify(dir + path, windows)
 ;%0A  
@@ -2833,16 +2833,27 @@
  return 
+windowsify(
 dir + '/
@@ -2860,16 +2860,26 @@
 ' + path
+, windows)
 ;%0A      
@@ -2889,32 +2889,180 @@
 %0A    %7D%0A    %0A    
+private String windowsify(String path, boolean windows) %7B%0A%09%09if (windows) %7B%0A%09%09%09return path.replace('/', '%5C%5C');%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09return path;%0A%09%09%7D%0A%09%7D%0A%0A%09
 public static fi

use backslash if remote site is windows