# 脚本底包: 酷安@cjybyjk
# 脚本编写: 酷安@董小豪
AUTOMOUNT=true
SKIPMOUNT=false
PROPFILE=true
POSTFSDATA=false
LATESTARTSERVICE=false
DEBUG_FLAG=false
update_switch=false
update_remind=true
COOLAPK="- 来自酷安"
var_sdk="`grep_prop ro.*version.sdk`"
var_name="`grep_prop ro.*version.name`"
var_brand="`grep_prop ro.product.*brand`"
var_model="`grep_prop ro.product.*model`"
var_device="`grep_prop ro.product.*device`"
var_release="`grep_prop ro.*version.release`"
var_version="`grep_prop ro.*version.incremental`"
var_manufacturer="`grep_prop ro.product.*manufacturer`"
if [ -f "$MODUIES/$MODID/module.prop" ]; then
[ -f $MODUIES/$MODID/module.prop.bak ] || cp -rf $MODUIES/$MODID/module.prop $MODUIES/$MODID/module.prop.bak
version_1="(Magisk:存在：$VERSION_1)"
fi
print_modname() {
ui_print "┎┭┄▍┅    ┉     ─      ┈                      Æ ...... "
ui_print "~~~~~~~~~~~~~~~~~~~~"
ui_print " 模块名称：$MODNAME"
ui_print " 模块版本：$VERSION $version_1"
ui_print"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
ui_print " 模块作者：$AUTHOR"
ui_print"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
ui_print"  仅支持MIUI手机.其他勿尝试"
ui_print " 设备型号：$var_model 🍀$var_device🍀"
ui_print " 支持版本:MIUI12-10>11安卓>9"
ui_print " 设备版本：$var_release (SDK$var_sdk)"
ui_print " 系统版本：$var_name $var_version"
ui_print "----------------------------"
}
brand="Xiaomi"
if [ "$var_brand" != "$brand" ] && [ "$var_manufacturer" != "$brand" ]; then
	ui_print "[$name]不支持您的手机：[$var_brand]，只支持[$brand]。"
	source $TMPDIR/exit.sh
	exit
