#!/sbin/sh
##########################################################################################
#
# Magisk Module Template Install Script
# by 酷安（@juyouxia）
#
##########################################################################################

TMPDIR=/dev/tmp
INSTALLER=$TMPDIR/install
# Always mount under tmp
MOUNTPATH=$TMPDIR/magisk_img

# 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 "  Magisk版本过低,请安装v16.6+! "
  ui_print "*******************************"
  exit 1
}

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

OUTFD=$2
ZIP=$3

mount /data 2>/dev/null

# Load utility functions
if [ -f /data/adb/magisk/util_functions.sh ]; then
  . /data/adb/magisk/util_functions.sh
elif [ -f /data/magisk/util_functions.sh ]; then
  NVBASE=/data
  . /data/magisk/util_functions.sh
else
  require_new_magisk
fi

# Use alternative image if in BOOTMODE
$BOOTMODE && IMG=/data/adb/magisk_merge.img

# Preperation for flashable zips
setup_flashable

# 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 :)
ui_print "******************************"
ui_print "Powered by Magisk (消失的烁星空)"
ui_print "******************************"

##########################################################################################
# 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

# 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

#ownhere
N="$MOUNTPATH/Theme_ownhere"
data="/data/system/theme/rights"
O="/sbin/.core/img/Theme_ownhere"
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
clean() {
    rm -rf /data/system/package_cache/1
    rm -rf /data/app/com.android.thememanager*
    rm -rf /data/user/0/com.android.thememanager
    sleep 1
}
ownhereinstall() {
echo "- *************************************"
echo "- BY:消失的烁星空"
echo "- 模块"
echo "- MIUI11主题破解"
echo "-音量键+安装模块 "
echo "- *************************************"
if $FUNCTION; then
    echo "- 开始进行安装模块..."
    chattr -i $data
    sleep 1
    rm -rf $data
    sleep 1
    mkdir -p $data
    sleep 1
    chmod 000 $data
    sleep 1
    chown root.root $data
    sleep 1
    chattr +i /data/system/theme/rights
    echo "#!/system/bin/sh" >> $N/service.sh
    echo "sleep 45" >> $N/service.sh
    echo "chown root.root /data/system/theme/rights"  >>  $N/service.sh
    echo "sleep 4" >> $N/service.sh
    echo "chmod 000 /data/system/theme/rights" >> $N/service.sh
    echo "sleep 5" >> $N/service.sh
    echo "chattr +i /data/system/theme/rights" >> $N/service.sh
    echo "sleep 45" >> $N/service.sh
    echo "chown root.root /data/system/theme/rights"  >>  $N/service.sh
    echo "sleep 4" >> $N/service.sh
    echo "chmod 000 /data/system/theme/rights" >> $N/service.sh
    echo "sleep 5" >> $N/service.sh
    echo "chattr +i /data/system/theme/rights" >> $N/service.sh
    echo "sleep 90" >> $N/service.sh
    echo "chown root.root /data/system/theme/rights"  >>  $N/service.sh
    echo "sleep 4" >> $N/service.sh
    echo "chmod 000 /data/system/theme/rights" >> $N/service.sh
    echo "sleep 5" >> $N/service.sh
    echo "chattr +i /data/system/theme/rights" >> $N/service.sh
    sleep 1
    echo "- 模块安装已完成...重启后即可使用"
    sleep 1
	clean
else
	echo "- *************************************"
    echo ""
	echo ""
	echo "- *************************************"
    if $FUNCTION; then
        clean
        rm -rf $MOUNTPATH/Theme_ownhere
        rm -rf $O/update
        echo "- 清除缓存完成,重启即可~"
	else
		clean
	    chattr -i $data
        rm -rf $data
        sleep 1
        mkdir -p $data
        sleep 1
		rm -rf $MOUNTPATH/Theme_ownhere
        rm -rf $O
		echo "- 模块已卸载,重启即可~"
	fi
fi
}
ownhereinstall
set_permissions

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

# Unmount magisk image and shrink if possible
unmount_magisk_img

$BOOTMODE || recovery_cleanup
rm -rf $TMPDIR

ui_print "- 安装完成,请重启手机"
exit 0
