#!/sbin/sh
# Shell Script EDIFY Replacement

#Variables
OUTFD=/proc/self/fd/$2;
ZIPFILE="$3";
COMMONDIR=/tmp/AFZCScripts;
TMPDIR=/dev/tmp
NVBASE=/data/adb
MAGISKBIN=$NVBASE/magisk
nikGappsLog="/tmp/NikGapps.log"

# 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

addToLog() {
  echo "$1" >>$nikGappsLog
}

ui_print() {
  until [ ! "$1" ]; do
    echo "ui_print $1
      ui_print" >> "$OUTFD";
      addToLog "$1"
    shift;
  done;
}

# package_extract_file <file> <destination_file>
package_extract_file() { mkdir -p "$(dirname "$2")"; unzip -o "$ZIPFILE" "$1" -p > "$2"; }

# package_extract_dir <dir> <destination_dir>
package_extract_dir() {
  local entry outfile;
  for entry in $(unzip -l "$ZIPFILE" "$1/*" 2>/dev/null | tail -n+4 | grep -v '/$' | grep -o " $1.*$" | cut -c2-); do
    outfile="$(echo "$entry" | sed "s|${1}|${2}|")";
    mkdir -p "$(dirname "$outfile")";
    unzip -o "$ZIPFILE" "$entry" -p > "$outfile";
  done;
}
#-----------------------------------Execution Starts---------------------------------#

package_extract_file "afzc/afzc" "$COMMONDIR/afzc.sh"
package_extract_file "afzc/ak3mount" "$COMMONDIR/ak3mount.sh"
package_extract_file "afzc/addon" "$COMMONDIR/addon"
chmod 755 "$COMMONDIR/afzc.sh";
chmod 755 "$COMMONDIR/addon";

if [ ! -f $COMMONDIR/afzc.sh ]; then
	echo "! Unable to extract zip file!"
	exit 1
fi

if [ ! -f $COMMONDIR/ak3mount.sh ]; then
	echo "! Unable to extract zip file!"
	exit 1
fi

. $COMMONDIR/afzc.sh "$2" "$3"
. $COMMONDIR/ak3mount.sh "$1" "$2" "$3"
show_progress 1.00 0

ensure_bb_1() {
  nikGappsLogo
  ui_print "--> Setting up Environment"
  if [ -x "$MAGISKTMP"/busybox/busybox ]; then
    bb=$MAGISKTMP/busybox/busybox
    [ -z "$BBDIR" ] && BBDIR=$MAGISKTMP/busybox
  elif [ -x $TMPDIR/bin/busybox ]; then
    bb=$TMPDIR/bin/busybox
    [ -z "$BBDIR" ] && BBDIR=$TMPDIR/bin
  else
    # Construct the PATH
    [ -z $BBDIR ] && BBDIR=$TMPDIR/bin
    mkdir -p $BBDIR
    ln -s $MAGISKBIN/busybox $BBDIR/busybox
    $MAGISKBIN/busybox --install -s $BBDIR
    bb=$MAGISKBIN/busybox
  fi
  ui_print "- Busybox found in $bb"
  echo "$PATH" | grep -q "^$BBDIR" || export PATH=$BBDIR:$PATH
}

ensure_bb() {
  if set -o | grep -q standalone; then
    addToLog "We are in busybox ash"
    # We are definitely in busybox ash
    set -o standalone
    return
  fi
  nikGappsLogo
  ui_print "--> Setting up Environment"
  # Find our busybox binary
#  local bb
  if [ -f $TMPDIR/busybox ]; then
    bb=$TMPDIR/busybox
  elif [ -f "$MAGISKBIN"/busybox ]; then
    bb=$MAGISKBIN/busybox
  else
    abort "! Cannot find BusyBox"
  fi
  ui_print "- Busybox found in $bb"
  chmod 755 "$bb"
  # Find our current arguments
  # Run in busybox environment to ensure consistent results
  # /proc/<pid>/cmdline shall be <interpreter> <script> <arguments...>
  local cmds=$($bb sh -o standalone -c "
  for arg in \$(tr '\0' '\n' < /proc/$$/cmdline); do
    if [ -z \"\$cmds\" ]; then
      # Skip the first argument as we want to change the interpreter
      cmds=\"sh -o standalone\"
    else
      cmds=\"\$cmds '\$arg'\"
    fi
  done
  echo \$cmds")

  # Re-exec our script
  echo "$cmds" | $bb xargs "$bb"
  exit
}

setup_flashable() {
  ensure_bb_1
  $BOOTMODE && return
  if [ -z "$OUTFD" ] || readlink /proc/$$/fd/"$OUTFD" | grep -q /tmp; then
    # We will have to manually find out OUTFD
    for FD in `ls /proc/$$/fd`; do
      if readlink /proc/$$/fd/$FD | grep -q pipe; then
        if ps | grep -v grep | grep -qE " 3 $FD |status_fd=$FD"; then
          OUTFD=$FD
          break
        fi
      fi
    done
  fi
}


nikGappsLogo() {
  $BOOTMODE && return
  ui_print " "
  ui_print "------------------------------------------"
  ui_print "*   * * *  * *****   *   ***** ***** *****"
  ui_print "**  * * * *  *      * *  *   * *   * *    "
  ui_print "* * * * **   * *** *   * ***** ***** *****"
  ui_print "*  ** * * *  *   * ***** *     *         *"
  ui_print "*   * * *  * ***** *   * *     *     *****"
  ui_print " "
  ui_print "-->     Created by Nikhil Menghani     <--"
  ui_print "------------------------------------------"
  ui_print " "
}

setup_flashable
SetupTools

InstallPackage "$ZIPFILE" "ExtraFiles"
set_progress 0.15
InstallPackage "$ZIPFILE" "GooglePlayStore"
set_progress 0.3
InstallPackage "$ZIPFILE" "GmsCore"
set_progress 0.45
InstallPackage "$ZIPFILE" "GoogleServicesFramework"
set_progress 0.6
InstallPackage "$ZIPFILE" "GoogleContactsSyncAdapter"
set_progress 0.75
InstallPackage "$ZIPFILE" "GoogleCalendarSyncAdapter"
set_progress 0.9

set_progress 1.00

exitInstall

