#!/sbin/sh
#
# ADDOND_VERSION=2
#
##########################################################################################
# 
# NanoDroid System Mode OTA survival Script
# by Nanolx
# 
# Inspired by 99-flashafterupdate.sh of osm0sis @ xda-developers
# Forked from 99-magisk.sh of topjohnwu @ xda-developers
# 
##########################################################################################

source /tmp/backuptool.functions || source /postinstall/tmp/backuptool.functions

MODID=NanoDroid
OUTFD=
VERSION=23.1.2.20210117

NANODROID_LIST=/system/addon.d/NanoDroid_FileList

print_google_apps()
{
cat <<EOF
AMAPNetworkLocation
BaiduNetworkLocation
BlankStore
ConfigUpdater
GCS
GmsCoreSetupPrebuilt
GmsCore_update
GoogleFeedback
GoogleLoginService
GoogleOneTimeInitializer
GoogleServicesFramework
GoogleConnectivityServices
GoogleTTS
LegacyNetworkLocation
MarketUpdater
MarkupGoogle
NetworkLocation
PlayGames
PlayStore
PrebuiltGmsCore
PrebuiltGmsCorePi
PrebuiltGmsCorePix
UnifiedNlp
Velvet
Vending
WhisperPush
EOF
}

ui_print () {
	echo -n -e "ui_print ${1}\n" >> /proc/self/fd/${OUTFD}
	echo -n -e "ui_print\n" >> /proc/self/fd/${OUTFD}
}

detect_outfd () {
	# taken from Magisk
	# update-binary|updater <RECOVERY_API_VERSION> <OUTFD> <ZIPFILE>
	OUTFD=$(ps | grep -v 'grep' | grep -oE 'update(.*) 3 [0-9]+' | cut -d" " -f3)
	[ -z $OUTFD ] && OUTFD=$(ps -Af | grep -v 'grep' | grep -oE 'update(.*) 3 [0-9]+' | cut -d" " -f3)
	# update_engine_sideload --payload=file://<ZIPFILE> --offset=<OFFSET> --headers=<HEADERS> --status_fd=<OUTFD>
	[ -z $OUTFD ] && OUTFD=$(ps | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2)
	[ -z $OUTFD ] && OUTFD=$(ps -Af | grep -v 'grep' | grep -oE 'status_fd=[0-9]+' | cut -d= -f2)
}

backup_action () {
	ui_print " ++ ${MODID} ${VERSION} addon.d: backup"
	cat ${NANODROID_LIST} | while read FILE; do
		echo " + backup: ${FILE}"
		backup_file "${FILE}"
	done

	for file in .nanodroid-apps .nanodroid-overlay .nanodroid-setup; do
		[ -f /system/addon.d/${file} ] && backup_file /system/addon.d/${file}
	done
	ui_print " ++ ${MODID} ${VERSION} addon.d: backup done"
}

restore_action () {
	ui_print " ++ ${MODID} ${VERSION} addon.d: restore"
	cat ${NANODROID_LIST} | while read FILE; do
		echo " + restore: ${FILE}"
		restore_file "${FILE}"
	done

	for file in .nanodroid-apps .nanodroid-overlay .nanodroid-setup; do
		restore_file /system/addon.d/${file}
	done
	ui_print " ++ ${MODID} ${VERSION} addon.d: restore done"
}

postrestore_action () {
	ui_print " ++ ${MODID} ${VERSION} addon.d: GApps removal"
	print_google_apps | while read app; do
		/system/bin/nanodroid-overlay --add ${app}
	done

	ui_print " ++ ${MODID} ${VERSION} addon.d: GApps removal done"

	if [ -f /system/addon.d/.nanodroid-overlay ]; then
		ui_print " ++ ${MODID} ${VERSION} addon.d: creating Overlays"
		/system/bin/nanodroid-overlay --create
		ui_print " ++ ${MODID} ${VERSION} addon.d: creating Overlays done"
	fi
}

detect_outfd

if [ ! -r ${NANODROID_LIST} ]; then
	echo "No NanoDroid installer information found!"
	exit 0
fi

case "${1}" in
	backup)
		backup_action
	;;
	restore)
		restore_action
	;;
	pre-backup)
		# Stub
	;;
	post-backup)
		# Stub
	;;
	pre-restore)
		# Stub
	;;
	post-restore)
		postrestore_action
	;;
esac
