/**
 *  Copyright 2011 Ryszard Wiśniewski <brut.alll@gmail.com>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package brut.androlib;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Logger;

/**
 * @author Ryszard Wiśniewski <brut.alll@gmail.com>
 */
public class ApktoolProperties {
	public static String get(String key) {
		return get().getProperty(key);
	}

	public static Properties get() {
		if (sProps == null) {
			loadProps();
		}
		return sProps;
	}

	private static void loadProps() {
		InputStream in = ApktoolProperties.class
				.getResourceAsStream("/properties/apktool.properties");
		sProps = new Properties();
		try {
			sProps.load(in);
			in.close();
		} catch (IOException ex) {
			LOGGER.warning("Can't load properties.");
		}

		InputStream templateStream = ApktoolProperties.class
				.getResourceAsStream("/properties/baksmali.properties");
		Properties properties = new Properties();
		String version = "(unknown)";
		try {
			properties.load(templateStream);
			version = properties.getProperty("application.version");
		} catch (IOException ex) {
		}
		sProps.put("baksmaliVersion", version);
		templateStream = ApktoolProperties.class
				.getResourceAsStream("/properties/smali.properties");
		properties = new Properties();
		version = "(unknown)";
		try {
			properties.load(templateStream);
			version = properties.getProperty("application.version");
		} catch (IOException ex) {
		}
		sProps.put("smaliVersion", version);
	}

	private static Properties sProps;

	private static final Logger LOGGER = Logger
			.getLogger(ApktoolProperties.class.getName());
}
@@ -654,16 +654,76 @@
 rolib;%0A%0A
+import org.jf.baksmali.baksmali;%0Aimport org.jf.smali.main;%0A%0A
 import j
@@ -1171,37 +1171,32 @@
 Properties.class
-%0A%09%09%09%09
 .getResourceAsSt
@@ -1421,36 +1421,39 @@
 m = 
-ApktoolProperties.class%0A%09%09%09%09
+baksmali.class.getClassLoader()
 .get
@@ -1470,28 +1470,16 @@
 Stream(%22
-/properties/
 baksmali
@@ -1565,35 +1565,97 @@
 (unknown)%22;%0A
-%09%09try %7B%0A%09%09%09
+%0A        if (templateStream != null) %7B%0A            try %7B%0A                
 properties.l
@@ -1667,35 +1667,48 @@
 emplateStream);%0A
-%09%09%09
+                
 version = proper
@@ -1740,34 +1740,44 @@
 tion.version%22);%0A
-%09%09
+            
 %7D catch (IOExcep
@@ -1777,32 +1777,57 @@
 IOException 
-ex) %7B%0A%09%09
+ignored) %7B%0A            %7D%0A        
 %7D%0A%09%09sProps.p
@@ -1858,16 +1858,18 @@
 rsion);%0A
+%0A%0A
 %09%09templa
@@ -1883,36 +1883,35 @@
 m = 
-ApktoolProperties.class%0A%09%09%09%09
+main.class.getClassLoader()
 .get
@@ -1928,28 +1928,16 @@
 Stream(%22
-/properties/
 smali.pr
@@ -2006,27 +2006,89 @@
 nown)%22;%0A
-%09%09try %7B%0A%09%09%09
+%0A        if (templateStream != null) %7B%0A            try %7B%0A                
 properti
@@ -2112,19 +2112,32 @@
 tream);%0A
-%09%09%09
+                
 version 
@@ -2185,18 +2185,28 @@
 sion%22);%0A
-%09%09
+            
 %7D catch 
@@ -2218,24 +2218,49 @@
 ception 
-ex) %7B%0A%09%09
+ignored) %7B%0A            %7D%0A        
 %7D%0A%09%09sPro

fix prop location of smali/baksmali, add more checks