<?xml version="1.0"?>
<antlib>
    <scriptdef name="generate" language="javascript">
        <element name="fileset" type="fileset"/>
        <attribute name="destDir"/>
        <attribute name="checkUpToDate"/>
        <attribute name="printerFriendly"/>
        <![CDATA[
            importClass(java.io.File);

            fs = elements.get("fileset").get(0);
            srcDir = elements.get("fileset").get(0).getDir(project);

            // Get the files (array) of that fileset
            ds = fs.getDirectoryScanner(project);
            srcFiles = ds.getIncludedFiles();

            // iterate over that array
            print('processing '+srcFiles.length+' source files...');
            for (i=0; i<srcFiles.length; i++) {

                // get the values via Java API
                var file = new File(fs.getDir(project), srcFiles[i]);

                var basedir = file.getParent();
                var filename = file.getName();
                var filepath = srcFiles[i].substring(0, srcFiles[i].lastIndexOf(project.getProperty("file.separator"))+1);

                if (attributes.get("checkuptodate") == "true") {
                    p = "xooki."+file.getAbsolutePath().replace(' ','_')+".uptodate";
                    upToDate = project.createTask("uptodate");
                    upToDate.setProperty(p);
                    upToDate.setSrcfile(file);
                    upToDate.setTargetFile(new File(attributes.get("destdir")+"/"+filepath+"/"+filename));
                    upToDate.perform();
                    if (project.getProperty(p) != null) {
                        self.log(srcFiles[i]+" is up to date", 3);
                        continue;
                    }
                }

                exec = project.createTask("exec");
                exec.setDir(new File(basedir));
                exec.setExecutable("jrunscript");
                exec.setTaskName("generate");
                exec.createArg().setValue(srcDir+"/xooki/xooki.js");
                exec.createArg().setValue(filename);
                exec.createArg().setValue(attributes.get("destdir")+"/"+filepath);
                if (attributes.get("printerfriendly") == "true") {
                    exec.createArg().setValue("print");
                }
                exec.perform();
            }
        ]]>
    </scriptdef>
    <scriptdef name="print" language="javascript">
        <attribute name="src"/>
        <attribute name="dest"/>
        <attribute name="checkUpToDate"/>
        <![CDATA[
            importClass(java.io.File);

          var file = new File(attributes.get("src"));
          var destFile = new File(attributes.get("dest"));

      var basedir = file.getParent();
      var filename = file.getName();

      var perform = true;

            if (attributes.get("checkuptodate") == "true") {
                p = "xooki."+file.getAbsolutePath().replace(' ','_')+".uptodate";
                upToDate = project.createTask("uptodate");
                upToDate.setProperty(p);
                upToDate.setSrcfile(file);
                upToDate.setTargetFile(destFile);
                upToDate.perform();
                if (project.getProperty(p) != null) {
                    self.log(file+" is up to date", 3);
                    perform = false;
                }
            }

            if (perform) {
            exec = project.createTask("exec");
            exec.setDir(new File(basedir));
            exec.setExecutable("jrunscript");
            exec.setTaskName("print");
            exec.createArg().setValue(project.getProperty("basedir")+"/xooki/xooki.js");
            exec.createArg().setValue(filename);
            exec.createArg().setValue(destFile);
            exec.createArg().setValue("print");
            exec.perform();
          }
        ]]>
    </scriptdef>
</antlib>