#!/sbin/sh
# Detect whether in boot mode
[ -z "$BOOTMODE" ] && ps | grep zygote | grep -qv grep && BOOTMODE=true
[ -z $BOOTMODE ] && ps -A 2>/dev/null | grep zygote | grep -qv grep && BOOTMODE=true
[ -z $BOOTMODE ] && BOOTMODE=false

test "$ANDROID_ROOT" || ANDROID_ROOT=/system;
# if the device has system as root, following will be set to true during the mount process
SYSTEM_ROOT=false
system=/system
nikGappsLog="/tmp/NikGapps.log"
datetime=$(date +%Y_%m_%d_%H_%M_%S)
nikGappsLogFile="NikGapps_logs_$datetime.tar.gz"
recoveryLog=/tmp/recovery.log
logDir="/tmp/NikGapps/logs"
nikGappsDir="/sdcard/NikGapps"
dynamic_partitions="false"

file_getprop() { $BB grep "^$2=" "$1" | $BB cut -d= -f2-; }
grep_cmdline() {
  local REGEX="s/^$1=//p"
  cat /proc/cmdline | tr '[:space:]' '\n' | sed -n "$REGEX" 2>/dev/null
}
# Setting up mount point
setup_mountpoint() {
  addToLog "- Setting up mount point $1 before actual mount"
  test -L "$1" && $BB mv -f "$1" "${1}"_link;
  if [ ! -d "$1" ]; then
    rm -f "$1";
    mkdir -p "$1";
  fi;
}
# Check if the partition is mounted
is_mounted() {
  addToLog "- Checking if $1 is mounted"
  $BB mount | $BB grep -q " $1 ";
}

