package kalang.script;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Random;
import javax.annotation.Nullable;
import kalang.lang.Script;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author Kason Yang
 */
public abstract class ShellScript extends Script {

    public final static String DEFAULT_ENCODING = "utf8";

    protected String readText(String file, String encoding) throws IOException {
        return FileUtils.readFileToString(new File(file), encoding);
    }

    protected String readText(String file) throws IOException {
        return readText(file, DEFAULT_ENCODING);
    }

    protected void writeText(String file, String data, String encoding) throws IOException {
        FileUtils.writeStringToFile(new File(file), data, encoding);
    }

    protected void writeText(String file, String data) throws IOException {
        writeText(file, data, DEFAULT_ENCODING);
    }

    protected int randomInt(int min, int max) {
        //TODO here never return max
        Random random = new Random();
        return random.nextInt(max - min) + min;
    }
    
    protected int randomInt() {
        return new Random().nextInt();
    }
    
    protected void deleteDirectory(String file) throws IOException {
        FileUtils.deleteDirectory(new File(file));
    }
    
    protected boolean exists(String file) {
        return new File(file).exists();
    }
    
    protected String baseName(String filename) {
        return FilenameUtils.getBaseName(filename);
    }
    
    protected String extension(String filename){
        return FilenameUtils.getExtension(filename);
    }
    
    protected int exec(String[] arguments) throws IOException{
        return exec(arguments,null);
    }

    protected int exec(String[] arguments,@Nullable String workingDirectory) throws IOException {
        ProcessBuilder pb = new ProcessBuilder(arguments);
        pb.inheritIO();
        if (workingDirectory!=null && !workingDirectory.isEmpty()) {
            pb.directory(new File(workingDirectory));
        }
        Process p = pb.start();
        try {
            p.waitFor();
        } catch (InterruptedException ex) {

        }
        return p.exitValue();
    }

    protected int exec(String command) throws IOException {
        String[] arguments = command.split(" ");//TODO fix quotes
        return exec(arguments);
    }
    
    protected Properties loadProperties(String file) throws FileNotFoundException, IOException {
        Properties props = new Properties();
        props.load(new FileInputStream(file));
        return props;
    }
    
    protected String now(){
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

}

@@ -1503,32 +1503,153 @@
 e));%0A    %7D%0A    %0A
+    protected void deleteFile(String file) throws IOException%7B%0A        FileUtils.forceDelete(new File(file));%0A    %7D%0A    %0A
     protected bo
@@ -1944,32 +1944,945 @@
 me);%0A    %7D%0A    %0A
+    protected void copyFile(String src,String dest) throws IOException%7B%0A        FileUtils.copyFile(new File(src), new File(dest));%0A    %7D%0A    %0A    protected void copyDirectory(String src,String dest) throws IOException %7B%0A        FileUtils.copyDirectory(new File(src), new File(dest));%0A    %7D%0A    %0A    protected void moveFile(String src,String dest) throws IOException%7B%0A        FileUtils.moveFile(new File(src), new File(dest));%0A    %7D%0A    %0A    protected void moveDirectory(String src,String desc) throws IOException%7B%0A        FileUtils.moveDirectory(new File(src), new File(desc));%0A    %7D%0A    %0A    protected String path(String%5B%5D parts)%7B%0A        if (parts.length==0)%7B%0A            throw new IllegalArgumentException(%22empty array%22);%0A        %7D%0A        File file = new File(parts%5B0%5D);%0A        for(int i=1;i%3Cparts.length;i++) %7B%0A            file = new File(file,parts%5Bi%5D);%0A        %7D%0A        return file.getPath();%0A    %7D%0A    %0A
     protected in

add methods: copy*,move*,path