#!/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 data partition"
mount /data

ui_print " - flashing boot.img"
mv /dev/block/bootdevice/by-name/boot.bak /dev/block/bootdevice/by-name/boot 2>/dev/null
cat boot.img /dev/zero > /dev/block/bootdevice/by-name/boot 2>/dev/null

ui_print " - tuning file systems"
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 " "
ui_print "finished"
rm -rf /tmp/arter97
sync
