#!/sbin/sh

##########################################################################################
# Flashable update-binary preparation
##########################################################################################

OUTFD=$2
ZIP=$3

readlink /proc/$$/fd/$OUTFD 2>/dev/null | grep /tmp >/dev/null
if [ "$?" -eq "0" ]; then
  OUTFD=0

  for FD in `ls /proc/$$/fd`; do
    readlink /proc/$$/fd/$FD 2>/dev/null | grep pipe >/dev/null
    if [ "$?" -eq "0" ]; then
      ps | grep " 3 $FD " | grep -v grep >/dev/null
      if [ "$?" -eq "0" ]; then
        OUTFD=$FD
        break
      fi
    fi
  done
fi

mkdir -p $INSTALLER
cd $INSTALLER
unzip -o "$ZIP" common/* replace.txt

##########################################################################################
# Functions
##########################################################################################

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

grep_prop() {
  REGEX="s/^$1=//p"
  shift
  FILES=$@
  if [ -z "$FILES" ]; then
    FILES='/system/build.prop'
  fi
  cat $FILES 2>/dev/null | sed -n $REGEX | head -n 1
}

is_mounted() {
  if [ ! -z "$2" ]; then
    cat /proc/mounts | grep $1 | grep $2, >/dev/null
  else
    cat /proc/mounts | grep $1 >/dev/null
  fi
  return $?
}

set_perm() {
  chown $2:$3 $1 || exit 1
  chmod $4 $1 || exit 1
  if [ "$5" ]; then
    chcon $5 $1 2>/dev/null
  else
    chcon 'u:object_r:system_file:s0' $1 2>/dev/null
  fi
}

set_perm_recursive() {
  find $1 -type d 2>/dev/null | while read dir; do
    set_perm $dir $2 $3 $4 $6
  done
  find $1 -type f 2>/dev/null | while read file; do
    set_perm $file $2 $3 $5 $6
  done
}

mktouch() {
  mkdir -p ${1%/*}
  if [ -z "$2" ]; then
    touch $1
  else
    echo $2 > $1
  fi
}

payload_size_check() {
  reqSizeM=0;
  for entry in $(unzip -l "$@" 2>/dev/null | tail -n +4 | awk '{ print $1 }'); do
    test $entry != "--------" && reqSizeM=$((reqSizeM + entry)) || break;
  done;
  test $reqSizeM -lt 1048576 && reqSizeM=1 || reqSizeM=$((reqSizeM / 1048576));
}

target_size_check() {
  e2fsck -p -f $1
  curBlocks=`e2fsck -n $1 2>/dev/null | cut -d, -f3 | cut -d\  -f2`;
  curUsedM=$((`echo "$curBlocks" | cut -d/ -f1` * 4 / 1024));
  curSizeM=$((`echo "$curBlocks" | cut -d/ -f2` * 4 / 1024));
  curFreeM=$((curSizeM - curUsedM));
}

remove_edxposed() {
    ui_print "- Deleting EdXposed..."
    rm -r -f /data/adb/modules/riru_edxposed/
    rm -r -f /data/adb/modules/riru_edxposed_sandhook/
    rm -r -f /data/adb/modules/riru_edxposed_whale/
    rm -r -f /data/misc/riru/modules/edxposed/
    rm -r -f /data/misc/riru/modules/edxp/
    ui_print "- Done"
}
##########################################################################################
# Preparation
##########################################################################################

# Print your mod name
ui_print "*******************************"
ui_print " EdXposed Uninstaller "
ui_print "*******************************"

mount /data 2>/dev/null

if [ -d "/data/adb/magisk/" ]; then

remove_edxposed
exit 0
    
fi

mount /cache 2>/dev/null
umount /magisk 2>/dev/null
mkdir /magisk 2>/dev/null
IMG=

if (is_mounted /data); then
  IMG=/data/adb/magisk.img
  if [ ! -f "$IMG" ]; then
    ui_print "! Magisk is not installed!"
    ui_print "! Magisk is required for this mod!"
    exit 1
  fi
else
  IMG=/cache/magisk.img
  if [ ! -f "$IMG" ]; then
    ui_print " "
    ui_print "***********************************"
    ui_print "*      !! Data unavalible !!      *"
    ui_print "* Magisk detection is impossible  *"
    ui_print "* Installation will still proceed *"
    ui_print "*  But please make sure you have  *"
    ui_print "*        Magisk installed!!       *"
    ui_print "***********************************"
    ui_print " "
  fi
fi

if [ -f "$IMG" ]; then
  ui_print "- $IMG detected!"
  target_size_check $IMG
  if [ "$reqSizeM" -gt "$curFreeM" ]; then
    SIZE=$((((reqSizeM + curUsedM) / 32 + 2) * 32))
    ui_print "- Resizing $IMG to ${SIZE}M..."
    resize2fs $IMG ${SIZE}M
  fi
else
  ui_print "! $IMG not detected!"
  ui_print "! Abort"
  exit 1
fi

ui_print "- Mounting $IMG to /magisk"
LOOPDEVICE=
for LOOP in 0 1 2 3 4 5 6 7; do
  if (! is_mounted /magisk); then
    LOOPDEVICE=/dev/block/loop$LOOP
    if [ ! -f "$LOOPDEVICE" ]; then
      mknod $LOOPDEVICE b 7 $LOOP
    fi
    losetup $LOOPDEVICE $IMG
    if [ "$?" -eq "0" ]; then
      mount -t ext4 -o loop $LOOPDEVICE /magisk
      if (! is_mounted /magisk); then
        /system/bin/toolbox mount -t ext4 -o loop $LOOPDEVICE /magisk
      fi
      if (! is_mounted /magisk); then
        /system/bin/toybox mount -t ext4 -o loop $LOOPDEVICE /magisk
      fi
    fi
    if (is_mounted /magisk); then
      break;
    fi
  fi
done

remove_edxposed
exit 0
