package org.kivy.android;

import java.io.File;

import android.util.Log;


public class PythonUtil {
	private static final String TAG = "PythonUtil";

	protected static String[] getLibraries() {
        return new String[] {
            "SDL2",
            "SDL2_image",
            "SDL2_mixer",
            "SDL2_ttf",
            "main"
        };
    }

	public static void loadLibraries(File filesDir) {

        String filesDirPath = filesDir.getAbsolutePath();

		for (String lib : getLibraries()) {
            System.loadLibrary(lib);
        }

        try {
            System.loadLibrary("python2.7");
        } catch(UnsatisfiedLinkError e) {
            Log.v(TAG, "Failed to load libpython2.7");
        }
        
        try {
            System.loadLibrary("python3.5m");
        } catch(UnsatisfiedLinkError e) {
            Log.v(TAG, "Failed to load libpython3.5m");
        }
        
        try {
            System.load(filesDirPath + "/lib/python2.7/lib-dynload/_io.so");
            System.load(filesDirPath + "/lib/python2.7/lib-dynload/unicodedata.so");
        } catch(UnsatisfiedLinkError e) {
            Log.v(TAG, "Failed to load _io.so or unicodedata.so...but that's okay.");
        }
        
        try {
            // System.loadLibrary("ctypes");
            System.load(filesDirPath + "/lib/python2.7/lib-dynload/_ctypes.so");
        } catch(UnsatisfiedLinkError e) {
            Log.v(TAG, "Unsatisfied linker when loading ctypes");
        }

        Log.v(TAG, "Loaded everything!");
	}
}

@@ -315,16 +315,67 @@
 2_ttf%22,%0A
+            %22python2.7%22,%0A            %22python3.5m%22,%0A
         
@@ -517,109 +517,102 @@
 ();%0A
-%0A%09%09for (String lib : getLibraries()) %7B%0A            System.loadLibrary(lib);%0A        %7D%0A%0A        try %7B%0A
+        boolean skippedPython = false;%0A%0A%09%09for (String lib : getLibraries()) %7B%0A%09%09    try %7B%0A    
     
@@ -642,22 +642,18 @@
 ary(
-%22python2.7%22);%0A
+lib);%0A    
     
@@ -706,69 +706,75 @@
     
-Log.v(TAG, %22Failed to load libpython2.7%22);%0A  
+    if (lib.startsWith(%22python%22) && !skippedPython) %7B%0A
      
- %7D%0A
         
 %0A   
@@ -773,166 +773,123 @@
     
-%0A
        
- try %7B%0A            System.loadLibrary(%22python3.5m%22);%0A        %7D catch(UnsatisfiedLinkError e) %7B%0A            Log.v(TAG, %22Failed to load libpython3.5m%22);
+skippedPython = true;%0A                    continue;%0A                %7D%0A                throw e;%0A            %7D
 %0A   
@@ -891,32 +891,24 @@
 %7D%0A        %7D%0A
-        
 %0A        try

load python lib before main