#!/system/bin/sh
# Script by XTC-ThemePro - @baiyao105

MODDIR=${0%/*}
gethitokoto() {
	hitokoto_file="${MODDIR}/Sundry/hitokoto"
	if [ -f "$hitokoto_file" ]; then
		all_texts=$(sed -n 's/.*text:\[\(.*\)\]/\1/p' "$hitokoto_file" | sed 's/","/"\n"/g' | sed 's/^"//;s/"$//')
		total_count=$(echo "$all_texts" | wc -l)
		if [ "$total_count" -gt 0 ]; then
			random_num=$(($(date +%s) % total_count + 1))
			best_text=$(echo "$all_texts" | sed -n "${random_num}p")
			echo "${best_text}"
		else
			echo "唔?"
		fi
	else
		echo "唔?文件未找到...(?"
	fi
}

getinstall() {
	if [ -n "$1" ] && [ "$1" = "true" ]; then
		touch "${MODDIR}/Sundry/install"
	fi
	install_file="${MODDIR}/Sundry/install"
	if [ -f "$install_file" ]; then
		echo "true"
	else
		echo "false"
	fi
}

getdevice() {
	bindnumber=$(getprop ro.boot.bindnumber)
	chipid=$(getprop ro.boot.xtc.chipid)
	model=$(_grep_prop ro.product.innermodel)
	serverinner=$(getprop persist.sys.serverinner "${model}")
	if [ -z "$serverinner" ]; then
		serverinner="${model}"
	fi
	if [ -z "$chipid" ]; then
		echo "Chipid获取失败"
		return 1
	fi
	Hwmac=$(cat /sys/class/net/wlan0/address)
	input_string="${bindnumber}${serverinner}${chipid}${Hwmac}"
	hash=$(printf "%s" "$input_string" | sha256sum | awk '{print $1}')
	Ostring=$(echo "$hash" | cut -c1-8)
	current_versions=$(dumpsys package com.xtc.theme | grep versionCode | awk '{print $1}' | sed 's/versionCode=//' | sort -nr)
	current_versionNames=$(dumpsys package com.xtc.theme | grep versionName | awk '{print $1}' | sed 's/versionName=//' | sort -nr)
	max_version=$(echo "$current_versions" | head -n 1)
	max_versionName=$(echo "$current_versionNames" | head -n 1)
	factory_version=$(echo "$current_versions" | sed -n '2p')
	factory_versionName=$(echo "$current_versionNames" | sed -n '2p')

	crontab_time=$(cat "${MODDIR}/crontab_time" 2>/dev/null || echo "未设置")
	best_text=$(gethitokoto)

	module_version=$(_grep_prop version "${MODDIR}/module.prop")
	module_code=$(_grep_prop versionCode "${MODDIR}/module.prop")

	#config_file="${MODDIR}/Sundry/config.conf"
	#if [ -f "$config_file" ]; then
	#    log_enabled=$(grep -v '^#' "$config_file" | grep "^log=" | cut -f2 -d '=')
	#    log_path=$(grep -v '^#' "$config_file" | grep "^log_path=" | cut -f2 -d '=')
	#fi
	echo "================================================================="
	echo "XTC-ThemePro @baiyao105 - Debug信息"
	echo "${best_text}"
	echo "设备型号: ${serverinner}"
	echo "绑定号: ${bindnumber}"
	echo "ChipID: ${chipid}"
	echo "设备标识: ${Ostring}"
	echo "Crontab时间: ${crontab_time}"
	echo "模块版本: ${module_version} (${module_code})"
	echo "个性主题版本: ${max_versionName} (${max_version})"
	if [ -n "$factory_version" ] && [ "$factory_version" != "$max_version" ]; then
		echo "个性主题版本(系统内置): ${factory_versionName} (${factory_version})"
	fi
	echo "================================================================="
}

prop() {
	prop_value=$(_grep_prop "$1")
	if [ -n "$prop_value" ]; then
		echo "${prop_value}"
	else
		echo "属性未找到: $1"
		return 1
	fi
}

_grep_prop() {
	REGEX="s/$1=//p"
	shift
	FILES="$*"
	[ -z "$FILES" ] && FILES="/system/build.prop /vendor/build.prop /product/build.prop"
	sed -n "$REGEX" "$FILES" 2>/dev/null | head -n 1
}

case "$1" in
"gethitokoto")
	gethitokoto
	;;
"getdevice")
	getdevice
	;;
"getinstall")
	getinstall "$2"
	;;
"prop")
	if [ -n "$2" ]; then
		prop "$2"
	else
		echo "Usage: themepro prop <property_name>"
		exit 1
	fi
	;;
*)
	echo "Usage: themepro <command>"
	echo "Commands:"
	echo "  gethitokoto - 获取一言"
	echo "  prop <property_name> - 获取系统属性"
	exit 1
	;;
esac
