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

# 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

TMPDIR=/dev/tmp
INSTALLER=$TMPDIR/install
MAGISKBIN=/data/adb/magisk

# Default permissions
umask 022

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

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

require_new_magisk() {
  ui_print "*******************************"
  ui_print " Please install Magisk v16.0+! "
  ui_print "*******************************"
  exit 1
}

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

OUTFD=$2
ZIP=$3

mount /data 2>/dev/null

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

# We can't alter magisk image live, use alternative image if required
$BOOTMODE && IMG=/data/adb/magisk_merge.img
# Always mount under tmp
MOUNTPATH=$TMPDIR/magisk_img

# Preperation for flashable zips
get_outfd

# Mount partitions
mount_partitions

# 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

# Setup busybox and binaries
$BOOTMODE && boot_actions || recovery_actions

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

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

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

# Check the installed magisk version
MIN_VER=`grep_prop minMagisk $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 :)

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

# Get the variable reqSizeM. Use your own method to determine reqSizeM if needed
request_zip_size_check "$ZIP"

# This function will mount $IMG to $MOUNTPATH, and resize the image based on $reqSizeM
mount_magisk_img

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

# Extract files to system. Use your own method if needed
ui_print "- Extracting module files"
unzip -o "$ZIP" 'system/*' -d $MODPATH >&2

# Remove placeholder
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 /sbin/.core/img/$MODID/update
  cp -af $INSTALLER/module.prop /sbin/.core/img/$MODID/module.prop
fi
chooseport() {
  #note from chainfire @xda-developers: getevent behaves weird when piped, and busybox grep likes that even less than toolbox/toybox grep
  while (true); do
    /system/bin/getevent -lc 1 2>&1 | /system/bin/grep VOLUME | /system/bin/grep " DOWN" > $INSTALLER/events
    if (`cat $INSTALLER/events 2>/dev/null | /system/bin/grep VOLUME >/dev/null`); then
      break
    fi
  done
  if (`cat $INSTALLER/events 2>/dev/null | /system/bin/grep VOLUMEUP >/dev/null`); then
    return 0
  else
    return 1
  fi
}
FUNCTION=chooseport
 echo ""
 echo "请停用所有有关温控优化及调度的magisk模块，否则模此块可能无法在你的设备上生效"
sleep 2s
echo ""
echo "按下音量+刷入性能版温控"
echo "按下音量-刷入省电版温控"
if $FUNCTION; then
 echo ""
 echo "刷入性能版"
 bb="（性能版）"
 echo ""
 echo "手机性能不受系统温控限制"
 echo "提升Qc4.0亮屏快充温控阈值至43℃"
 rm -r /dev/tmp/magisk_img/855wk/system/jp
 mv /dev/tmp/magisk_img/855wk/system/xn /dev/tmp/magisk_img/855wk/system/vendor
 else
 echo ""
 echo "刷入省电版"
 bb="（省电版）"
 echo ""
 echo "日常使用限制手机性能（游戏无限制）"
 echo "提升Qc4.0亮屏快充温控阈值至43℃"
 rm -r /dev/tmp/magisk_img/855wk/system/xn
 mv /dev/tmp/magisk_img/855wk/system/jp /dev/tmp/magisk_img/855wk/system/vendor
fi
echo ""
sleep 2s
echo "按下音量+开启gpu超频"
echo "按下音量-跳过（关闭）"
if $FUNCTION; then
 echo ""
 echo "开启gpu超频"
 gpu="（开启gpu超频）"
 echo ""
 else
 echo ""
 echo "关闭gpu超频"
 echo ""
fi
sleep 2s
echo "刷入完成，重启生效"
module="/dev/tmp/magisk_img/855wk/module.prop"
module1="/data/adb/modules/855wk/module.prop"
echo "id=855wk
name=XiaoMi 855温控修改
version=v1.0
versionCode=1
author=磬
minMagisk=1600" > $module
echo "id=855wk
name=XiaoMi 855温控修改
version=v1.0
versionCode=1
author=磬
minMagisk=1600" > $module1
js="description=提升Qc4.0亮屏快充温控阈值至43℃"
echo $js$bb$gpu >> $module
echo $js$bb$gpu >> $module1
# 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

set_permissions

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

# Unmount magisk image and shrink if possible
unmount_magisk_img

$BOOTMODE || recovery_cleanup
rm -rf $TMPDIR

exit 0