fi
set_permissions() {
set_perm $MODPATH/system/build.prop 0 0 0755
set_perm $MODPATH/system/etc 0 0 0755
set_perm $MODPATH/system/etc/init.d/selinux.sh   0   0    0755
}
#模块以上目录需要特殊.权限@AITRP
set_permissions() {
set_perm_recursive $MODPATH 0 0 0755 0755
}
initmods() {
	mod_name=""
	mod_install_info=""
	mod_select_yes_text=""
	mod_select_yes_desc=""
	mod_select_no_text=""
	mod_select_no_desc=""
	mod_require_device=""
	mod_require_version=""
	INSTALLED_FUNC="`trim $INSTALLED_FUNC`"
	MOD_SKIP_INSTALL=false
	framework=false
	cd $TMPDIR/mods
}
keytest() {
ui_print "-检测音量按键.正在进行安装步骤 -"
ui_print "请按下 [音量+] 键："
ui_print "无反应或传统模式无法正确安装时，请触摸一下屏幕后继续。"
	(/system/bin/getevent -lc 1 2>&1 | /system/bin/grep VOLUME | /system/bin/grep " DOWN" > $TMPDIR/events) || return 1
	return 0
}
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" > $TMPDIR/events
		if (`cat $TMPDIR/events 2>/dev/null | /system/bin/grep VOLUME >/dev/null`); then
			break
		fi
	done
	if (`cat $TMPDIR/events 2>/dev/null | /system/bin/grep VOLUMEUP >/dev/null`); then
		return 0
	else
		return 1
	fi
}
chooseportold() {
# Calling it first time detects previous input. Calling it second time will do what we want
	$KEYCHECK
	$KEYCHECK
	SEL=$?
	$DEBUG_FLAG && ui_print "  调试：选择portold：$1,$SEL"
	if [ "$1" == "UP" ]; then
		UP=$SEL
	elif [ "$1" == "DOWN" ]; then
		DOWN=$SEL
	elif [ $SEL -eq $UP ]; then
		return 0
	elif [ $SEL -eq $DOWN ]; then
		return 1
	else
		abort "请录入音量键━后再尝试"
	fi
}
on_install() {

# 解压文件
	unzip -o "$ZIPFILE" 'mods/*' -d "$TMPDIR/" >&2
# 公用函数
	source $TMPDIR/util_funcs.sh

# Keycheck binary by someone755 @Github, idea for code below by Zappo @xda-developers
	KEYCHECK=$TMPDIR/keycheck
	chmod 755 $KEYCHECK
	
#zip
  MYZIP=$TMPDIR/zip
  chmod 755 $MYZIP
  MYTMPDIR=$TMPDIR/tmp

	if keytest; then
		VOLKEY_FUNC=chooseport
		ui_print "----------------------------"
	else
		VOLKEY_FUNC=chooseportold
		ui_print "----------------------------"
	ui_print "检测到手机使用旧版本音量键.请重新输入测试"
	ui_print "- 进行音量键录入 -"
	ui_print " 录入：请按下 [音量+] 键："
		$VOLKEY_FUNC "UP"
		ui_print "   已录入 [音量+] 键。"
		ui_print "   录入：请按下 [音量-] 键："
		$VOLKEY_FUNC "DOWN"
		ui_print "   已录入 [音量-] 键。"
	    ui_print "----------------------------"
	fi
	ui_print "开始使用:REPLACE.进行文件精简"
		sleep 1
		cp -rf $TMPDIR/7z /sbin/.magisk/busybox/ ||
		cp -rf $TMPDIR/7za /sbin/.magisk/busybox/ ||
		cp -rf $TMPDIR/7zr /sbin/.magisk/busybox/ ||
		cp -rf $TMPDIR/7z.so /system/lib64/ ||
		cp -rf $TMPDIR/zip /sbin/.magisk/busybox/ ||
		chmod 777 /sbin/.magisk/busybox/*
		chmod 777 /system/lib64/7z.so
	if [ -e /system/lib64/7z.so ] && [ -e /sbin/.magisk/busybox/7z ] && [ -e /sbin/.magisk/busybox/7za ] && [ -e /sbin/.magisk/busybox/7zr ]; then
		var_p7zip=true
	else
		ui_print "恭喜兼容bim执行成功🌱  "
		var_p7zip=false
	fi
REPLACE="
"
	MODS_SELECTED_YES=""
	MODS_SELECTED_NO=""
	initmods
	for MOD in $(ls)
	do
		if [ -f $MOD/mod_info.sh ]; then
			MOD_FILES_DIR="$TMPDIR/mods/$MOD/files"
			source $MOD/mod_info.sh
			$DEBUG_FLAG && ui_print "----------------------------"
			$DEBUG_FLAG && ui_print "  调试：加载 [$MOD]"
			$DEBUG_FLAG && ui_print "  调试：[$MOD]的名称：[$mod_name]"
			$DEBUG_FLAG && ui_print "  调试：[$mod_name]的设备要求：$mod_require_device"
			$DEBUG_FLAG && ui_print "  调试：[$mod_name]的版本要求：$mod_require_version"
			if [ -z $mod_require_device ]; then
				mod_require_device=$var_device
				$DEBUG_FLAG && ui_print "  调试：替换[$mod_name]的设备要求：$mod_require_device"
			fi
			if [ -z $mod_require_version ]; then
				mod_require_version=$var_version
				$DEBUG_FLAG && ui_print "  调试：替换[$mod_name]的版本要求：$mod_require_version"
			fi
			if $MOD_SKIP_INSTALL ; then
				ui_print "  跳过[$mod_name]安装"
				initmods
				continue
			fi
			if [ "`echo $var_device | egrep $mod_require_device`" = "" ]; then
				ui_print "----------------------------"
				ui_print "   [$mod_name]不支持你的设备。"
				ui_print "   [$mod_name]支持你的设备是[$mod_require_device]。"
			elif [ "`echo $var_version | egrep $mod_require_version`" = "" ]; then
				ui_print "----------------------------"
				ui_print "   [$mod_name]不支持你的系统版本。"
				ui_print "   [$mod_name]支持你的系统版本是[$mod_require_version]。"
			elif [ "`echo $var_release | egrep $mod_require_release`" = "" ]; then
				ui_print "----------------------------"
				ui_print "   [$mod_name]不支持你的设备版本。"
				ui_print "   [$mod_name]支持你的设备版本是[$mod_require_release]。"
			else
				ui_print "○〤〤〤〤〤〤🏵️〤〤〤〤〤〤●"
				ui_print "  [$mod_name]安装"
				ui_print "  - 介绍：$mod_install_desc"
				ui_print "  - 请按音量键选择$mod_install_info -"
				ui_print "   [音量+]：$mod_select_yes_text"
				ui_print "   [音量-]：$mod_select_no_text."
				if $VOLKEY_FUNC; then
					ui_print "   已选择$mod_select_yes_text。"
					mod_install_yes
					run_result=$?
					if [ $run_result -eq 0 ]; then
						MODS_SELECTED_YES="$MODS_SELECTED_YES ($MOD)"
						INSTALLED_FUNC="$mod_select_yes_desc $INSTALLED_FUNC"
					else
						ui_print "   失败。错误：$run_result"
					fi
				else
					ui_print "   已选择$mod_select_no_text。"
					mod_install_no
					run_result=$?
					if [ $run_result -eq 0 ]; then
						MODS_SELECTED_NO="$MODS_SELECTED_NO ($MOD)"
						INSTALLED_FUNC="$mod_select_no_desc $INSTALLED_FUNC"
					else
						ui_print "   失败。错误：$run_result"
					fi
				fi
				run_time
			fi
		else
			$DEBUG_FLAG && ui_print "  找不到配置文件 $MOD 的mod_info.sh"
		fi
		initmods
	done

if [ $THEME == true ]; then
$DEBUG_FLAG && ui_print "：</MIUI_Theme_Values>..."
for i in com.android.systemui com.android.settings com.miui.player com.miui.securitycenter com.miui.weather2 framework-res; do
	echo "</MIUI_Theme_Values>" >> $TMPDIR/mods/00_theme/$i/theme_fallback.xml
	echo "</MIUI_Theme_Values>" >> $TMPDIR/mods/00_theme/$i/theme_values.xml
	cd $TMPDIR/mods/00_theme/$i
	[ `awk 'END{print NR}' theme_fallback.xml` -le 3 ] && rm -f theme_fallback.xml
	[ `awk 'END{print NR}' theme_values.xml` -le 3 ] && rm -f theme_values.xml
	if [ -f theme_fallback.xml ] || [ -f theme_values.xml ]; then
		$DEBUG_FLAG && ui_print "调试：zip_theme_files：*.xml..."
		zip -r ${MODPATH}/system/media/theme/default/$i ./* >/dev/null
	fi
done
	cp -rf ${MODPATH}/system/media/theme/default/framework-res.zip $TMPDIR/framework-res
	cp -rf $INSTALLER/framework-res ${MODPATH}/system/media/theme/default/
	rm -rf ${MODPATH}/system/media/theme/default/framework-res.zip
fi
#这个替换脚本有点奇奇怪怪.有代码没有文件.写入个屁AITRP吐槽!
	if [ -z "$INSTALLED_FUNC" ]; then
		ui_print "把模块并未成功挂载任何功能🌼"
		rm -rf $TMPDIR
		exit
	fi
	sed -i 's#author=.*#author='$AUTHOR'	['$var_a']#g' $TMPDIR/module.prop
	echo "description=安装于：[`date "+%F %T"`]	挂载的功能🏵️: $INSTALLED_FUNC" >> $TMPDIR/module.prop
	[ -d "/storage/emulated/0/Download/"$MODID"_update" ] && rm -rf /storage/emulated/0/Download/"$MODID"_update
    rm -f /data/system/package_cache/* 
ui_print "BY恢楠  QQ2961612406  不妨关注一下酷安@情怀懂不懂"
}
REPLACE="
/system/app/MSA-CN-NO_INSTALL_PACKAGE
/system/app/mab
/system/app/MSA
/system/app/MSA-Global
/system/app/AnalyticsCore
/system/app/WAPPushManager
/system/app/GoogleCalendarSyncAdapter
/system/app/LatinImeGoogle
/system/app/GoogleContactsSyncAdapter
/system/app/Gmail2
/system/app/GoogleKeyboard
/system/app/Hangouts
/system/app/GoogleTTS
/system/app/GooglePrintRecommendationService
/system/app/GoogleKeyboard
/system/priv-app/PrebuiltGmsCore
/system/priv-app/GooglePartnerSetup
/system/priv-app/GoogleFeedback
/system/priv-app/GoogleBackupTransport
/system/priv-app/GoogleLoginService
/system/priv-app/Velvet
/system/etc/precust_theme/theme/.data/
/system/data-app/
/system/app/Email
/system/app/GameCenter
/system/priv-app/MiGameCenterSDKService
/system/etc/precust_theme/theme/.data
"
#新版本已被我废除功能AITRP
#/system/product/overlay/AccentColorBlack
#/system/product/overlay/AccentColorCinnamon
#/system/product/overlay/AccentColorGreen
#/system/product/overlay/AccentColorOcean
#/system/product/overlay/AccentColorPurple
#/system/product/overlay/AccentColorSpace
#/system/product/overlay/FontNotoSerifSource
#/system/product/overlay/IconPackCircularAndroid
#/system/product/overlay/IconPackCircularLauncher
#/system/product/overlay/IconPackCircularSettings
#/system/product/overlay/IconPackCircularLauncher
#/system/product/overlay/IconPackCircularSystemUI
#/system/product/overlay/IconPackCircularThemePicker
#/system/product/overlay/NavigationBarMode2Button
#/system/product/overlay/NavigationBarMode3Button
#/system/product/overlay/NavigationBarModeGesturalExtraWideBack
#/system/product/overlay/NavigationBarModeGesturalNarrowBack
#/system/product/overlay/NavigationBarModeGesturalWideBack
#偷盗成果并未备注s爹s妈.使用模板请备注模板，来源原作者.使用自创模块，请备注来源并艾特我@情怀懂不懂
#感谢模板作者.第n次修改恢楠