#!/bin/bash

POSITIONAL=()
while [[ $# -gt 0 ]]
do
	key="$1"
	case $key in
		-b|--bundle)
		bundleIdentifier="$2"
		shift # past argument
		shift # past value
		;;
		-n|--name)
		applicationName="$2"
		shift
		shift
		;;
		-x|--exec)
		executableName="$2"
		shift
		shift
		;;
		-e|--enable)
		enable=YES
		shift # past argument
		;;
		-d|--disable)
		disable=YES
		shift
		;;
		-f|--force)
		force=YES
		shift
		;;
		-h|--help)
		help=YES
		shift
		;;
		-s|--sign)
		sign=YES
		shift
		;;
		-c|--cyscript)
		cyscript="$2"
		shift
		shift
		;;
		*)    # unknown option
		POSITIONAL+=("$1") # save it in an array for later
		shift # past argument
		;;
	esac
done

set -- "${POSITIONAL[@]}" # restore positional parameters

if [ "$help" == "YES" ]; then
	echo "Usage:"
	echo "    -b <AppBundleID>        - Bundle Identifier of the Application"
	echo "                                \"com.apple.MobileSMS\""
	echo "    -n <AppName>            - Application Name, ExecutableName, IconName or LocalizedName"
	echo "                                \"Messages\""
	echo "    -x <ExecutableName>     - Executable Name"
	echo "                                \"backboardd\""
	echo "    -e                      - Enable Cycript for the given Process"
	echo "                                Then, if it is an App, restart it"
	echo "    -d                      - Disable Cycript for the given Process"
	echo "                                Then, if it is an App, restart it"
	echo "                            If both enable and disable are set,"
	echo "                                Cycript will be loaded and then unloaded when done"
	echo "    -c </path/to/script.cy> - Load an external Cycript script"
	echo "    -s                      - Sign the Cycript binaries with entitlements for Electra iOS 11"
	echo "    -h                      - This help file"
	echo "    You must choose an option to Sign or an (AppBundleID or AppName or ExecutableName) and options for (enable and/or disable Cycript)"
	exit 0
fi

if [ "$sign" == "YES" ]; then
	`cycript -h`
	retVal=$?

	if [ "$retVal" == "127" ] ; then
		echo "ERROR - The Cycript binary does not appear to be installed"
		exit 1
	fi

	if [ "$retVal" != "137" ] && [ "$force" != "YES" ]; then
		echo "The Cycript binary appears to have already been signed correctly"
		echo "If you would like to force it to be resigned, run this again with"
		echo "    cyrun -s -f"
		exit 0
	fi

	read -p "Would you like to sign the Cycript binaries for the Electra JB (y or n)? " -n 1 -r
	echo
	if [[ ! $REPLY =~ ^[Yy]$ ]]; then
		echo "Ok, cancelled"
		exit 0
	fi

	cat > /tmp/cyrun_ent.xml << EOF
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>platform-application</key>
	<true/>
	<key>com.apple.private.skip-library-validation</key>
	<true/>
	<key>run-unsigned-code</key>
	<true/>
	<key>get-task-allow</key>
	<true/>
</dict>
</plist>
EOF

	# cp -a /usr/bin/cycript /usr/bin/cycript.bak
	# cp -a /usr/lib/libcycript.dylib /usr/lib/libcycript.dylib.bak

	cp -a /usr/bin/cycript /tmp/
	cp -a /usr/lib/libcycript.dylib /tmp/
	rm -rf /usr/bin/cycript
	rm -rf /usr/lib/libcycript.dylib
	ldid -S/tmp/cyrun_ent.xml /tmp/cycript
	ldid -S /tmp/libcycript.dylib
	cp -a /tmp/cycript /usr/bin/
	cp -a /tmp/libcycript.dylib /usr/lib/

	exit 0
fi

if [ "$sign" == "YES" ]; then
	netstat -na | grep LISTEN
fi

cmd="cyrun2"
if [ "$help" == "YES" ]; then
	enable=N
	disable=N
	bundleIdentifier=""
	applicationName=""
	cmd="$cmd -h"
fi

if [ "$bundleIdentifier" != "" ]; then
	cmd="$cmd -b $bundleIdentifier"
fi

if [ "$applicationName" != "" ]; then
	cmd="$cmd -n $applicationName"
fi

if [ "$executableName" != "" ]; then
	cmd="$cmd -x $executableName"
fi

if [ "$enable" == "YES" ]; then
	cmd="$cmd -e"
fi

if [ "$disable" == "YES" ]; then
	cmd="$cmd -d"
fi

if [ "$force" == "YES" ]; then
	cmd="$cmd -f"
fi

`$cmd`
retVal=$?

if [ "$retVal" == "0" ] && [ "$enable" == "YES" ]; then
	if [ "$cyscript" != "" ]; then
		cycript -r 127.0.0.1:8556 "$cyscript"
	fi
	cycript -r 127.0.0.1:8556

	if [ "$disable" == "YES" ]; then
		cmd="cyrun2 -d"

		if [ "$bundleIdentifier" != "" ]; then
			cmd="$cmd -b $bundleIdentifier"
		fi

		if [ "$applicationName" != "" ]; then
			cmd="$cmd -n $applicationName"
		fi

		if [ "$executableName" != "" ]; then
			cmd="$cmd -x $executableName"
		fi

		if [ "$force" == "YES" ]; then
			cmd="$cmd -f"
		fi

		`$cmd`
	fi
fi
