#!/sbin/sh
# arg 1 is recovery api version, generally 3.
# arg 2 is the pipe fd, to the recovery binary.
# communicate with it using the recovery api.
# arg 3 is the zip file
echo -n -e 'ui_print Installing Superuser...\n' > /proc/self/fd/$2
echo -n -e 'ui_print\n' > /proc/self/fd/$2

# detect binary versions to install
ARCH=$(uname -m)

# x86 needs to match: i486, i686, x86_64, ...
if echo "$ARCH" | grep -q 86; then
  PLATFORM=x86
elif [ "$ARCH" = "mips" -o "$ARCH" = "mips64" ]; then
  PLATFORM=mips
else
  PLATFORM=armeabi
fi

cd /tmp
mkdir superuser
cd superuser
unzip -o "$3"
if [ "$?" -ne "0" ]
then
  mkdir -p $PLATFORM
  cp /cache/su $PLATFORM/su
  cp /cache/Superuser.apk .
  cp /cache/install-recovery.sh .
fi


echo -n -e 'ui_print Installing '$PLATFORM' binaries...\n' > /proc/self/fd/$2
echo -n -e 'ui_print\n' > /proc/self/fd/$2

mount /system
chattr -i /system/bin/su
chattr -i /system/xbin/su
rm -f /system/bin/su
rm -f /system/xbin/su
rm -f /system/app/Superuser.*
rm -f /system/app/Supersu.*
rm -f /system/app/superuser.*
rm -f /system/app/supersu.*
rm -f /system/app/SuperUser.*
rm -f /system/app/SuperSU.*

cp $PLATFORM/su /system/xbin/su
chown 0:0 /system/xbin/su
chmod 6755 /system/xbin/su
ln -s /system/xbin/su /system/bin/su

cp Superuser.apk /system/app
chmod 644 /system/app/Superuser.apk

# if the system is at least 4.3, and there is no su daemon built in,
# let's try to install it using install-recovery.sh
BUILD_RELEASE_VERSION="$(grep 'ro\.build\.version\.release' /system/build.prop)"
VERSION="${BUILD_RELEASE_VERSION##*=}"
MAJ=${VERSION%%.*}
MIN=${VERSION#*.}
MIN=${MIN//.*}

if [ "${MAJ}${MIN}" -ge 43 ]
then
  # check for rom su daemon before clobbering install-recovery.sh
  if [ ! -f "/system/etc/.has_su_daemon" ]
  then
      echo -n -e 'ui_print Installing Superuser daemon...\n' > /proc/self/fd/$2
      echo -n -e 'ui_print\n' > /proc/self/fd/$2
      chattr -i /system/etc/install-recovery.sh
      cp install-recovery.sh /system/etc/install-recovery.sh
      chmod 755 /system/etc/install-recovery.sh
      # note that an post install su daemon was installed
      # so recovery doesn't freak out and recommend you disable
      # the install-recovery.sh execute bit.
      touch /system/etc/.installed_su_daemon
  fi
fi

umount /system