// Path: halvade/src/be/ugent/intec/halvade/hadoop/mapreduce/HalvadeCounters.java
// public enum HalvadeCounters {
//     TIME_BWA_ALN,
//     TIME_BWA_MEM,
//     TIME_BOWTIE2,
//     TIME_CUSHAW2,
//     TIME_BWA_SAMPE,
//     TIME_STAR,
//     TIME_STAR_REF,
//     TIME_STAR_BUILD,
//     TIME_ELPREP,
//     TIME_BEDTOOLS,
//     TIME_SAMTOBAM,
//     TIME_SAMPLESAM,
//     TIME_HADOOP_SAMTOBAM,
//     TIME_PICARD_CLEANSAM,
//     TIME_PICARD_MARKDUP,
//     TIME_PICARD_ADDGRP,
//     TIME_FEATURECOUNTS,
//     TIME_PICARD_BAI,    
//     TIME_GATK_RECAL,  
//     TIME_GATK_TARGET_CREATOR, 
//     TIME_GATK_INDEL_REALN, 
//     TIME_GATK_PRINT_READS, 
//     TIME_GATK_COMBINE_VCF,
//     TIME_GATK_VARIANT_CALLER,
//     
//     IN_BWA_READS,
//     IN_PREP_READS,
//     
//     OUT_BWA_READS,
//     OUT_VCF_FILES,
//     OUT_UNMAPPED_READS,
//     OUT_DIFF_CHR_READS,
//     OUT_OVERLAPPING_READS,
//     
//     FOUT_BWA_TMP,
//     FOUT_STAR_TMP,
//     FOUT_GATK_TMP,
//     FOUT_GATK_VCF,
//     FOUT_TO_HDFS,
//     
//     FIN_FROM_HDFS,
//     
//     TOOLS_GATK,
//     
//     STILL_RUNNING_HEARTBEAT
// }


import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.TaskInputOutputContext;
import be.ugent.intec.halvade.hadoop.mapreduce.HalvadeCounters;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.zip.GZIPInputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.ChecksumException;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
    }
    
    public static boolean removeLocalFile(String filename, TaskInputOutputContext context, HalvadeCounters counter) {
        return removeLocalFile(false, filename, context, counter);
    }
    
    public static boolean removeLocalFile(boolean keep, String filename, TaskInputOutputContext context, HalvadeCounters counter) {
        if(keep) return false;
        File f = new File(filename);
        if(f.exists()) context.getCounter(counter).increment(f.length());
        long size = f.length();
        boolean res = f.exists() && f.delete();
        Logger.DEBUG((res ? "successfully deleted ": "failed to delete ") + "\"" + filename + "\" [" + (size / 1024 / 1024)+ "Mb]");
        return res;
    }
    
    public static boolean removeLocalDir(String filename) {
        return removeLocalDir(false, filename);
    }
    public static boolean removeLocalDir(boolean keep, String filename) {
        if(keep) return false;
        File f = new File(filename);
//        long size = getFolderSize(f);
        boolean res = f.exists() && deleteDir(f);
        Logger.DEBUG((res ? "successfully deleted ": "failed to delete ") + "\"" + filename + "\""); // [" + (size / 1024 / 1024)+ "Mb]");
        return res; 
    }
    public static  boolean removeLocalDir(String filename, TaskInputOutputContext context, HalvadeCounters counter) {
        return removeLocalDir(false, filename, context, counter);
    } 
// Path: halvade/src/be/ugent/intec/halvade/hadoop/mapreduce/HalvadeCounters.java
// public enum HalvadeCounters {
//     TIME_BWA_ALN,
//     TIME_BWA_MEM,
//     TIME_BOWTIE2,
//     TIME_CUSHAW2,
//     TIME_BWA_SAMPE,
//     TIME_STAR,
//     TIME_STAR_REF,
//     TIME_STAR_BUILD,
//     TIME_ELPREP,
//     TIME_BEDTOOLS,
//     TIME_SAMTOBAM,
//     TIME_SAMPLESAM,
//     TIME_HADOOP_SAMTOBAM,
//     TIME_PICARD_CLEANSAM,
//     TIME_PICARD_MARKDUP,
//     TIME_PICARD_ADDGRP,
//     TIME_FEATURECOUNTS,
//     TIME_PICARD_BAI,    
//     TIME_GATK_RECAL,  
//     TIME_GATK_TARGET_CREATOR, 
//     TIME_GATK_INDEL_REALN, 
//     TIME_GATK_PRINT_READS, 
//     TIME_GATK_COMBINE_VCF,
//     TIME_GATK_VARIANT_CALLER,
//     
//     IN_BWA_READS,
//     IN_PREP_READS,
//     
//     OUT_BWA_READS,
//     OUT_VCF_FILES,
//     OUT_UNMAPPED_READS,
//     OUT_DIFF_CHR_READS,
//     OUT_OVERLAPPING_READS,
//     
//     FOUT_BWA_TMP,
//     FOUT_STAR_TMP,
//     FOUT_GATK_TMP,
//     FOUT_GATK_VCF,
//     FOUT_TO_HDFS,
//     
//     FIN_FROM_HDFS,
//     
//     TOOLS_GATK,
//     
//     STILL_RUNNING_HEARTBEAT
// }


// Path: halvade/src/be/ugent/intec/halvade/utils/HalvadeFileUtils.java
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.TaskInputOutputContext;
import be.ugent.intec.halvade.hadoop.mapreduce.HalvadeCounters;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.zip.GZIPInputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.ChecksumException;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;

    }
    
    public static boolean removeLocalFile(String filename, TaskInputOutputContext context, HalvadeCounters counter) {
        return removeLocalFile(false, filename, context, counter);
    }
    
    public static boolean removeLocalFile(boolean keep, String filename, TaskInputOutputContext context, HalvadeCounters counter) {
        if(keep) return false;
        File f = new File(filename);
        if(f.exists()) context.getCounter(counter).increment(f.length());
        long size = f.length();
        boolean res = f.exists() && f.delete();
        Logger.DEBUG((res ? "successfully deleted ": "failed to delete ") + "\"" + filename + "\" [" + (size / 1024 / 1024)+ "Mb]");
        return res;
    }
    
    public static boolean removeLocalDir(String filename) {
        return removeLocalDir(false, filename);
    }
    public static boolean removeLocalDir(boolean keep, String filename) {
        if(keep) return false;
        File f = new File(filename);
//        long size = getFolderSize(f);
        boolean res = f.exists() && deleteDir(f);
        Logger.DEBUG((res ? "successfully deleted ": "failed to delete ") + "\"" + filename + "\""); // [" + (size / 1024 / 1024)+ "Mb]");
        return res; 
    }
    public static  boolean removeLocalDir(String filename, TaskInputOutputContext context, HalvadeCounters counter) {
        return removeLocalDir(false, filename, context, counter);
    } 
    public static  boolean removeLocalDir(boolean keep, String filename, TaskInputOutputContext context, HalvadeCounters counter) {