#!/sbin/sh
##########################################################################################
#
# Magisk Module Template Install Script
# by topjohnwu
#
##########################################################################################

# Detect whether in boot mode
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true || BOOTMODE=false
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true

# This path should work in any cases
TMPDIR=/dev/tmp
MOUNTPATH=$TMPDIR/magisk_img
$BOOTMODE && IMG=/data/magisk_merge.img || IMG=/data/magisk.img
INSTALLER=$TMPDIR/install
MAGISKBIN=/data/magisk

# Default permissions
umask 022

# Initial cleanup
rm -rf $TMPDIR 2>/dev/null
mkdir -p $INSTALLER

##########################################################################################
# Environment
##########################################################################################

OUTFD=$2
ZIP=$3

# echo before loading util_functions
ui_print() { echo "$1"; }

require_new_magisk() {
  ui_print "***********************************"
  ui_print "! $MAGISKBIN isn't setup properly!"
  ui_print "! Please install Magisk v14.0+!"
  ui_print "***********************************"
  exit 1
}

mount /data 2>/dev/null

# Utility functions must exist
[ -f $MAGISKBIN/util_functions.sh ] || require_new_magisk
# Load utility fuctions
. $MAGISKBIN/util_functions.sh

get_outfd

mount_partitions

$BOOTMODE && ! is_mounted /magisk && abort "! Magisk is not activated!"
[ ! -f /system/build.prop ] && abort "! /system could not be mounted!"

# Detect version and architecture
api_level_arch_detect

# You can get the Android API version from $API, the CPU architecture from $ARCH
# Useful if you are creating Android version / platform dependent mods

# We need busybox/binaries to be setup
$BOOTMODE && boot_actions || recovery_actions

##########################################################################################
# Preparation
##########################################################################################

# Extract common files
unzip -o "$ZIP" module.prop config.sh 'common/*' -d $INSTALLER 2>/dev/null

[ ! -f $INSTALLER/config.sh ] && abort "! Unable to extract zip file!"
# Load configurations
. $INSTALLER/config.sh

# Check the min magisk version
MIN_VER=`grep_prop template $INSTALLER/module.prop`
[ ! -z $MAGISK_VER_CODE -a $MAGISK_VER_CODE -ge $MIN_VER ] || require_new_magisk
MODID=`grep_prop id $INSTALLER/module.prop`
MODPATH=$MOUNTPATH/$MODID

# Print mod name
print_modname

# Please leave this message in your flashable zip for credits :)
ui_print "******************************"
ui_print "Powered by Magisk (@topjohnwu)"
ui_print "******************************"

##########################################################################################
# Install
##########################################################################################

request_zip_size_check "$ZIP"

if [ -f "$IMG" ]; then
  ui_print "- Found $IMG"
  image_size_check $IMG
  if [ "$reqSizeM" -gt "$curFreeM" ]; then
    newSizeM=$(((reqSizeM + curUsedM) / 32 * 32 + 64))
    ui_print "- Resizing $IMG to ${newSizeM}M"
    $MAGISKBIN/magisk --resizeimg $IMG $newSizeM
  fi
else
  newSizeM=$((reqSizeM / 32 * 32 + 64));
  ui_print "- Creating $IMG with size ${newSizeM}M"
  $MAGISKBIN/magisk --createimg $IMG $newSizeM
fi

ui_print "- Mounting $IMG to $MOUNTPATH"
MAGISKLOOP=`$MAGISKBIN/magisk --mountimg $IMG $MOUNTPATH`
is_mounted $MOUNTPATH || abort "! $IMG mount failed..."

# Create mod paths
rm -rf $MODPATH 2>/dev/null
mkdir -p $MODPATH

ui_print "- Extracting module files"
unzip -o "$ZIP" 'system/*' -d $MODPATH 2>/dev/null
# Remove placeholder (people always forgot to remove it...)
rm -f $MODPATH/system/placeholder 2>/dev/null

# Handle replace folders
for TARGET in $REPLACE; do
  mktouch $MODPATH$TARGET/.replace
done

# Auto Mount
$AUTOMOUNT && touch $MODPATH/auto_mount

# prop files
$PROPFILE && cp -af $INSTALLER/common/system.prop $MODPATH/system.prop

# Module info
cp -af $INSTALLER/module.prop $MODPATH/module.prop
if $BOOTMODE; then
  # Update info for Magisk Manager
  mktouch /magisk/$MODID/update
  cp -af $INSTALLER/module.prop /magisk/$MODID/module.prop
fi

# post-fs-data mode scripts
$POSTFSDATA && cp -af $INSTALLER/common/post-fs-data.sh $MODPATH/post-fs-data.sh

# service mode scripts
$LATESTARTSERVICE && cp -af $INSTALLER/common/service.sh $MODPATH/service.sh

ui_print "- Setting permissions"
set_permissions

##########################################################################################
# Finalizing
##########################################################################################

$MAGISKBIN/magisk --umountimg $MOUNTPATH $MAGISKLOOP

# Shrink the image if possible
image_size_check $IMG
newSizeM=$((curUsedM / 32 * 32 + 64))
if [ $curSizeM -gt $newSizeM ]; then
  ui_print "- Shrinking $IMG to ${newSizeM}M"
  $MAGISKBIN/magisk --resizeimg $IMG $newSizeM
fi

$BOOTMODE || recovery_cleanup
rm -rf $TMPDIR

ui_print "- Done"
exit 0