# More info on Apex here -> https://www.xda-developers.com/android-q-apex-biggest-thing-since-project-treble/
mount_apex() {
  addToLog "- Setting up Apex Mount"
  test -d /system/apex || return 1;
  local apex dest loop minorx num;
  setup_mountpoint /apex;
  test -e /dev/block/loop1 && minorx=$($BB ls -l /dev/block/loop1 | $BB awk '{ print $6 }') || minorx=1;
  num=0;
  for apex in /system/apex/*; do
    dest=/apex/$($BB basename "$apex" .apex);
    test "$dest" == /apex/com.android.runtime.release && dest=/apex/com.android.runtime;
    $BB mkdir -p $dest;
    case $apex in
      *.apex)
        $BB unzip -qo "$apex" apex_payload.img -d /apex;
        $BB mv -f /apex/apex_payload.img $dest.img;
        $BB mount -t ext4 -o ro,noatime $dest.img $dest 2>/dev/null;
        if [ $? != 0 ]; then
          while [ $num -lt 64 ]; do
            loop=/dev/block/loop$num;
            ($BB mknod $loop b 7 $((num * minorx));
            $BB losetup $loop $dest.img) 2>/dev/null;
            num=$((num + 1));
            $BB losetup $loop | $BB grep -q $dest.img && break;
          done;
          $BB mount -t ext4 -o ro,loop,noatime $loop $dest;
          if [ $? != 0 ]; then
            $BB losetup -d $loop 2>/dev/null;
          fi;
        fi;
      ;;
      *) $BB mount -o bind "$apex" $dest;;
    esac;
  done;
  export ANDROID_RUNTIME_ROOT=/apex/com.android.runtime;
  export ANDROID_TZDATA_ROOT=/apex/com.android.tzdata;
  export BOOTCLASSPATH=/apex/com.android.runtime/javalib/core-oj.jar:/apex/com.android.runtime/javalib/core-libart.jar:/apex/com.android.runtime/javalib/okhttp.jar:/apex/com.android.runtime/javalib/bouncycastle.jar:/apex/com.android.runtime/javalib/apache-xml.jar:/system/framework/framework.jar:/system/framework/ext.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/android.test.base.jar:/system/framework/telephony-ext.jar:/apex/com.android.conscrypt/javalib/conscrypt.jar:/apex/com.android.media/javalib/updatable-media.jar;
}
# Unmount apex partition upon recovery cleanup
umount_apex() {
  test -d /apex || return 1;
  local dest loop;
  for dest in $($BB find /apex -type d -mindepth 1 -maxdepth 1); do
    if [ -f "$dest".img ]; then
      loop=$($BB mount | $BB grep "$dest" | $BB cut -d" " -f1);
    fi;
    ($BB umount -l "$dest";
    $BB losetup -d "$loop") 2>/dev/null;
  done;
  $BB rm -rf /apex 2>/dev/null;
  unset ANDROID_RUNTIME_ROOT ANDROID_TZDATA_ROOT BOOTCLASSPATH;
}
# Mount all the partitions
mount_all() {
  dynamic_partitions=$(getprop ro.boot.dynamic_partitions)
  addToLog "- dynamic_partitions is $dynamic_partitions"
  # Check A/B slot
  SLOT=$(grep_cmdline androidboot.slot_suffix)
  if [ -z "$SLOT" ]; then
    SLOT=$(grep_cmdline androidboot.slot)
    [ -z "$SLOT" ] || SLOT=_${SLOT}
  fi
  [ -z "$SLOT" ] || ui_print "- Current boot slot: $SLOT"
  if ! is_mounted /data; then
    ui_print "- Mounting /data"
    $BB mount /data;
    UMOUNT_DATA=1;
  else
    addToLog "- /data already mounted!"
  fi;
  ui_print "- Mounting /vendor"
  $BB mount -o rw -t auto /vendor 2>/dev/null;
  ui_print "- Mounting /product"
  $BB mount -o rw -t auto /product 2>/dev/null;
  addToLog "- Setting up mount point $ANDROID_ROOT"
  setup_mountpoint $ANDROID_ROOT;
  if ! is_mounted $ANDROID_ROOT; then
    ui_print "- $ANDROID_ROOT not mounted, mounting.."
    $BB mount -o rw -t auto $ANDROID_ROOT 2>/dev/null;
  fi;
  case $ANDROID_ROOT in
    /system_root) setup_mountpoint /system;;
    /system)
      if ! is_mounted /system && ! is_mounted /system_root; then
        setup_mountpoint /system_root;
        addToLog "- mounting /system_root partition as rw"
        $BB mount -o rw -t auto /system_root;
      elif [ -f /system/system/build.prop ]; then
        setup_mountpoint /system_root;
        addToLog "- Moving /system to /system_root"
        SYSTEM_ROOT=true
        $BB mount --move /system /system_root;
      fi;
      if [ $? != 0 ]; then
        addToLog "- Unmounting and Remounting /system as /system_root"
        umount /system
        umount -l /system 2>/dev/null
        if [ "$dynamic_partitions" == "true" ]; then
          addToLog "- Device with dynamic partitions Found"
          test -e /dev/block/mapper/system || local slot=$(getprop ro.boot.slot_suffix 2>/dev/null)
          addToLog "- Mounting /system$slot as read only"
          mount -o ro -t auto /dev/block/mapper/system"$slot" /system_root
          addToLog "- Mounting /vendor$slot as read only"
          mount -o ro -t auto /dev/block/mapper/vendor"$slot" /vendor 2>/dev/null
          addToLog "- Mounting /product$slot as read only"
          mount -o ro -t auto /dev/block/mapper/product"$slot" /product 2>/dev/null
        else
          test -e /dev/block/bootdevice/by-name/system || local slot=$(getprop ro.boot.slot_suffix 2>/dev/null)
          addToLog "- Device doesn't have dynamic partitions, mounting /system$slot as ro"
          mount -o ro -t auto /dev/block/bootdevice/by-name/system"$slot" /system_root
        fi
      fi
    ;;
  esac;
  if is_mounted /system_root; then
    SYSTEM_ROOT=true
    if [ -f /system_root/build.prop ]; then
      addToLog "- Binding /system_root as /system"
      $BB mount -o bind /system_root /system;
    else
      addToLog "- Binding /system_root/system as /system"
      $BB mount -o bind /system_root/system /system;
    fi;
  else
    addToLog "- Could not mount /system"
    abort_while_mounting "- Could not mount /system, try changing recovery!"
  fi;
  if [ $SYSTEM_ROOT ]; then
    if [ -f /system_root/init.rc ]; then
      if [ -f /system_root/system/build.prop ]; then
        system=/system_root/system
      else
        abort_while_mounting "- /system_root/system/build.prop does not exists"
      fi
    elif [ -f /system/init.rc ]; then
      if [ -f /system/system/build.prop ]; then
        system=/system/system
      else
        abort_while_mounting "- /system/system/build.prop does not exists"
      fi
    fi
  else
    system=/system
  fi
  mount_apex;
  if [ "$dynamic_partitions" == "true" ]; then
    for block in system vendor product; do
      for slot in "" _a _b; do
        addToLog "- Executing blockdev setrw /dev/block/mapper/$block$slot"
        blockdev --setrw /dev/block/mapper/$block$slot 2>/dev/null
      done
    done
  fi
  addToLog "- Remounting /system as read write"
  mount -o rw,remount -t auto /system || mount -o rw,remount -t auto /
  addToLog "- Remounting /vendor as read write"
  mount -o rw,remount -t auto /vendor 2>/dev/null
  addToLog "- Remounting /product as read write"
  mount -o rw,remount -t auto /product 2>/dev/null
}
# Unmount all partitions on recovery clean up and for a fresh install
umount_all() {
  (umount_apex;
  if [ ! -d /postinstall/tmp ]; then
    ui_print "- Unmounting /system"
    $BB umount /system;
    $BB umount -l /system;
    if [ -e /system_root ]; then
      ui_print "- Unmounting /system_root"
      $BB umount /system_root;
      $BB umount -l /system_root;
    fi;
  fi;
  ui_print "- Unmounting /vendor"
  umount /vendor;
  umount -l /vendor;
  if [ "$UMOUNT_DATA" ]; then
    ui_print "- Unmounting /data"
    $BB umount /data;
    $BB umount -l /data;
  fi) 2>/dev/null;
}
setup_env() {
  $BOOTMODE && return 1;
  $BB mount -o bind /dev/urandom /dev/random;
  if [ -L /etc ]; then
    setup_mountpoint /etc;
    $BB cp -af /etc_link/* /etc;
    $BB sed -i 's; / ; /system_root ;' /etc/fstab;
  fi;
  ui_print " "
  ui_print "--> Unmounting partitions for fresh install"
  umount_all;
  ui_print " "
  ui_print "--> Mounting partitions"
  mount_all;
  OLD_LD_PATH=$LD_LIBRARY_PATH;
  OLD_LD_PRE=$LD_PRELOAD;
  OLD_LD_CFG=$LD_CONFIG_FILE;
  unset LD_LIBRARY_PATH LD_PRELOAD LD_CONFIG_FILE;
  if [ ! "$(getprop 2>/dev/null)" ]; then
    getprop() {
      local propdir propfile propval;
      for propdir in / /system_root /system /vendor /odm /product; do
        for propfile in default.prop build.prop; do
          test "$propval" && break 2 || propval="$(file_getprop $propdir/$propfile $1 2>/dev/null)";
        done;
      done;
      test "$propval" && echo "$propval" || echo "";
    }
  elif [ ! "$(getprop ro.build.type 2>/dev/null)" ]; then
    getprop() {
      ($(which getprop) | $BB grep "$1" | $BB cut -d[ -f3 | $BB cut -d] -f1) 2>/dev/null;
    }
  fi;
}
restore_env() {
  $BOOTMODE && return 1;
  local dir;
  unset -f getprop;
  test "$OLD_LD_PATH" && export LD_LIBRARY_PATH=$OLD_LD_PATH;
  test "$OLD_LD_PRE" && export LD_PRELOAD=$OLD_LD_PRE;
  test "$OLD_LD_CFG" && export LD_CONFIG_FILE=$OLD_LD_CFG;
  umount_all;
  test -L /etc_link && $BB rm -rf /etc/*;
  (for dir in /apex /system /system_root /etc; do
    if [ -L "${dir}_link" ]; then
      rmdir $dir;
      $BB mv -f ${dir}_link $dir;
    fi;
  done;
  $BB umount -l /dev/random) 2>/dev/null;
}

abort_while_mounting() {
  ui_print "$@";
  addToLog "Aborting while mounting $1"
  copyLogs_mounting;
  restore_env;
  exit 1;
}

copyLogs_mounting() {
  cp -f "$recoveryLog" "$logDir/recovery.log"
  cp -f "$nikGappsLog" "$logDir/NikGapps.log"
  cp -f "$system/build.prop" "$logDir/build.prop"
  cp -f "/sdcard/NikGapps/debloater.config" "$logDir/debloater.config"
  cp -f "/sdcard/NikGapps/nikgapps.config" "$logDir/nikgapps.config"
  cd $logDir
  rm -rf $nikGappsDir/logs
  tar -cz -f "/tmp/$nikGappsLogFile" *
  mkdir -p "$nikGappsDir"/logs
  cp -f /tmp/"$nikGappsLogFile" $nikGappsDir/logs/"$nikGappsLogFile"
  cd /
}