#!/sbin/sh
#
# SuperSU installer ZIP
# Copyright (c) 2012-2014 - Chainfire
#
# Patched by WErt(WErt) 4PDA for DOOGEE X5 3G

OUTFD=$2
ZIP=$3

SYSTEMLIB=/system/lib

ui_print() {
  /tmp/supersu/busybox echo -n -e "ui_print $1\n"
  /tmp/supersu/busybox echo -n -e "ui_print\n"
}

ch_con() {
  /tmp/supersu/busybox chcon -h u:object_r:system_file:s0 $1
  /tmp/supersu/busybox chcon u:object_r:system_file:s0 $1
}

ch_con_ext() {
  /tmp/supersu/busybox chcon $2 $1
}

ln_con() {
  /tmp/supersu/busybox ln -s $1 $2
  ch_con $2
}

set_perm() {
  /tmp/supersu/busybox chown $1.$2 $4
  /tmp/supersu/busybox chown $1:$2 $4
  /tmp/supersu/busybox chmod $3 $4
  ch_con $4
  if [ -f "$5" ]; then
    ch_con_ext $4 $5
  fi
}

cp_perm() {
  /tmp/supersu/busybox rm -f $5
  if [ -f "$4" ]; then
    /tmp/supersu/busybox cat $4 > $5
    set_perm $1 $2 $3 $5 $6
  fi
}

ui_print "*****************"
ui_print "SuperSU installer"
ui_print "*****************"

ui_print "- Mounting /system and rootfs"
/tmp/supersu/busybox mount -t ext4 /dev/block/platform/mtk-msdc.0/by-name/system /system
/tmp/supersu/busybox mount -o rw,remount /system
/tmp/supersu/busybox mount -o rw,remount /system /system
/tmp/supersu/busybox mount -o rw,remount /
/tmp/supersu/busybox mount -o rw,remount / /

/tmp/supersu/busybox cat /system/bin/toolbox > /system/toolbox
/tmp/supersu/busybox chmod 0755 /system/toolbox
ch_con /system/toolbox

API=$(/tmp/supersu/busybox cat /system/build.prop | /tmp/supersu/busybox grep "ro.build.version.sdk=" | /tmp/supersu/busybox dd bs=1 skip=21 count=2)
ABI=$(/tmp/supersu/busybox cat /system/build.prop /default.prop | /tmp/supersu/busybox grep -m 1 "ro.product.cpu.abi=" | /tmp/supersu/busybox dd bs=1 skip=19 count=3)
ABILONG=$(/tmp/supersu/busybox cat /system/build.prop /default.prop | /tmp/supersu/busybox grep -m 1 "ro.product.cpu.abi=" | /tmp/supersu/busybox dd bs=1 skip=19)
ABI2=$(/tmp/supersu/busybox cat /system/build.prop /default.prop | /tmp/supersu/busybox grep -m 1 "ro.product.cpu.abi2=" | /tmp/supersu/busybox dd bs=1 skip=20 count=3)
SUMOD=06755
SUGOTE=false
SUPOLICY=false
INSTALL_RECOVERY_CONTEXT=u:object_r:system_file:s0
MKSH=/system/bin/mksh
PIE=
ARCH=arm
APKFOLDER=false
APKNAME=/system/app/Superuser.apk
APPPROCESS=false
APPPROCESS64=false
if [ "$ABI" = "x86" ]; then ARCH=x86; fi;
if [ "$ABI2" = "x86" ]; then ARCH=x86; fi;
if [ "$API" -eq "$API" ]; then
  if [ "$API" -ge "17" ]; then
    SUGOTE=true
    PIE=.pie
    if [ "$ABILONG" = "armeabi-v7a" ]; then ARCH=armv7; fi;
    if [ "$ABI" = "mip" ]; then ARCH=mips; fi;
    if [ "$ABILONG" = "mips" ]; then ARCH=mips; fi;
  fi
  if [ "$API" -ge "18" ]; then
    SUMOD=0755
  fi
  if [ "$API" -ge "20" ]; then
    if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
    if [ "$ABILONG" = "mips64" ]; then ARCH=mips64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
    if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; SYSTEMLIB=/system/lib64; APPPROCESS64=true; fi;
    APKFOLDER=true
    APKNAME=/system/app/SuperSU/SuperSU.apk
  fi
  if [ "$API" -ge "19" ]; then
    SUPOLICY=true
    if [ "$(LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox ls -lZ /system/bin/toolbox | /tmp/supersu/busybox grep toolbox_exec > /dev/null; /tmp/supersu/busybox echo $?)" -eq "0" ]; then
      INSTALL_RECOVERY_CONTEXT=u:object_r:toolbox_exec:s0
    fi
  fi
  if [ "$API" -ge "21" ]; then
    APPPROCESS=true
  fi
