/*
 *  Copyright (C) 2005, 2006 Mikhail A. Kryshen
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id: IO.java,v 1.4 2006/12/14 15:59:49 mikhail Exp $
 */

package kryshen.tema.functions;

import java.io.*;
import java.util.*;

import kryshen.tema.*;

/**
 * I/O functions.
 */
public class IO {
    public static final Function COPY =
        new Function() {
           public int invoke(FunctionDataParser fdp, Writer out)
                throws IOException, TemplateException {

                String arg0 = fdp.getNextArg();
                String arg1 = fdp.getNextArg();

                File source = new File
                    (Tema.getProperty("resource_base"), arg0);
                File dest = new File(arg1);
                
                try {
                    copyFile(source, dest);
                } catch (IOException e) {
                    System.err.println(e);
                    return 0;
                }
                out.write(arg1);
                return 1;
            }
        };

    public static final Function WRITE =
        new Function() {
           public int invoke(FunctionDataParser fdp, Writer out)
                throws IOException, TemplateException {

                String arg0 = fdp.getNextArg();

		System.err.println("Writing " + arg0 + "...");

                Writer fw;

                try {
		    fw = Tema.createFileWriter(arg0);
                } catch (IOException e) {
                    System.err.println(e);
                    //throw new TemplateException(e.getMessage(), e, in);
                    return 0;
                }

                try {
		    fdp.parseData(fw);
                } finally {
                    fw.close();
                }

                out.write(arg0);

		System.err.println("Saved " + arg0 + ".");
                return 1;
            }
        };

    public static final Function READ =
        new Function() {
           public int invoke(FunctionDataParser fdp, Writer out)
                throws IOException, TemplateException {

	       String filename = fdp.getData();
	       String file;
	       
                try {
                    file = Tema.readFile(new File(filename));
                } catch (IOException e) {
                    System.err.println(e);
                    //throw new TemplateException(e.getMessage(), e, in);
                    return 0;
                }
		
                out.write(file);
                return 1;
            }
        };

    public static final Function INCLUDE =
        new Function() {
           public int invoke(FunctionDataParser fdp, Writer out)
                throws IOException, TemplateException {
                
                String filename = fdp.getData();
                
                TemplateReader fin;

                try {
                    fin = Tema.createTemplateReader(new File(filename));
                } catch (IOException e) {
                    throw new TemplateException(e.getMessage(), e,
                                                fdp.getTemplateReader());
                }

                int r = fdp.getTemplateParser().parse(fin, out);

                fin.close();
                return r;
            }
        };

    /**
     * Update file (copy if newer).
     *
     * @param src source file.
     * @param dest destination file.
     *
     * @trows IOException on copying error.
     */
    private static void copyFile(File src, File dest) throws IOException {
	System.err.print("Copying " + src + "... ");
	
	if (src.lastModified() < dest.lastModified()) {
	    System.err.println(dest + " is up to date.");
	    return;
	}

	File parent = dest.getParentFile();
	if (parent != null) parent.mkdirs();

        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);
        
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();

	System.err.println("saved " + dest + ".");
    }
}