#!/sbin/sh

OUTFD=$2
ZIP=$3

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

cd /tmp
rm -rf arter97
mkdir arter97
cd arter97
unzip -o "$ZIP"

ui_print " ";
ui_print " - mounting system partition"
mount /system

ui_print " - mounting data partition"
mount /data

ui_print " - flashing boot.img"
cat boot.img > /dev/block/platform/15570000.ufs/by-name/BOOT

ui_print " - removing Knox"
cd /system

rm -rf *app/BBCAgent*
rm -rf *app/Bridge*
rm -rf *app/ContainerAgent*
rm -rf *app/ContainerEventsRelayManager*
rm -rf *app/DiagMonAgent*
rm -rf *app/ELMAgent*
rm -rf *app/FotaClient*
rm -rf *app/FWUpdate*
rm -rf *app/FWUpgrade*
rm -rf *app/HLC*
rm -rf *app/KLMSAgent*
mv app/KnoxSwitcher .
rm -rf *app/*Knox*
mv KnoxSwitcher app/
rm -rf *app/*KNOX*
rm -rf *app/LocalFOTA*
rm -rf *app/RCPComponents*
rm -rf *app/SecKids*
rm -rf *app/SecurityLogAgent*
rm -rf *app/SPDClient*
rm -rf *app/SyncmlDM*
rm -rf *app/UniversalMDMClient*
rm -rf container/*Knox*
rm -rf container/*KNOX*

cp /tmp/arter97/kernel.prop /system/vendor/build.prop
chown 0.0 /system/vendor/build.prop
chmod 644 /system/vendor/build.prop

cd /
ui_print " - setting system-mode SuperSU"
echo "SYSTEMLESS=false" > /system/.supersu

ui_print " - tuning file systems"
fstrim -v /system > /dev/null 2>&1
fstrim -v /data   > /dev/null 2>&1
fstrim -v /cache  > /dev/null 2>&1

ui_print " - unmounting partition /data"
umount /data > /dev/null 2>&1

ui_print " - unmounting partition /system"
umount /system > /dev/null 2>&1

ui_print " "
ui_print "finished"
rm -rf /tmp/arter97
sync
