# Unzip curl
if [[ "$ARCH" = "arm" ]]; then
	unzip $MODPATH/bin/curl.zip -d $MODPATH/bin
elif [[ "$ARCH" = "arm64" ]]; then
	unzip $MODPATH/bin/curl64.zip -d $MODPATH/bin
else
	echo "Unsupported CPU Architecture"
	exit
fi
chmod 0755 $MODPATH/bin/$ARCH/curl

# Alias for Curl
alias curl='$MODPATH/bin/$ARCH/curl --dns-servers 1.1.1.1,1.0.0.1'

# Variables
local LOS=$(getprop | grep -o -c "lineage")

# API Check
if [[ $API -ge 29 ]]; then
	echo "Your Android Version is Supported!"
else
	abort "Your Android Version is not Supported!"
fi

# ARCH Check
if [[ "$ARCH" = "arm64" ]]; then
	echo "Your CPU Architecture is Supported!"
else
	abort "Your CPU Architecture is not Supported!"
fi

# Custom ROM Check
if [[ $LOS -gt 0 ]]; then
	TLP=/system/product/app/VanadiumTrichromeLibrary
	WVP=/system/product/app/VanadiumWebView
	echo "LineageOS Based Custom ROM Detected!"
else
	TLP=/system/app/VanadiumTrichromeLibrary
	WVP=/system/app/VanadiumWebView
fi
mkdir -p "$MODPATH"/$TLP
mkdir -p "$MODPATH"/$WVP

# Download and Install WebView
echo "Download and Install Vanadium WebView..."
curl -o $MODPATH/$TLP/VanadiumTrichromeLibrary.apk https://gitlab.com/grapheneos/platform_external_vanadium/-/raw/15-qpr2/prebuilt/arm64/TrichromeLibrary.apk
if [[ -f $MODPATH/$TLP/VanadiumTrichromeLibrary.apk ]]; then
	su -c cp $MODPATH/$TLP/VanadiumTrichromeLibrary.apk /data/local/tmp
	su -c pm install --install-location 1 /data/local/tmp/VanadiumTrichromeLibrary.apk
else
	abort "Couldn't Download File..."
fi
curl -o $MODPATH/$WVP/VanadiumWebView.apk https://gitlab.com/grapheneos/platform_external_vanadium/-/raw/15-qpr2/prebuilt/arm64/TrichromeWebView.apk
if [[ -f $MODPATH/$TLP/VanadiumWebView.apk ]]; then
	su -c cp $MODPATH/$WVP/VanadiumWebView.apk /data/local/tmp
	su -c pm install --install-location 1 /data/local/tmp/VanadiumWebView.apk
	echo "Vanadium WebView Downloaded and Installed!"
else
	abort "Couldn't Download File..."
fi

# Check for Overlay Directory (code from OpenWebview)
echo "Checking for Overlay Directory..."
if [[ $LOS -gt 0 ]]; then
	OVERLAY_PATH=system/product/overlay/
	elif [[ -d /system_ext/overlay ]]; then
	OVERLAY_PATH=system/system_ext/overlay/
	elif [[ -d /system/overlay ]]; then
	OVERLAY_PATH=system/overlay/
	elif [[ -d /system/vendor/overlay ]]; then
	OVERLAY_PATH=system/vendor/overlay/
	echo "Overlay Directory Found!"
else
	echo "Unable to Find a Correct Overlay Path!"
fi

# Create Overlay Directory and Copy Overlay.apk
echo "Creating Overlay Directory Inside the Module..."
mkdir -p "$MODPATH"/$OVERLAY_PATH
if [[ $API -eq 28 ]]; then
	cp $MODPATH/Overlay/WebViewOverlay28.apk $MODPATH/$OVERLAY_PATH/WebViewOverlay.apk
elif [[ $API -ge 29 ]]; then
	cp $MODPATH/Overlay/WebViewOverlay29.apk $MODPATH/$OVERLAY_PATH/WebViewOverlay.apk
fi

# Some CleanUp
echo "Cleaning Up..."
mv $MODPATH/bin/$ARCH/curl $MODPATH/bin/curl
rm -rf $MODPATH/bin/$ARCH
rm -rf $MODPATH/bin/*.zip
rm -rf $MODPATH/system/.placeholder
rm -rf $MODPATH/Overlay
rm -rf $MODPATH/common
echo "WV1=app.vanadium.webview" >> $MODPATH/debloat.sh
echo "WV2=app.vanadium.trichromelibrary" >> $MODPATH/debloat.sh