package alexander.martinz.libs.execution.binaries;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;

import alexander.martinz.libs.execution.RootShell;
import alexander.martinz.libs.execution.ShellHelper;
import alexander.martinz.libs.execution.ShellLogger;
import alexander.martinz.libs.execution.ShellManager;

public class BusyBox {
    private static final String TAG = BusyBox.class.getSimpleName();

    private static final String[] PATH_BUSYBOX = new String[]{
            "/system/bin/busybox", "/system/xbin/busybox"
    };

    private static Boolean sHasBusybox= null;

    public static boolean isAvailable() {
        return isAvailable(false);
    }

    public static boolean isAvailable(boolean forceCheck) {
        if (!forceCheck && sHasBusybox != null) {
            return sHasBusybox;
        }

        final String busyboxPath = getBusyboxPath();
        if (!TextUtils.isEmpty(busyboxPath)) {
            if (ShellLogger.DEBUG) {
                Log.d(TAG, String.format("Found busybox path: %s", busyboxPath));
            }
            sHasBusybox = true;
            return true;
        }
        if (ShellLogger.DEBUG) {
            Log.d(TAG, "no busybox binary found, trying with hit and miss");
        }

        final String busyboxLocation = ShellHelper.findBinary("busybox");
        if (!TextUtils.isEmpty(busyboxLocation)) {
            if (ShellLogger.DEBUG) {
                Log.d(TAG, String.format("Found busybox path: %s", busyboxLocation));
            }
            sHasBusybox = true;
            return true;
        }
        return sHasBusybox;
    }

    /**
     * @return The path of the busybox binary or null if none found
     */
    @Nullable public static String getBusyboxPath() {
        for (final String path : PATH_BUSYBOX) {
            if (new File(path).exists()) {
                return path;
            }
        }
        return null;
    }

    @Nullable public static String callBusyBoxApplet(@NonNull Context context, @NonNull String applet, @Nullable String args) {
        final File fileDir = context.getFilesDir();
        if (!fileDir.exists()) {
            if (ShellLogger.DEBUG) {
                Log.e(TAG, "Files folder does not exist!");
            }
            return null;
        }

        final File busybox = new File(fileDir, "busybox");
        if (!busybox.exists()) {
            Log.e(TAG, "busybox binary does not exist!");
            return null;
        }

        String cmd = String.format("%s %s", busybox.getAbsolutePath(), applet);
        if (TextUtils.isEmpty(args)) {
            Log.e(TAG, String.format("No args specified, returning -> %s", cmd));
            return cmd;
        }

        cmd = String.format("%s %s", cmd, args);
        if (ShellLogger.DEBUG) {
            Log.v(TAG, String.format("Calling applet with args -> %s", cmd));
        }
        return cmd;
    }
}

@@ -77,16 +77,71 @@
 ontext;%0A
+import android.content.Intent;%0Aimport android.net.Uri;%0A
 import a
@@ -171,24 +171,24 @@
 on.NonNull;%0A
-
 import andro
@@ -302,59 +302,8 @@
 e;%0A%0A
-import alexander.martinz.libs.execution.RootShell;%0A
 impo
@@ -407,62 +407,8 @@
 ger;
-%0Aimport alexander.martinz.libs.execution.ShellManager;
 %0A%0Apu
@@ -498,16 +498,156 @@
 ame();%0A%0A
+    // my favorite%0A    private static final String PLAY_STORE_BUSYBOX = %22https://play.google.com/store/apps/details?id=stericson.busybox%22;%0A%0A
     priv
@@ -767,16 +767,16 @@
     %7D;%0A%0A
-
     priv
@@ -805,16 +805,17 @@
 sBusybox
+ 
 = null;%0A
@@ -3125,11 +3125,313 @@
 d;%0A    %7D
+%0A%0A    public static void offerBusyBox(@NonNull Context context) %7B%0A        final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(PLAY_STORE_BUSYBOX));%0A        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);%0A        try %7B%0A            context.startActivity(i);%0A        %7D catch (Exception ignored) %7B %7D%0A    %7D
 %0A%7D%0A

add offer method for opening the store