#!/bin/bash

#
# This script is refer to Pavo's work as follow.                                        by crazybirdy. v1.20180807
#
# found patch code of com.apple.telemetry.plugin is credit to Pavo.
# https://www.insanelymac.com/forum/topic/334596-pre-release-macos-mojave/?do=findComment&comment=2624931
# https://www.insanelymac.com/forum/topic/334898-pre-release-macos-mojave/?do=findComment&comment=2624961
#
# found UserEventPlugins is credit to ASentientBot at post#1683.
# https://forums.macrumors.com/threads/macos-10-14-mojave-on-unsupported-macs-thread.2121473/page-68#post-26222082
#
# found com.apple.telemetry.plugin is credit to jackluke at post#1761.
# https://forums.macrumors.com/threads/macos-10-14-mojave-on-unsupported-macs-thread.2121473/page-71#post-26222642
#
printred() {
  printf "\e[1;31m${1}\e[0m"
}
printgreen() {
  printf "\e[1;32m${1}\e[0m"
}
printyellow() {
  printf "\e[1;33m${1}\e[0m"
}
printblue() {
  printf "\e[1;34m${1}\e[0m"
}
printpurple() {
  printf "\e[1;35m${1}\e[0m"
}
printLatest() {
  printf "\e[1;31m${1}\e[0m"
}
printf '\033[8;50;120t'
if [[ -f "/usr/bin/clear" ]]; then
	clear
fi

