#!/bin/bash

if [ $# -gt 0 ]; then
	gpu_debug_app=`adb shell cmd package list packages | dos2unix | grep -i "$1" | sed 's/package://g'`
fi

if [ "$gpu_debug_app" == "" ]; then
	if [ $# -gt 0 ]; then
		echo package not found
	else
		echo "adb shell settings list global | grep gpu_debug"
		adb shell settings list global | grep gpu_debug
	fi
else

	primaryCpuAbi=`adb shell cmd package dump $gpu_debug_app | dos2unix | grep -i primaryCpuAbi | sed 's/.*primaryCpuAbi=//g'`

	gpu_debug_layer_app=
	if [ "$primaryCpuAbi" == "arm64-v8a" ]; then
		gpu_debug_layer_app=org.renderdoc.renderdoccmd.arm64
	else
		gpu_debug_layer_app=org.renderdoc.renderdoccmd.arm32
	fi

	adb shell settings put global enable_gpu_debug_layers 1
	adb shell settings put global gpu_debug_app $gpu_debug_app
	if [ "$2" == "1" ]; then
		adb shell settings put global gpu_debug_layer_app $gpu_debug_layer_app
	else
		adb shell settings delete global gpu_debug_layer_app
	fi
	adb shell settings put global gpu_debug_layers VK_LAYER_RENDERDOC_Capture
	adb shell settings put global gpu_debug_layers_gles libVkLayer_GLES_RenderDoc.so
fi
