#!/system/bin/sh

mkdir /system/app/SuperSU
cp /data/local/tmp/systemroot/Superuser.apk /system/app/SuperSU/SuperSU.apk
chmod 0644 /system/app/SuperSU/SuperSU.apk
chcon -R u:object_r:system_file:s0 /system/app/SuperSU/SuperSU.apk

cp /data/local/tmp/systemroot/install-recovery.sh /system/etc/install-recovery.sh
chmod 0755 /system/etc/install-recovery.sh
chcon u:object_r:toolbox_exec:s0 /system/etc/install-recovery.sh
rm /system/bin/install-recovery.sh
ln -s /system/etc/install-recovery.sh /system/bin/install-recovery.sh 

cp /data/local/tmp/systemroot/su /system/xbin/su
cp /data/local/tmp/systemroot/su /system/bin/.ext/.su
cp /data/local/tmp/systemroot/su /system/xbin/daemonsu
cp /data/local/tmp/systemroot/su /system/xbin/sugote
chmod 0755 /system/xbin/su
chmod 0755 /system/bin/.ext/.su
chmod 0755 /system/xbin/daemonsu
chmod 0755 /system/xbin/sugote
chcon u:object_r:system_file:s0 /system/xbin/su
chcon u:object_r:system_file:s0 /system/bin/.ext/.su
chcon u:object_r:system_file:s0 /system/xbin/daemonsu
chcon u:object_r:zygote_exec:s0 /system/xbin/sugote

cp /data/local/tmp/systemroot/supolicy /system/xbin/supolicy
chmod 0755 /system/xbin/supolicy
chcon u:object_r:system_file:s0 /system/xbin/supolicy

cp /data/local/tmp/systemroot/libsupol.so /system/lib64/libsupol.so
chmod 0644 /system/lib64/libsupol.so
chcon u:object_r:system_file:s0 /system/lib64/libsupol.so

cp /system/bin/sh /system/xbin/sugote-mksh
chmod 0755 /system/xbin/sugote-mksh
chcon u:object_r:system_file:s0 /system/xbin/sugote-mksh

cp /system/bin/app_process /system/bin/app_process_original
cp /system/bin/app_process64 /system/bin/app_process64_original
cp /system/bin/app_process64 /system/bin/app_process_init
chmod 0755 /system/bin/app_process_original
chmod 0755 /system/bin/app_process64_original
chmod 0755 /system/bin/app_process_init
chcon u:object_r:zygote_exec:s0 /system/bin/app_process64_original
chcon u:object_r:system_file:s0 /system/bin/app_process_init
rm /system/bin/app_process
ln -s /system/xbin/daemonsu /system/bin/app_process
rm /system/bin/app_process64
ln -s /system/xbin/daemonsu /system/bin/app_process64

touch /data/.supersu
chmod 0755 /data/.supersu
echo "SYSTEMLESS=false" > /data/.supersu

sync
./system/xbin/su --install
reboot