fi
if [ ! -f $MKSH ]; then
  MKSH=/system/bin/sh
fi

if [ -z "$BIN" ]; then
  ui_print "- Extracting files"

  cd /tmp
  cd supersu

  /tmp/supersu/busybox  unzip -o "$ZIP"

  BIN=/tmp/supersu/$ARCH
  COM=/tmp/supersu/common
fi

ui_print "- Disabling OTA survival"
/tmp/supersu/busybox chmod 0755 $BIN/chattr$PIE
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/bin/su
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/su
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/bin/.ext/.su
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/sbin/su
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /vendor/sbin/su
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /vendor/bin/su
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /vendor/xbin/su
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/daemonsu
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/sugote
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/sugote_mksh
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/supolicy
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/xbin/ku.sud
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/lib/libsupol.so
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/lib64/libsupol.so
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/etc/install-recovery.sh
LD_LIBRARY_PATH=$SYSTEMLIB $BIN/chattr$PIE -ia /system/bin/install-recovery.sh

ui_print "- Removing old files"

if [ -f "/system/bin/install-recovery.sh" ]; then
  if [ ! -f "/system/bin/install-recovery_original.sh" ]; then
    /tmp/supersu/busybox mv /system/bin/install-recovery.sh /system/bin/install-recovery_original.sh
    ch_con /system/bin/install-recovery_original.sh
  fi
fi
if [ -f "/system/etc/install-recovery.sh" ]; then
  if [ ! -f "/system/etc/install-recovery_original.sh" ]; then
    /tmp/supersu/busybox mv /system/etc/install-recovery.sh /system/etc/install-recovery_original.sh
    ch_con /system/etc/install-recovery_original.sh
  fi
fi

/tmp/supersu/busybox rm -f /system/bin/su
/tmp/supersu/busybox rm -f /system/xbin/su
/tmp/supersu/busybox rm -f /system/sbin/su
/tmp/supersu/busybox rm -f /vendor/sbin/su
/tmp/supersu/busybox rm -f /vendor/bin/su
/tmp/supersu/busybox rm -f /vendor/xbin/su
/tmp/supersu/busybox rm -f /system/xbin/daemonsu
/tmp/supersu/busybox rm -f /system/xbin/sugote
/tmp/supersu/busybox rm -f /system/xbin/sugote-mksh
/tmp/supersu/busybox rm -f /system/xbin/supolicy
/tmp/supersu/busybox rm -f /system/xbin/ku.sud
/tmp/supersu/busybox rm -f /system/lib/libsupol.so
/tmp/supersu/busybox rm -f /system/lib64/libsupol.so
/tmp/supersu/busybox rm -f /system/bin/.ext/.su
/tmp/supersu/busybox rm -f /system/bin/install-recovery.sh
/tmp/supersu/busybox rm -f /system/etc/install-recovery.sh
/tmp/supersu/busybox rm -f /system/etc/init.d/99SuperSUDaemon
/tmp/supersu/busybox rm -f /system/etc/.installed_su_daemon
/tmp/supersu/busybox rm -f /system/app/Superuser.apk
/tmp/supersu/busybox rm -f /system/app/Superuser.odex
/tmp/supersu/busybox rm -rf /system/app/Superuser
/tmp/supersu/busybox rm -f /system/app/SuperUser.apk
/tmp/supersu/busybox rm -f /system/app/SuperUser.odex
/tmp/supersu/busybox rm -rf /system/app/SuperUser
/tmp/supersu/busybox rm -f /system/app/superuser.apk
/tmp/supersu/busybox rm -f /system/app/superuser.odex
/tmp/supersu/busybox rm -rf /system/app/superuser
/tmp/supersu/busybox rm -f /system/app/Supersu.apk
/tmp/supersu/busybox rm -f /system/app/Supersu.odex
/tmp/supersu/busybox rm -rf /system/app/Supersu
/tmp/supersu/busybox rm -f /system/app/SuperSU.apk
/tmp/supersu/busybox rm -f /system/app/SuperSU.odex
/tmp/supersu/busybox rm -rf /system/app/SuperSU
/tmp/supersu/busybox rm -f /system/app/supersu.apk
/tmp/supersu/busybox rm -f /system/app/supersu.odex
/tmp/supersu/busybox rm -rf /system/app/supersu
/tmp/supersu/busybox rm -f /system/app/VenomSuperUser.apk
/tmp/supersu/busybox rm -f /system/app/VenomSuperUser.odex
/tmp/supersu/busybox rm -rf /system/app/VenomSuperUser

