#!/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=22.91.99999999

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
}

# check for configuration files
config_locations="/data/media/0 /external_sd /sdcard1 /data"

get_config () {
	config=""
	config_exists=0
	for path in ${config_locations}; do
		if test -r "${path}/.nanodroid-${1}"; then
			config="${path}/.nanodroid-${1}"
			config_exists=1
			return
		fi
	done
}

is_mounted () {
	grep -q "$(readlink -f ${1})" /proc/mounts 2>/dev/null
	return $?
}

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

grep_prop() {
	sed -n "s/^${1}=//p" ${build_props} ${2} | head -n 1
}

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
	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
	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"

	get_config setup
	if [ "${config_exists}" -eq 1 ]; then
		source "${config}"
		if [ "${nanodroid_overlay}" -eq 1 ]; then
			ui_print " ++ ${MODID} ${VERSION} addon.d: creating Overlays"
			/system/bin/nanodroid-overlay --create
			ui_print " ++ ${MODID} ${VERSION} addon.d: creating Overlays done"
		fi
	fi
}

detect_outfd

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

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