﻿#!/sbin/sh
#Modified updater-binary by iicc to install Xposed without a custom recovery
#All thanks come to rovo89, the developer of Xposed
#XDA link: http://forum.xda-developers.com/showthread.php?t=3034811
#HTCmania link: http://www.htcmania.com/showthread.php?t=1020123



#Making log output
mount -o rw,remount /
mount -o rw,remount /storage/emulated
if [ ! -f "/test.txt" ]; then
	> /test.txt
	if [ -f /system/xbin/busybox ]; then
		echo ""
		sleep 1.5
	else
		echo "Error: Busybox is not installed"
		sleep 1.5
		exit 1
	fi
	rm /sdcard/XposedInstallerLog.txt
	sh /sdcard/installer.bin 2>&1 | tee /sdcard/XposedInstallerLog.txt
	exit 1
fi

rm /test.txt

echo "Start of Log:"

echo ""
echo ""

clear
echo ""
echo ""
echo ""
echo ""
echo ""
busybox echo -e "  \e[00;41m            Xposed Installer          \e[00;37;40m"
busybox echo -e "  \e[31m  --------------------------------------- \e[00;37;40m"
busybox echo -e "  \e[31m     --------------------------------    \e[00;37;40m"
busybox echo -e "  \e[01;34m                  por iicc                \e[00;37;40m"
sleep 2.2
clear
sleep 0.5
echo""
echo ""
echo ""
echo ""
echo ""
echo -n "           \e[1;35m¿Are you sure you want to install Xposed?\e[0m

                  \e[1;34mSelect an option:\e[0m

					
                    \e[1;32m[ Yes = 1\e[0m \e[1;33m|\e[0m \e[1;31mNo = 2 ]\e[0m  
					  
					  
                             "
read option
if [ $option = 1 ]; then
	echo ""
	echo ""
	echo ""
	echo ""
	echo ""
	sleep 1.5
	if [ -f /system/xbin/busybox ]; then
		echo "Busybox installed correctly xbin"
		sleep 1.5
	else
		echo "Error: Busybox is not installed"
		sleep 1.5
		exit 1
	fi
	echo ""
	sleep 1.2
	echo "Mounting partitions and creating directories..."
	echo ""
	echo ""
	sleep 0.5
	mount -o rw,remount /system
	mount -o rw,remount /storage/emulated
	mount -o rw,remount /system /system
	mount -o rw,remount /
	mount -o rw,remount / /
	mkdir -p -v /tmp/xposed
	unzip /sdcard/xposed-v* -d /tmp/xposed
	sleep 1
	echo ""
	echo ""
	echo "Running the main script..."
	echo ""
	echo ""
	else
	echo "\e[1;32mEmerging.......... \e[0m"
	sleep 2
	clear
	exit
fi

sleep 3



#main xposed installer

# Xposed framework installer zip.
#
# This script installs the Xposed framework files to the system partition.
# The Xposed Installer app is needed as well to manage the installed modules.
#
# Thanks to Chainfire for some of the functions and logic!
#


OUTFD=$2
ZIP=$3

SYSTEMLIB=/system/lib

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

ch_con() {
  LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon -h u:object_r:system_file:s0 $1
  LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon -h u:object_r:system_file:s0 $1
  chcon -h u:object_r:system_file:s0 $1
  LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon u:object_r:system_file:s0 $1
  LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon u:object_r:system_file:s0 $1
  chcon u:object_r:system_file:s0 $1
}

ch_con_ext() {
  LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox chcon $2 $1
  LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox chcon $2 $1
  chcon $2 $1
}

ln_ext() {
  LD_LIBRARY_PATH=$SYSTEMLIB /system/toolbox ln -s $1 $2
  LD_LIBRARY_PATH=$SYSTEMLIB /system/bin/toolbox ln -s $1 $2
  ln -s $1 $2
}

set_perm() {
  chown $1.$2 $4
  chown $1:$2 $4
  chmod $3 $4
  ch_con $4
  ch_con_ext $4 $5
}

cp_perm() {
  rm $5
  cat $4 > $5
  set_perm $1 $2 $3 $5 $6
}

install_and_link() {
  TARGET=$1
  XPOSED="${1}_xposed"
  BACKUP="${1}_original"
  if [ ! -f "/tmp/xposed/$XPOSED" ]; then
    return
  fi
  cp_perm $2 $3 $4 /tmp/xposed/$XPOSED $XPOSED $5
  if [ ! -f $BACKUP ]; then
    mv $TARGET $BACKUP
    ln_ext $XPOSED $TARGET
  fi
}

install_overwrite() {
  TARGET=$1
  if [ ! -f "/tmp/xposed/$TARGET" ]; then
    return
  fi
  BACKUP="${1}.orig"
  NO_ORIG="${1}.no_orig"
  if [ ! -f $TARGET ]; then
    echo -n > $NO_ORIG
  elif [ ! -f $BACKUP -a ! -f $NO_ORIG ]; then
    mv $TARGET $BACKUP
  fi
  cp_perm $2 $3 $4 /tmp/xposed/$TARGET $TARGET $5
}

cd /tmp/xposed

if [ ! -f "/tmp/xposed/system/xposed.prop" ]; then
  ui_print "! Failed: /tmp/xposed/system/xposed not found!"
  exit 1
fi

ui_print "- Checking environment"
API=$(cat /system/build.prop | grep "ro.build.version.sdk=" | dd bs=1 skip=21 count=2)
ABI=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi=" | dd bs=1 skip=19 count=3)
ABILONG=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi=" | dd bs=1 skip=19)
ABI2=$(cat /system/build.prop /default.prop | grep -m 1 "ro.product.cpu.abi2=" | dd bs=1 skip=20 count=3)

XVERSION=$(grep -m 1 "^version=" /tmp/xposed/system/xposed.prop | dd bs=1 skip=8)
XARCH=$(grep -m 1 "^arch=" /tmp/xposed/system/xposed.prop | dd bs=1 skip=5)
XMINSDK=$(grep -m 1 "^minsdk=" /tmp/xposed/system/xposed.prop | dd bs=1 skip=7)
XMAXSDK=$(grep -m 1 "^maxsdk=" /tmp/xposed/system/xposed.prop | dd bs=1 skip=7)

ARCH=arm
IS64BIT=false
RC=0
if [ "$ABI" = "x86" ]; then ARCH=x86; fi;
if [ "$ABI2" = "x86" ]; then ARCH=x86; fi;
if [ "$API" -eq "$API" ]; then
  if [ "$API" -ge "21" ]; then
    if [ "$ABILONG" = "arm64-v8a" ]; then ARCH=arm64; SYSTEMLIB=/system/lib64; IS64BIT=true; fi;
    if [ "$ABILONG" = "x86_64" ]; then ARCH=x64; SYSTEMLIB=/system/lib64; IS64BIT=true; fi;
  fi
fi

#ui_print "DBG [$API] [$ABI] [$ABI2] [$ABILONG] [$ARCH] [$XARCH] [$XMINSDK] [$XMAXSDK] [$XVERSION]"

ui_print "  Xposed version: $XVERSION"

XVALID=false
if [ "$ARCH" = "$XARCH" ]; then
  if [ "$API" -ge "$XMINSDK" ]; then
    if [ "$API" -le "$XMAXSDK" ]; then
      XVALID=true
    else
      ui_print "! Wrong SDK version: $API, expected $XMINSDK - $XMAXSDK"
    fi
  else
    ui_print "! Wrong SDK version: $API, expected $XMINSDK - $XMAXSDK"
  fi
else
  ui_print "! Wrong platform: $ARCH, expected: $XARCH"
fi

if ($XVALID); then
  ui_print "- Placing files"

  cat /system/bin/toolbox > /system/toolbox
  chmod 0755 /system/toolbox
  ch_con /system/toolbox

  cp_perm 0 0 0644 /tmp/xposed/system/xposed.prop /system/xposed.prop
  cp_perm 0 0 0644 /tmp/xposed/system/framework/XposedBridge.jar /system/framework/XposedBridge.jar

  install_and_link /system/bin/app_process32                0 2000 0755 u:object_r:zygote_exec:s0
  install_overwrite /system/bin/dex2oat                     0 2000 0755 u:object_r:dex2oat_exec:s0
  install_overwrite /system/bin/oatdump                     0 2000 0755
  install_overwrite /system/bin/patchoat                    0 2000 0755 u:object_r:dex2oat_exec:s0
  install_overwrite /system/lib/libart.so                   0    0 0644
  install_overwrite /system/lib/libart-compiler.so          0    0 0644
  install_overwrite /system/lib/libart-disassembler.so      0    0 0644
  install_overwrite /system/lib/libsigchain.so              0    0 0644
  install_overwrite /system/lib/libxposed_art.so            0    0 0644
  if ($IS64BIT); then
    install_and_link /system/bin/app_process64              0 2000 0755 u:object_r:zygote_exec:s0
    install_overwrite /system/lib64/libart.so               0    0 0644
    install_overwrite /system/lib64/libart-compiler.so      0    0 0644
    install_overwrite /system/lib64/libart-disassembler.so  0    0 0644
    install_overwrite /system/lib64/libsigchain.so          0    0 0644
    install_overwrite /system/lib64/libxposed_art.so        0    0 0644
  fi

  rm /system/toolbox
else
  ui_print "! Please download the correct package"
  ui_print "! for your platform/ROM!"
  RC=1
fi

rm -r /tmp/xposed
ui_print "- Unmounting /system"
umount /system

ui_print "- Done !"

echo""
echo""
sleep 2.5
clear
sleep 1.4
echo "Installation Complete"
sleep 0.9
echo ""
echo "Xposed Now go and do a soft reset"
sleep 0.9
echo "After reboot will be optimizing apps"
sleep 0.9
echo "Depending on the applications installed ,"
echo "IT TAKE seconds to an hour ."
sleep 0.9
echo "If all is well , Xposed will be active."
sleep 1.5
echo ""
echo "It has created a log of the installation"
echo "In the root /sdcard"
sleep 1.3
echo ""
echo "Cheers!"
echo ""
sleep 1
exit 1


