/*
 * Copyright (C) 2015 Willi Ye
 *
 * 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.grarak.kerneladiutor.utils.tools;

import com.grarak.kerneladiutor.utils.Constants;
import com.grarak.kerneladiutor.utils.Utils;
import com.kerneladiutor.library.root.RootUtils;

import java.util.LinkedHashMap;

/**
 * Created by willi on 01.01.15.
 */
public class Buildprop implements Constants {

    public static void overwrite(String oldKey, String oldValue, String newKey, String newValue, String busybox) {
        RootUtils.mount(true, "/system");
        RootUtils.runCommand(busybox + " sed -ir \"s/" + oldKey+ "=" + oldValue + "/" + newKey + "=" + newValue + "/\" "  + BUILD_PROP);
    }

    public static void delete(String key, String busybox) {
        if (!key.isEmpty()) {
            RootUtils.mount(true, "/system");
            RootUtils.runCommand(busybox + " sed -ir \"/" + key + "=/d\" " + BUILD_PROP);
        }
    }

    public static void addKey(String key, String value) {
        RootUtils.mount(true, "/system");
        RootUtils.runCommand("echo " + key + "=" + value + " >> " + BUILD_PROP);
    }

    public static LinkedHashMap<String, String> getProps() {
        LinkedHashMap<String, String> list = new LinkedHashMap<>();
        String buildprop;
        if ((buildprop = Utils.readFile(BUILD_PROP)) != null) {
            String[] values = buildprop.split("\\r?\\n");
            for (String prop : values)
                if (!prop.isEmpty() && !prop.startsWith("#")) {
                    String[] line = prop.split("=");

                    StringBuilder value = new StringBuilder();
                    if (line.length > 1) {
                        for (int i = 1; i < line.length; i++) value.append(line[i]).append("=");
                        value.setLength(value.length() - 1);
                    }
                    list.put(line.length > 0 ? line[0].trim() : "", value.toString().trim());
                }
        }
        return list;
    }

    public static boolean hasBuildprop() {
        return getProps().size() > 0;
    }

}

@@ -1030,116 +1030,139 @@
     
-RootUtils.mount(true, %22/system%22);%0A        RootUtils.runCommand(busybox + %22 sed -ir %5C%22s/%22 + oldKey+ %22
+String oldvalue = oldKey + %22=%22 + oldValue;%0A        String newvalue = newKey + %22=%22 + newValue;%0A        String command = %22old
 =%22 + old
 Valu
@@ -1157,25 +1157,25 @@
 =%22 + old
-V
+v
 alue + %22
 /%22 + new
@@ -1170,31 +1170,28 @@
  + %22
-/
+ && 
 %22 + 
+%22
 new
-Key + %22
 =%22 + new
 Valu
@@ -1190,17 +1190,17 @@
  new
-V
+v
 alue + %22
 /%5C%22 
@@ -1199,14 +1199,57 @@
  + %22
-/%5C%22 
+ && %22 + busybox + %22 sed -i -r %5C%22s%25$old%25$new%25g%5C
 %22 
+%22
  + B
@@ -1249,32 +1249,112 @@
 %22 %22 + BUILD_PROP
+;%0A        RootUtils.mount(true, %22/system%22);%0A        RootUtils.runCommand(command
 );%0A    %7D%0A%0A    pu
@@ -1475,32 +1475,32 @@
 ue, %22/system%22);%0A
-
             Root
@@ -1534,16 +1534,18 @@
 %22 sed -i
+ -
 r %5C%22/%22 +

improve and fix props with ///