ui_print "- Placing files"

/tmp/supersu/busybox mkdir /system/bin/.ext
set_perm 0 0 0777 /system/bin/.ext
cp_perm 0 0 $SUMOD $BIN/su /system/bin/.ext/.su
cp_perm 0 0 $SUMOD $BIN/su /system/xbin/su
cp_perm 0 0 0755 $BIN/su /system/xbin/daemonsu
if ($SUGOTE); then
  cp_perm 0 0 0755 $BIN/su /system/xbin/sugote u:object_r:zygote_exec:s0
  cp_perm 0 0 0755 $MKSH /system/xbin/sugote-mksh
fi
if ($SUPOLICY); then
  cp_perm 0 0 0755 $BIN/supolicy /system/xbin/supolicy
  cp_perm 0 0 0644 $BIN/libsupol.so $SYSTEMLIB/libsupol.so
fi
if ($APKFOLDER); then
  /tmp/supersu/busybox mkdir /system/app/SuperSU
  set_perm 0 0 0755 /system/app/SuperSU
fi
cp_perm 0 0 0644 $COM/Superuser.apk $APKNAME
cp_perm 0 0 0755 $COM/install-recovery.sh /system/etc/install-recovery.sh
ln_con /system/etc/install-recovery.sh /system/bin/install-recovery.sh
if ($APPPROCESS); then
  /tmp/supersu/busybox rm /system/bin/app_process
  ln_con /system/xbin/daemonsu /system/bin/app_process
  if ($APPPROCESS64); then
    if [ ! -f "/system/bin/app_process64_original" ]; then
      /tmp/supersu/busybox mv /system/bin/app_process64 /system/bin/app_process64_original
    else
      /tmp/supersu/busybox rm /system/bin/app_process64
    fi
    ln_con /system/xbin/daemonsu /system/bin/app_process64
    if [ ! -f "/system/bin/app_process_init" ]; then
      cp_perm 0 2000 0755 /system/bin/app_process64_original /system/bin/app_process_init
    fi
  else
    if [ ! -f "/system/bin/app_process32_original" ]; then
      /tmp/supersu/busybox mv /system/bin/app_process32 /system/bin/app_process32_original
    else
      /tmp/supersu/busybox rm /system/bin/app_process32
    fi
    ln_con /system/xbin/daemonsu /system/bin/app_process32
    if [ ! -f "/system/bin/app_process_init" ]; then
      cp_perm 0 2000 0755 /system/bin/app_process32_original /system/bin/app_process_init
    fi
  fi
fi
cp_perm 0 0 0744 $COM/99SuperSUDaemon /system/etc/init.d/99SuperSUDaemon
/tmp/supersu/busybox echo 1 > /system/etc/.installed_su_daemon
set_perm 0 0 0644 /system/etc/.installed_su_daemon

ui_print "- Post-installation script"
/tmp/supersu/busybox rm /system/toolbox
LD_LIBRARY_PATH=$SYSTEMLIB /system/xbin/su --install

ui_print "- Unmounting /system"
/tmp/supersu/busybox umount /system

ui_print "- Done !"
exit 0
