/*
 * Copyright 2014-present Facebook, Inc.
 *
 * 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 com.facebook.buck.android.support.exopackage;

import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Build;
import android.util.Log;

/**
 * Loads native library files installed by the exopackage installer. This class requires
 * initialization before use; {@link ExopackageApplication#attachBaseContext} is responsible
 * for calling {@link #init} so that application-specific code doesn't need to.
 */
public class ExopackageSoLoader {

  private static final String TAG = "ExopackageSoLoader";

  private static boolean initialized = false;

  private static Map<String, String> cpuAbi1Libraries = new HashMap<>();
  private static Map<String, String> cpuAbi2Libraries = new HashMap<>();

  private ExopackageSoLoader() {}

  public static void init(Context context) {
    if (initialized) {
      return;
    }
    try {
      collectLibraries(context, Build.CPU_ABI, cpuAbi1Libraries);
      collectLibraries(context, Build.CPU_ABI2, cpuAbi2Libraries);
      initialized = true;
    } catch (IOException e) {
      cpuAbi1Libraries.clear();
      cpuAbi2Libraries.clear();
      return;
    }
  }

  public static void collectLibraries(Context context, String abi, Map<String, String> libraries)
      throws IOException {
    if (abi == null || abi.isEmpty()) {
      return;
    }

    String abiDirectory = "/data/local/tmp/exopackage/" + context.getPackageName() +
        "/native-libs/" + abi;

    File metadataFile = new File(abiDirectory + "/metadata.txt");
    if (!metadataFile.exists()) {
      return;
    }

    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(metadataFile.getAbsolutePath()));
      String line = null;
      while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (line.isEmpty()) {
          continue;
        }
        int index = line.indexOf(' ');
        if (index == -1) {
          Log.w(TAG, "Skipping corrupt metadata line: \n" + line + "\n");
          continue;
        }

        libraries.put(line.substring(0, index), abiDirectory + "/" + line.substring(index + 1));
      }
    } finally {
      if (reader != null) {
        reader.close();
      }
    }
  }

  public static void loadLibrary(String library) {
    String path = cpuAbi1Libraries.get(library);
    if (path == null) {
      path = cpuAbi2Libraries.get(library);
    }
    if (path == null) {
      return;
    }

    System.load(path);
  }
}

@@ -1551,24 +1551,86 @@
 tialized) %7B%0A
+      Log.d(TAG, %22init() already called, so nothing to do.%22);%0A
       return
@@ -1814,33 +1814,135 @@
     
-%7D catch (IOException e) %7B
+  Log.i(TAG, %22Finished initializing.%22);%0A    %7D catch (IOException e) %7B%0A      Log.e(TAG, %22There was an error initializing: %22, e);
 %0A   
@@ -2440,66 +2440,229 @@
     
-return;%0A    %7D%0A%0A    BufferedReader reader = null;%0A    try %7B
+Log.d(TAG, %22Could not find metadata file: %22 + metadataFile.getAbsolutePath());%0A      return;%0A    %7D%0A%0A    BufferedReader reader = null;%0A    try %7B%0A      Log.d(TAG, %22Reading metadata file: %22 + metadataFile.getAbsolutePath());
 %0A   
@@ -3073,24 +3073,72 @@
 %0A        %7D%0A%0A
+        Log.v(TAG, %22Processing line: %22 + line);%0A
         libr
@@ -3525,39 +3525,205 @@
     
-return;%0A    %7D%0A%0A    System.load(
+Log.e(TAG, %22Could not load library %22 + library);%0A      return;%0A    %7D%0A%0A    Log.d(TAG, %22Attempting to load library: %22 + path);%0A    System.load(path);%0A    Log.d(TAG, %22Successfully loaded library: %22 + 
 path

Add logging to ExopackageSoLoader.