#!/bin/sh
#Based on AndroidPC.es / Daemonium root_u1.bat script

echo ""
echo "  Getting root! Here we go!. // Rooteando! Alla vamos!"
echo ""
echo "  *** USB Debugging must be set to ON in the developer options ***"
echo "  *** La depuracion USB debe estar en ACTIVA en ajustes de desarrollo ***"
echo ""
echo "  *** Be sure the TVBox is connected by ADB Wifi. ***"
echo "  *** Asegurate que tu TVBox conectado por ADB Wifi. ***"
echo ""
echo "  Write your IP like: 192.168.X.XX and press ENTER"
echo "  Escribe a continuacion la IP, ej: 192.168.X.XX y pulsa INTRO"
echo ""

if [ -z $1 ]; then
	echo "Usage sudo ./root_s905.sh IP_address"
	exit 0
fi

adb connect $1
adb root
sleep 5
adb shell id
echo ""
echo "  If ID above says "0" or "root", then you are ready to go, just press enter"
echo "  Si el ID marca "0" o "root" entonces puedes continuar, pulsa enter"
echo ""
echo "  If otherwise press CTRL + C to cancel"
echo "  Si marca cualquier otra cosa, pulsa CTRL + C para cancelar"
echo ""  
read continue

echo ""
echo "  Removing old superuser and root related files ... "
echo "  Eliminando ficheros viejos relacionados con root y superuser"
echo ""  
adb shell mount -o rw,remount /system
adb shell rm -f /system/bin/su
adb shell rm -f /system/xbin/su
adb shell rm -f /system/xbin/daemonsu
adb shell rm -f /system/bin/.ext/.su
adb shell rm -f /system/etc/install-recovery.sh
adb shell rm -f /system/etc/init.d/99SuperSUDaemon
adb shell rm -f /system/etc/.installed_su_daemon
adb shell rm -f /system/app/Superuser.apk
adb shell rm -f /system/app/Superuser.odex
adb shell rm -f /system/app/SuperUser.apk
adb shell rm -f /system/app/SuperUser.odex
adb shell rm -f /system/app/superuser.apk
adb shell rm -f /system/app/superuser.odex
adb shell rm -f /system/app/Supersu.apk
adb shell rm -f /system/app/Supersu.odex
adb shell rm -f /system/app/SuperSU.apk
adb shell rm -f /system/app/SuperSU.odex
adb shell rm -f /system/app/supersu.apk
adb shell rm -f /system/app/supersu.odex
adb shell rm -f /data/dalvik-cache/*com.noshufou.android.su*
adb shell rm -f /data/dalvik-cache/*com.koushikdutta.superuser*
adb shell rm -f /data/dalvik-cache/*com.mgyun.shua.su*
adb shell rm -f /data/dalvik-cache/*Superuser.apk*
adb shell rm -f /data/dalvik-cache/*SuperUser.apk*
adb shell rm -f /data/dalvik-cache/*superuser.apk*
adb shell rm -f /data/dalvik-cache/*eu.chainfire.supersu*
adb shell rm -f /data/dalvik-cache/*Supersu.apk*
adb shell rm -f /data/dalvik-cache/*SuperSU.apk*
adb shell rm -f /data/dalvik-cache/*supersu.apk*
adb shell rm -f /data/dalvik-cache/*.oat
adb shell rm -f /data/app/com.noshufou.android.su-*
adb shell rm -f /data/app/com.koushikdutta.superuser-*
adb shell rm -f /data/app/com.mgyun.shua.su-*
adb shell rm -f /data/app/eu.chainfire.supersu-*
sleep 2

echo ""
echo "  Sending new files to safe place..."
echo "  Enviando ficheros nuevos a un sitio seguro"
echo ""  
adb push root /data/local/tmp/ > null
sleep 2
echo ""
echo "  Copy files from safe place to system ..."
echo "  Copiando ficheros de sitio seguro a system"
echo ""  
adb shell mkdir /system/bin/.ext >null
adb shell mkdir /system/etc/init.d >null
adb shell mkdir /system/app/SuperSU
adb shell chmod 755 /system/app/SuperSU
adb shell cp /data/local/tmp/system/xbin/su /system/xbin/daemonsu
adb shell cp /data/local/tmp/system/xbin/su /system/xbin/su
adb shell cp /data/local/tmp/system/xbin/su /system/bin/.ext/.su
adb shell cp /data/local/tmp/system/app/SuperSU.apk /system/app/SuperSU/SuperSU.apk
adb shell cp /data/local/tmp/system/app/SuperSU.apk /data/app/SuperSU.apk
adb shell cp /data/local/tmp/system/app/install-recovery.sh /system/etc/install-recovery.sh
adb shell cp /data/local/tmp/system/app/99SuperSUDaemon /system/etc/init.d/99SuperSUDaemon
adb shell cp /data/local/tmp/system/preinstall.sh /system/bin/preinstall.sh
adb shell chmod 755 /system/bin/preinstall.sh
adb shell /system/xbin/busybox touch /system/etc/.installed_su_daemon
sleep 2

echo ""
echo "  Setting permissions"
echo "  Otorgando permisos"
echo ""  
adb shell chown root.root /system/bin/.ext
adb shell chown root.root /system/bin/.ext/.su
adb shell chown root.root /system/xbin/su
adb shell chown root.root /system/xbin/daemonsu
adb shell chown root.root /system/etc/install-recovery.sh
adb shell chown root.root /system/etc/init.d/99SuperSUDaemon
adb shell chown root.root /system/etc/.installed_su_daemon
adb shell chown root.root /system/app/SuperSU/SuperSU.apk
adb shell chmod 0777 /system/bin/.ext
adb shell chmod 06755 /system/bin/.ext/.su
adb shell chmod 06755 /system/xbin/su
adb shell chmod 0755 /system/xbin/daemonsu
adb shell chmod 0755 /system/etc/install-recovery.sh
adb shell chmod 0755 /system/etc/init.d/99SuperSUDaemon
adb shell chmod 0644 /system/etc/.installed_su_daemon
adb shell chmod 0644 /system/app/SuperSU.apk
adb shell /system/xbin/su --install
adb shell /system/xbin/busybox --install -s /system/xbin
sleep 2
echo ""
echo "  Cleaning the unnecessary files"
echo "  Limpiando ficheros innecesarios"
echo ""  
adb shell rm -fR data/local/tmp/system/
sleep 2
echo ""  
echo "  Got r00t!!"
echo "  Ya eres r00t!!"
echo ""  
echo "  After press ENTER the TVBox will restart, wait 1 min"
echo "  Al pulsar ENTER el TVBox se reiniciara, espere 1 minuto"
echo ""  
echo ""  
echo "  If the system does not reboot automatically restart it manually"
echo "  Si el sistema no se reinicie automaticamente reinicie manualmente"
echo "" 
adb reboot &
exit 1

