#!/bin/bash -ex

# Do some simple validation
if [[ "$BUILD_REVISION" == "" ]] ; then
	echo "This script should only be run on bots."
	exit 1
fi

# Print disk status before cleaning
df -h

# We don't care about errors in this section, we just want to clean as much as possible
set +e

# Clean workspace
(
	for repo in "$SYSTEM_DEFAULTWORKINGDIRECTORY"/.git "$SYSTEM_DEFAULTWORKINGDIRECTORY"/*/.git; do
		if test -d "$repo"; then
			cd "$repo"
			cd ..
			echo "Running 'git clean' (for all submodules too) in $(pwd)"
			git clean -xffd | sed 's/^/    /' || true
			git submodule foreach --recursive git clean -xffd | sed 's/^/    /' || true
		else
			echo "$repo is not a git repository"
		fi
	done

	echo "Cleaning packages directory:"
	rm -rv "$SYSTEM_DEFAULTWORKINGDIRECTORY/packages" | sed 's/^/    /' || true

	echo "Contents of SYSTEM_DEFAULTWORKINGDIRECTORY ($SYSTEM_DEFAULTWORKINGDIRECTORY):"
	# shellcheck disable=SC2012
	ls -la "$SYSTEM_DEFAULTWORKINGDIRECTORY" | sed 's/^/    /' || true
	echo "Contents of BUILD_SOURCESDIRECTORY ($BUILD_SOURCESDIRECTORY):"
	# shellcheck disable=SC2012
	ls -la "$BUILD_SOURCESDIRECTORY" | sed 's/^/    /' || true
)

# Delete all the simulator devices. These can take up a lot of space over time (I've seen 100+GB on the bots)
/Applications/Xcode.app/Contents/Developer/usr/bin/simctl delete all

# Delete old Xcodes.
ls -lad /Applications/Xcode*.app

oldXcodes=(
	"/Applications/Xcode44.app"
	"/Applications/Xcode5.app"
	"/Applications/Xcode502.app"
	"/Applications/Xcode511.app"
	"/Applications/Xcode6.0.1.app"
	"/Applications/Xcode6.app"
	"/Applications/Xcode601.app"
	"/Applications/Xcode61.app"
	"/Applications/Xcode611.app"
	"/Applications/Xcode62.app"
	"/Applications/Xcode63.app"
	"/Applications/Xcode64.app"
	"/Applications/Xcode7.app"
	"/Applications/Xcode701.app"
	"/Applications/Xcode71.app"
	"/Applications/Xcode711.app"
	"/Applications/Xcode72.app"
	"/Applications/Xcode731.app"
	"/Applications/Xcode8-GM.app"
	"/Applications/Xcode8.app"
	"/Applications/Xcode81-GM.app"
	"/Applications/Xcode81.app"
	"/Applications/Xcode82.app"
	"/Applications/Xcode821.app"
	"/Applications/Xcode83.app"
	"/Applications/Xcode833.app"
	"/Applications/Xcode9-GM.app"
	"/Applications/Xcode9.app"
	"/Applications/Xcode91.app"
	"/Applications/Xcode92.app"
	"/Applications/Xcode93.app"
	"/Applications/Xcode94.app"
	"/Applications/Xcode941.app"
	"/Applications/Xcode10.app"
	"/Applications/Xcode101-beta2.app"
	"/Applications/Xcode101-beta3.app"
	"/Applications/Xcode101.app"
	"/Applications/Xcode102-beta1.app"
	"/Applications/Xcode102.app"
	"/Applications/Xcode1021.app"
	"/Applications/Xcode103.app"
	"/Applications/Xcode10GM.app"
	"/Applications/Xcode11-beta3.app"
	"/Applications/Xcode11-GM.app"
	"/Applications/Xcode11.app"
	"/Applications/Xcode111.app"
	"/Applications/Xcode112.app"
	"/Applications/Xcode1121.app"
	"/Applications/Xcode113.app"
	"/Applications/Xcode1131.app"
	"/Applications/Xcode114-beta1.app"
	"/Applications/Xcode114-beta2.app"
	"/Applications/Xcode114-beta3.app"
	"/Applications/Xcode114.app"
	"/Applications/Xcode1141.app"
	"/Applications/Xcode115-beta1.app"
	"/Applications/Xcode115-beta2.app"
	"/Applications/Xcode115-GM.app"
	"/Applications/Xcode_8.0.app"
	"/Applications/Xcode_8.1.app"
	"/Applications/Xcode_8.2.1.app"
	"/Applications/Xcode_8.3.3.app"
	"/Applications/Xcode_9.0.app"
	"/Applications/Xcode_9.1.0.app"
	"/Applications/Xcode_9.2.0.app"
	"/Applications/Xcode_9.2.app"
	"/Applications/Xcode_9.4.1.app"
# Xcode 10.2.1 is currently used by Binding Tools for Swift # /Applications/Xcode_10.2.1.app
	"/Applications/Xcode_11.3.0.app"
	"/Applications/Xcode_11.5.0.app"
	"/Applications/Xcode_11.6.0-beta1.app"
	"/Applications/Xcode_12.0.0-beta1.app"
	"/Applications/Xcode_12.0.0-beta2.app"
	"/Applications/Xcode_12.0.0-beta3.app"
	"/Applications/Xcode_12.0.0-beta4.app"
	"/Applications/Xcode_12.0.0-beta5.app"
	"/Applications/Xcode_12.0.0-beta6.app"
	"/Applications/Xcode_12.1.0-GM.app"
	"/Applications/Xcode_12.0.0-GMb.app"
	"/Applications/Xcode_12.2.0-beta1.app"
	"/Applications/Xcode_12.2.0-beta2.app"
	"/Applications/Xcode_12.2.0-beta3.app"
	"/Applications/Xcode_12.2.0-beta.3.app"
	"/Applications/Xcode_12.2.0-rc.app"
	"/Applications/Xcode_12.5.0-rc.app"
	"/Applications/Xcode_13.0.0-beta.app"
	"/Applications/Xcode_13.0.0-beta2.app"
	"/Applications/Xcode_13.0.0-beta3.app"
)

# remove wrongly added .xip files under /Applications, confuses provisionator and 
# are not needed and wrong
sudo rm -Rf /Applications/*.xip

# pick the current selected xcode to make sure we do not remove it.
XCODE_SELECT=$(xcode-select -p)

for oldXcode in "${oldXcodes[@]}"; do
	if [ "$XCODE_SELECT" != "$oldXcode/Contents/Developer" ]; then
		if test -d "$oldXcode"; then
			sudo rm -Rf "$oldXcode"
		fi
	else
		echo "Not removing $oldXcode because is the currently selected one."
	fi
done

DIR="$(dirname "${BASH_SOURCE[0]}")"
"$DIR"/clean-simulator-runtime.sh
"$DIR"/kill-deadlocked-processes.sh

# Remove legacy Xamarin/MonoTouch stuff
sudo rm -Rf /Developer/MonoTouch
sudo rm -Rf /Library/Frameworks/Xamarin.iOS.framework
sudo rm -Rf /Library/Frameworks/Xamarin.Mac.framework
ls -R /Library/Frameworks

echo "Clearing xcrun cache..."
xcrun -k

# Print disk status after cleaning
df -h
