#!/sbin/sh
#
# 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.
#
# RMM State Remover for Samsung Devices
# Original idea by BlackMesa123 @XDA developers, updated by _alexndr @XDA developers
# to add support modern Samsung devices with separate /vendor partition
# Furtherly updated and adapted by corsicanu @ xda-developers for Pie support and new RMM(KG)
#

OUTFD=$2

# Detect real $OUTFD
readlink /proc/$$/fd/$OUTFD 2>/dev/null | grep /tmp >/dev/null
if [ $? -eq 0 ] ; then
    OUTFD=0
    for FD in `ls /proc/$$/fd` ; do
        readlink /proc/$$/fd/$FD 2>/dev/null | grep pipe >/dev/null
        if [ $? -eq 0 ] ; then
            ps | grep " 3 $FD " | grep -v grep >/dev/null
            if [ $? -eq 0 ] ; then
                OUTFD=$FD
                break
            fi
        fi
    done
fi

ui_print() {
    echo -n -e "ui_print $1\n" >> /proc/self/fd/$OUTFD
    echo -n -e "ui_print\n" >> /proc/self/fd/$OUTFD
}

resolve_link() {
    if [ -z "$1" ] || [ ! -e $1 ] ; then return 1 ; fi
    local VAR=$1
    while [ -L $VAR ] ; do
        VAR=$(readlink $VAR)
    done
    echo $VAR
}

is_mounted() {
    if [ -z "$2" ] ; then
        cat /proc/mounts | grep "$1" >/dev/null
    else
        cat /proc/mounts | grep "$1" | grep "$2," >/dev/null
    fi
    return $?
}

SYSTEM=$(resolve_link $(find /dev/block/platform -type l -iname system))
VENDOR=$(resolve_link $(find /dev/block/platform -type l -iname vendor))

ui_print " "
ui_print "RMM State Remover for Samsung Devices"
ui_print "by corsicanu @XDA developers"
ui_print " "

ui_print "- Mounting /system..."
(! is_mounted /system) && mount /system
(! is_mounted /system rw) && mount -o rw,remount /system
(! is_mounted /system) && mount -t auto $SYSTEM /system
if (! is_mounted /system) ; then
    ui_print "Failed! Can't mount /system, aborting!"
    ui_print " "
    exit 1
fi

if [ ! -z "$VENDOR" ] ; then
    ui_print "- Mounting /vendor..."
    (! is_mounted /vendor) && mount /vendor
    (! is_mounted /vendor rw) && mount -o rw,remount /vendor
    if (! is_mounted /vendor) ; then
        mkdir -p /vendor
        mount -t auto $VENDOR /vendor
    fi
    if (! is_mounted /vendor) ; then
        ui_print "Failed! Can't mount /vendor, aborting!"
        ui_print " "
        exit 1
    fi
fi

ui_print "- Applying Bypass/Fix..."
sed -i 's/vaultkeeper\.feature\=1/vaultkeeper\.feature\=0/' /system/build.prop 2>/dev/null
sed -i 's/vaultkeeper\.feature\=1/vaultkeeper\.feature\=0/' /vendor/build.prop 2>/dev/null
sed -i 's/vaultkeeper\.feature\=1/vaultkeeper\.feature\=0/' /system/vendor/build.prop 2>/dev/null
rm -rf /system/lib*/libvk* 2>/dev/null
rm -rf /system/vendor/lib*/libvk* 2>/dev/null
rm -rf /vendor/lib*/libvk* 2>/dev/null
rm -rf /system/priv-app/Rlc 2>/dev/null
rm -rf /system/priv-app/KnoxGuard 2>/dev/null

ui_print "- Unmounting /system..."
umount /system
(is_mounted /vendor) && ui_print "- Unmounting /vendor..."
umount /vendor 2>/dev/null

ui_print " "
ui_print "Please don't forget this comes with "
ui_print "no warranty, you may get locked again "
ui_print "without any notice, don't blame me or "
ui_print "other devs involved in finding this "
ui_print "bypass for your failure."
ui_print " "
ui_print "Done! Reboot and enjoy"

exit 0
