/* 
 * Copyright (c) 2015 Sebastian Brudzinski
 * 
 * See the file LICENSE for copying permission.
 */
package latexstudio.editor.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.openide.util.Exceptions;

/**
 * This is a helper class, that resolves the location of certain files, used
 * by the application.
 * 
 * @author Sebastian
 */
public final class ApplicationUtils {
    
    private static final String HOME = System.getProperty("user.home");
    private static final String OS = System.getProperty("os.name");
    private static final String APP_DIR_NAME = ".OpenLaTeXStudio";
    private static final String APP_VERSION_FILENAME = "openlatexstudio/version.txt";
    private static final String PREVIEW_SOURCE_FILENAME = "preview.tex";
    private static final String PREVIEW_PDF_FILENAME = "preview.pdf";
    private static final String SETTINGS_FILENAME = "settings.properties";
    
    public static final String PDFLATEX = "pdflatex";
    
    public static final String TEMPLATES_DIR = "/openlatexstudio/templates/";
    public static final String TEMPLATES_FILE = "/openlatexstudio/templates.xml";

    private ApplicationUtils() {
    }
    
    public static String getPathToTEX(String texDirectory) {
        if (texDirectory == null || texDirectory.equals("")) {
            return PDFLATEX;
        } else {
            if (texDirectory.endsWith(File.separator)) {
                return texDirectory.concat(PDFLATEX);
            } else {
                return texDirectory.concat(File.separator).concat(PDFLATEX);
            }
        }
    }
    
    public static String getTempSourceFile() {
        return getAppDirectory().concat(File.separator).concat(PREVIEW_SOURCE_FILENAME);
    }
    
    public static String getTempPDFFile() {
        return getAppDirectory().concat(File.separator).concat(PREVIEW_PDF_FILENAME);
    }
    
    public static String getSettingsFile() {
        String settingsFileName = getAppDirectory().concat(File.separator).concat(SETTINGS_FILENAME);
        File file = new File(settingsFileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        
        return settingsFileName;
    }
    
    public static String getAppDirectory() {
        File tempDir = new File(HOME + File.separator + APP_DIR_NAME);
        if (!tempDir.exists()) {
            tempDir.mkdir();
        }
        return tempDir.getAbsolutePath();
    }
    
    public static String getAppVersion() {
        URL url = ApplicationUtils.class.getClassLoader().getResource(APP_VERSION_FILENAME);
        String version = "UNKNOWN";
        InputStream is = null;
        try {
            is = url.openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = br.readLine()) != null) {
                // ?!? Allow the use of comments and optionally supplementary info in version.txt           
                if (!line.startsWith("#")) {                    
                    String[] pairs = Pattern.compile("=").split(line);             
                    if (pairs[0].equals("version")) {                        
                        version = pairs[1];
                    }
                }
            }
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } finally {
            IOUtils.closeQuietly(is);
        }
        return version;
    }
    
    public static void deleteTempFiles() {
        File source = new File(getTempSourceFile());
        File tmpPdf = new File(getTempPDFFile());
        if (source.exists()) {
            source.delete();
        }
        if (tmpPdf.exists()) {
            tmpPdf.delete();
        }
    }
    
    public static boolean isWindows() {
        return OS.startsWith("Windows");
    }
}

@@ -787,94 +787,8 @@
 o%22;%0A
-    private static final String APP_VERSION_FILENAME = %22openlatexstudio/version.txt%22;%0A
     
@@ -852,24 +852,24 @@
 eview.tex%22;%0A
+
     private 
@@ -1001,24 +1001,242 @@
 rties%22;%0A    
+private static final String APP_PROPERTIES = %22application.properties%22;%0A    private static final String APP_VERSION = PropertyService.%0A            readProperties(APP_PROPERTIES).getProperty(%22application.version%22);%0A     
 %0A    public 
@@ -2931,969 +2931,26 @@
     
-URL url = ApplicationUtils.class.getClassLoader().getResource(APP_VERSION_FILENAME);%0A        String version = %22UNKNOWN%22;%0A        InputStream is = null;%0A        try %7B%0A            is = url.openStream();%0A            BufferedReader br = new BufferedReader(new InputStreamReader(is));%0A            String line;%0A            while ((line = br.readLine()) != null) %7B%0A                // ?!? Allow the use of comments and optionally supplementary info in version.txt           %0A                if (!line.startsWith(%22#%22)) %7B                    %0A                    String%5B%5D pairs = Pattern.compile(%22=%22).split(line);             %0A                    if (pairs%5B0%5D.equals(%22version%22)) %7B                        %0A                        version = pairs%5B1%5D;%0A                    %7D%0A                %7D%0A            %7D%0A        %7D catch (IOException ex) %7B%0A            Exceptions.printStackTrace(ex);%0A        %7D finally %7B%0A            IOUtils.closeQuietly(is);%0A        %7D%0A        return version
+return APP_VERSION
 ;%0A  

Simplify version read as suggested by @sebbrudzinski