#!/sbin/sh

RECOVERY_SCRIPT_FD=$2

ui_print() {
    echo "ui_print $1" > /proc/self/fd/$RECOVERY_SCRIPT_FD
    echo "ui_print" > /proc/self/fd/$RECOVERY_SCRIPT_FD
}

stdout_to_ui_print() {
    while read -r line; do
        ui_print "$line"
    done
}

ui_print ""
ui_print ""
ui_print "---------------------"
ui_print "   Xposed disabler   "
ui_print "---------------------"
ui_print ""

ui_print "Mounting /system partition"
mount /system
mount -o rw,remount /system

{
if [ -f /system/bin/app_process.orig ]
then
    ui_print "Restoring the original app_process file"
    cp -a /system/bin/app_process.orig /system/bin/app_process || exit 1
    chown 0:2000 /system/bin/app_process || exit 1
    chmod 0755 /system/bin/app_process || exit 1
    /system/bin/restorecon /system/bin/app_process 2>/dev/null

    ui_print "Restore complete"
    ui_print "You should now be able to boot normally"
    ui_print "and remove all traces of Xposed"
    ui_print "from within the Installer app"
else
    ui_print ""
    ui_print "!!! No backup found for app_process, nothing to be performed !!!"
fi
} 2>&1 | stdout_to_ui_print

ui_print ""
ui_print "Done"
ui_print ""

exit 0