function telemetrypatch() {
	echo
	echo "Patch Mojave telemetry for Core2 Duo CPU with only SSE4.1, without SSE4.2, like as CPU E8400."

	cputype=`sysctl -a | grep cpu.brand_string | sed 's/machdep.cpu.brand_string: //' `
	cpusse=`sysctl -a | grep cpu.features | sed 's/machdep.cpu.features: //' `
	cpusse41=`sysctl -a | grep cpu.features | sed 's/machdep.cpu.features: //' | grep SSE4.1 `
	cpusse42=`sysctl -a | grep cpu.features | sed 's/machdep.cpu.features: //' | grep SSE4.2 `
	echo
	printblue "${cputype}"
	echo
	printred "${cpusse}"
	echo
	if [[ "${cpusse41}" == "" ]]; then
		echo
		echo "Your CPU isn't supported for SSE4.1, exit!"
		echo
		exit
	elif [[ "${cpusse42}" == "" ]]; then
		echo
		echo "Your CPU isn't supported SSE4.2, need to patch telemetry for Mojave."
		echo
	elif [[ ! "${cpusse42}" == "" ]]; then
		echo
		echo "Your CPU is supported SSE4.2, exit!"
		echo
		exit
	fi

	echo "Available target volumes:"
	echo
	cd /Volumes
	AllVolumes=(*)
	indexA=0
	index=0
	for volume in "${AllVolumes[@]}"
	do
		targetforVolume="/Volumes/$volume"
		if [[ -f "$targetforVolume/System/Library/CoreServices/SystemVersion.plist" ]]; then
			ProductBuildVersion=`cat "$targetforVolume/System/Library/CoreServices/SystemVersion.plist" | grep -A 1 "ProductBuildVersion" | grep string | sed 's/<string>//' | sed 's/<\/string>//' | awk '{print $1;}' `
			ProductVersion=`cat "$targetforVolume/System/Library/CoreServices/SystemVersion.plist" | grep -A 1 "ProductVersion" | grep string | sed 's/<string>//' | sed 's/<\/string>//' | awk '{print $1;}' `
			SelectVersion=`echo $ProductVersion | sed 's/.\{5\}/& /g' | awk '{print $1;}' `
			if [[ $SelectVersion > 10.13 ]]; then
				if [[ -f "$targetforVolume/System/Library/UserEventPlugins/com.apple.telemetry.plugin/Contents/MacOS/com.apple.telemetry" ]]; then
					AtlistVolume[$indexA]="$targetforVolume"
					OSVersion[$indexA]="$ProductVersion"
					OSBuildVersion[$indexA]="$ProductBuildVersion"					
					echo "[ $indexA ] ${AtlistVolume[$indexA]} ......... $ProductVersion $ProductBuildVersion"
					let indexA++
				fi
			fi
		fi
		let index++
	done

	echo 
	read -p "Select the com.apple.telemetry partition: " VolumeNumber
	if [[ ! ${AtlistVolume[$VolumeNumber]} ]]; then
		echo
		echo "Error!!!"
		echo
		echo "Please select right target volume to update."
		echo
		exit 0
	fi

	targetVolume="${AtlistVolume[$VolumeNumber]}"

		echo "                                         [ $VolumeNumber ] $targetVolume ......... ${OSVersion[$VolumeNumber]} ${OSBuildVersion[$VolumeNumber]}"
		echo

		read -p "The target volume is selected, is it right? (y/n) " targetyesno
		case "$targetyesno" in
			y|Y)
				echo
			;;
			*)
				echo
				echo "Error!!!"
				echo
				echo "The selected target volume is not right."
				echo
				exit 0
			;;
		esac

	### 
	### 參考資料
	### https://www.insanelymac.com/forum/topic/334596-pre-release-macos-mojave/?do=findComment&comment=2625312
	### https://forums.macrumors.com/threads/macos-10-14-mojave-on-unsupported-macs-thread.2121473/page-84#post-26229516
	### https://www.insanelymac.com/forum/topic/334596-pre-release-macos-mojave/?do=findComment&comment=2625327
	### http://dosdude1.com/mojave/
	### https://forums.macrumors.com/threads/macos-10-14-mojave-on-unsupported-macs-thread.2121473/page-85#post-26230707
	### https://forums.macrumors.com/threads/macos-10-14-mojave-on-unsupported-macs-thread.2121473/page-68#post-26222082
	### https://www.insanelymac.com/forum/topic/334596-pre-release-macos-mojave/?do=findComment&comment=2624833
	### https://www.insanelymac.com/forum/topic/334596-pre-release-macos-mojave/?do=findComment&comment=2624866
	### 
	### https://www.insanelymac.com/forum/topic/334596-pre-release-macos-mojave/?do=findComment&comment=2624931
	### The only difference I see in the binary HS: C806000085 Mojave: 2807000085
	###
	### Find
	### 2807000085
	### Replace
	### C806000085
	###

		echo "Patch Mojave telemetry for Core2 Duo CPU with only SSE4.1, without SSE4.2, like as CPU E8400."
		echo
		echo "${targetVolume}/System/Library/UserEventPlugins/com.apple.telemetry.plugin/Contents/MacOS/com.apple.telemetry"
		echo

		telemetrypluginbin="${targetVolume}/System/Library/UserEventPlugins/com.apple.telemetry.plugin/Contents/MacOS/com.apple.telemetry"

		cp "${telemetrypluginbin}" "${telemetrypluginbin}-org"

	### 10.14 db1 18A293u ~ db5 18A347e
	###
	### Find
	### 28 07 00 00 85 
	### Replace
	### C8 06 00 00 85
	###
		#perl -pi -e "s|\x28\x07\x00\x00\x85|\xC8\x06\x00\x00\x85|g" "${telemetrypluginbin}"

	### 10.14 db6 18A353d ~
	###
	### Find
	### D8 06 00 00 85 
	### Replace
	### C8 06 00 00 85
	###
		#perl -pi -e "s|\xD8\x06\x00\x00\x85|\xC8\x06\x00\x00\x85|g" "${telemetrypluginbin}"

    ### common method ###
	### Find
	### 10 00 00 00 D8 06 00 00 85 00 00 00 
	### Replace
	### 10 00 00 00 C8 06 00 00 85 00 00 00 
	###

	odbin="/usr/bin/od"
	diffbin="/usr/bin/diff"
	if [[ ! -f "/usr/bin/od" ]]; then
		odbin="${targetVolume}/usr/bin/od"
		diffbin="${targetVolume}/usr/bin/diff"
	fi

		code1=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $1;}' `
		code2=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $2;}' `
		code3=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $3;}' `
		code4=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $4;}' `
		code5=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $5;}' `
		code6=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $6;}' `
		code7=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $7;}' `
		code8=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $8;}' `
		code9=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $9;}' `
		code10=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $10;}' `
		code11=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $11;}' `
		code12=`${odbin} -An -tx1 -v "${telemetrypluginbin}" | awk NR==2 | awk '{print $12;}' `

		echo "Find ...... $code1 $code2 $code3 $code4 $code5 $code6 $code7 $code8 $code9 $code10 $code11 $code12"
		echo "Replace ... $code1 $code2 $code3 $code4 c8 06 $code7 $code8 $code9 $code10 $code11 $code12"
		
		perl -pi -e "s|\x$code1\x$code2\x$code3\x$code4\x$code5\x$code6\x$code7\x$code8\x$code9\x$code10\x$code11\x$code12|\x$code1\x$code2\x$code3\x$code4\xC8\x06\x$code7\x$code8\x$code9\x$code10\x$code11\x$code12|g" "${telemetrypluginbin}"

    ### common method ###
    
		echo
		echo "Patching diff:"
		echo
		${diffbin} <(${odbin} -An -tx1 -v "${telemetrypluginbin}-org") <(${odbin} -An -tx1 -v "${telemetrypluginbin}")
		echo
		echo "Auto patch is done."
		echo

		rm -f "${telemetrypluginbin}-org"

		if [[ ! -f "${targetVolume}/usr/bin/mojave2core" ]]; then
			cp -fv "$0" "${targetVolume}/usr/bin/mojave2core"
			echo
			echo
		fi
}

if [[ -f "/usr/bin/whoami" ]]; then
	username=`whoami`
fi
echo
echo "User = $username"

if [[ "$username" == root ]]; then
	telemetrypatch
elif [[ "$username" == "" ]]; then
	telemetrypatch
elif [[ ! "$username" == root ]]; then
	echo 
	echo "This script must be run as root!"
	sudo "$0"
fi

exit 0