#!/sbin/sh
# unSU Script: Recovery Flashable Zip
# osm0sis @ xda-developers

OUTFD=/proc/self/fd/$2;
ui_print() { echo -e "ui_print $1\nui_print" > $OUTFD; }
show_progress() { echo "progress $1 $2" > $OUTFD; }

ui_print " ";
ui_print "unSU Script";
ui_print "by osm0sis @ xda-developers";
show_progress 1.34 2;

ui_print " ";
ui_print "Mounting...";
ui_print " ";
mount -o ro /system;
mount /data;
mount /cache;

if [ -e /data/su ]; then
  ui_print "Removing phh's SuperUser...";
  rm -rf /data/app/me.phh.superuser* /data/data/me.phh.superuser* /data/su;
  bootmsg=1;
fi;

if [ -e /cache/su.img -o -e /data/su.img ]; then
  ui_print "Removing SuperSU Systemless...";
  umount /su;
  rm -rf /cache/.supersu /cache/su.img /cache/SuperSU.apk /data/.supersu /data/stock_boot_*.img.gz /data/su.img /data/SuperSU.apk /data/app/eu.chainfire.supersu* /data/data/eu.chainfire.supersu*;
  bootmsg=1;
fi;

if [ -e /system/bin/.ext/.su ]; then
  ui_print "Removing SuperSU...";
  mount -o rw,remount /system;

  rm -rf /system/.pin /system/.supersu /system/app/Superuser.apk /system/app/SuperSU /system/bin/.ext /system/bin/app_process_init /system/etc/.installed_su_daemon /system/etc/install-recovery.sh  /system/etc/init.d/99SuperSUDaemon /system/lib/libsupol.so /system/lib64/libsupol.so /system/su.d /system/xbin/daemonsu /system/xbin/su /system/xbin/sugote /system/xbin/sugote-mksh /system/xbin/supolicy /cache/.supersu /data/.supersu /data/app/eu.chainfire.supersu* /data/data/eu.chainfire.supersu*;

  mv -f /system/bin/app_process32_original /system/bin/app_process32;
  mv -f /system/bin/app_process64_original /system/bin/app_process64;
  mv -f /system/bin/install-recovery_original.sh /system/bin/install-recovery.sh;

  cd /system/bin;
  if [ -e app_process64 ]; then
    ln -sf app_process64 app_process;
  else
    ln -sf app_process32 app_process;
  fi;
fi;

if [ -e /system/bin/su -a "$(strings /system/xbin/su | grep koush)" ]; then
  ui_print "Removing Koush's SuperUser...";
  mount -o rw,remount /system;

  rm -rf /system/app/Superuser.apk /system/bin/su /system/etc/.has_su_daemon /system/etc/.installed_su_daemon /system/xbin/su /cache/su /cache/Superuser.apk /cache/install-recovery-sh /data/app/com.koushikdutta.superuser* /data/data/com.koushikdutta.superuser*;
fi;

if [ -e /system/bin/su -o -e /system/xbin/su ]; then
  ui_print "Removing ROM su binary...";
  mount -o rw,remount /system;
  rm -rf /system/bin/su /system/xbin/su;
fi;

ui_print " ";
ui_print "Unmounting...";
umount /system;
umount /data;
umount /cache;

ui_print " ";
ui_print "Done!";

if [ "$bootmsg" ]; then
  ui_print " ";
  ui_print "NOTE: Flash your ROM's default boot.img to complete the unrooting process!"
fi;
exit 0;

