#!/bin/sh -


shopt -s expand_aliases 2>>/dev/null
if which lecho 2>>/dev/null 1>>/dev/null; then
	if [ -n "${LANG}" ]; then
		alias lecho="lecho -c ${0##*/} -l ${LANG}"
	else
		alias lecho="lecho -c ${0##*/}"
	fi
else
	lecho(){
		read _incoming
		echo "${_incoming}"
	}
fi
if ! id -u | grep -q '0'; then
	echo 'Please run "${0##*/}" as root, or use "sudo ${0##*/}"' | lecho | sed "s#\${0\#\#\*/}#${0##*/}#g"
	echo 'Unable to use "$(whoami)" users to achieve the expected goals' | lecho | sed "s#\$(whoami)#$(whoami)#"
	exit 1
fi

_check_command(){
	if which apfs_snapshot >>/dev/null; then
		_command='apfs_snapshot'
	fi
	if which snaputil >>/dev/null; then
		_command='snaputil'
	fi
	if which snappy >>/dev/null; then
		_command='snappy'
	fi
	if [ -z "${_command}" ]; then
		echo 'Please install "apfs_snapshop" or "snappy" or "snaputil"' | lecho
		exit 3
	fi
	if ! which rsync >>/dev/null; then
		echo 'Please install "rsync"' | lecho
		exit 3
	fi
}

_usage(){
	echo "$(echo 'Usage: ft [OPTIONS...]' | lecho)
	$(echo '-h	Print this help' | lecho)
	$(echo '-c	Clean caches' | lecho)
	$(echo '-r	Restore Fonts' | lecho)"
}

_get_orig_snaoshot_name(){
	_orig_snapshot="$(ioreg -p IODeviceTree -l | grep boot-manifest-hash)"
	if [ -n "${_orig_snapshot}" ]; then
		_orig_snapshot="${_orig_snapshot#*<}"
		_orig_snapshot="${_orig_snapshot%>*}"
		_orig_snapshot=$(echo ${_orig_snapshot} | tr 'a-z' 'A-Z')
		_orig_snapshot="com.apple.os.update-${_orig_snapshot}"
	fi
}

_check(){
	_get_orig_snaoshot_name
	if [ -n "$(mount | grep "${_orig_snapshot}@/dev/disk0s1s1 on ")" ]; then
		i='1'
		while [ 1 -le 2 ]; do
			if umount -f "${_orig_snapshot}@/dev/disk0s1s1"; then
				break
			fi
			if [ "${i}" = '100' ]; then
				break
			fi
			i=$((i+1))
		done
		unset i
	fi
	mkdir -p "/mnt2/"
}

_mount_snapshot(){
	case "${_command}" in
		apfs_snapshot)
			orig_snapshot=$(apfs_snapshot -l / | sed -n 1p)
		;;
		snaputil)
			orig_snapshot=$(snaputil -l / | sed -n 1p)
		;;
		snappy)
			orig_snapshot=$(snappy -lf / | sed -n 2p)
		;;
	esac
	if [ -z "${orig_snapshot}" ]; then
		echo 'Error: No snapshot found on current device, operation terminated.' | lecho
		exit 1
	else
		mount_apfs -s "${orig_snapshot}" "/" "/mnt2" 2>>/dev/null
		echo "[*] $(echo 'mount snapshot success' | lecho)"
	fi
}

_umount(){
	i='1'
	while [ 1 -le 2 ]; do
		if umount -f '/mnt2'; then
			break
		fi
		if [ "${i}" = '100' ]; then
			break
		fi
		i=$((i+1))
	done
	unset i
	if mount | grep -q 'on /mnt2 ('; then
		echo "[*] $(echo 'umount snapshot failure' | lecho)"
	else
		echo "[*] $(echo 'umount snapshot success' | lecho)"
	fi
	if ls /mnt2 | wc -l | grep -q '0'; then
		rm -rf "/mnt2"
	fi
}

_restore_font(){
	if [ -e "/mnt2/System/Library/Fonts/" ]; then
			echo "[*] $(echo 'Recovering' | lecho)"
		if rsync -a --delete "/mnt2/System/Library/Fonts" "/System/Library/" >>/dev/null; then
			echo "[*] $(echo 'Recovery complete' | lecho)"
		else
			echo "[*] $(echo 'Recovery exception' | lecho)"
		fi
	else
		_umount
		echo "[*] $(echo 'Error: Font file not found' | lecho)"
		exit 6
	fi
}

_clean_caches(){
	rm -rf '/var/mobile/Library/Caches/com.apple.keyboards/'
	rm -rf '/var/mobile/Library/Caches/com.apple.UIStatusBar/'
	rm -rf "/var/mobile/Library/Caches/TelephonyUI"*
	find "/var/mobile/Containers/Data/Application" -maxdepth 4 -type d -iname "TelephonyUI*" -exec rm -rf {} \;
}

_restore_run(){
	_check_command
	_check
	_mount_snapshot
	_restore_font
	_umount
	_clean_caches
}


case "${1}" in
	-h | --help)
		_usage
		exit 0
	;;
	-r)
		_restore_run
		echo 'Respring!' | lecho
		echo 'Respring!' | lecho
		echo 'Respring!' | lecho
	;;
	-R)
		_restore_run
		killall -9 backboardd
	;;
	-c)
		_clean_caches
		echo "[*] $(echo 'clean caches success' | lecho)"
	;;
	-C)
		_clean_caches
		killall -9 backboardd
		echo "[*] $(echo 'clean caches success' | lecho)"
	;;
	*)
		_usage
		exit 2
	;;
esac
