#!/system/bin/sh

export NO_MAGISK=0
export MODPATH=@MODPATH@

if [ -z "${MODPATH}" ]; then
	OVERLAY_PATH=""
	NO_MAGISK=1
elif [ -d "${MODPATH}" ]; then
	OVERLAY_PATH="${MODPATH}"
elif [ -d "/dev/tmp/magisk_img/$(basename "${MODPATH}")" ]; then
	OVERLAY_PATH="/dev/tmp/magisk_img/$(basename "${MODPATH}")"
else
	echo "couldn't find NanoDroid!"
	exit 1
fi

error () {
	echo "!! ${@}"
	exit 1
}

show_help () {

	echo "nanodroid-overlay

Magisk Mode: Manages Overlays (Pseudo Debloat feature)
System Mode: Manages Moved-Outs (Force Debloat feature)

Options:
-a | --add		[appname]	add override for app [appname]
-r | --remove		[appname]	remove override for app [appname]
-s | --show				show non-overriden /system applications
-l | --list				show all overriden /system applications
-x | --create				create all overrides
-u | --update				update all overrides (= clear + create)
-c | --clear				remove all overrides
-g | --genconfig			(re-)create configuration from existing overrides
"

	exit 0
}

[[ $(whoami) != "root" ]] && error "not running as root"

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

overrides_add_intern () {
	sysdir=${1}
	app=${2}

	if [[ ${NO_MAGISK} -eq 0 ]]; then
		echo "  creating overlay: ${app}"
		mkdir -p "${OVERLAY_PATH}/system/${sysdir}/${app}"
		touch "${OVERLAY_PATH}/system/${sysdir}/${app}/.replace"
	else
		mkdir -p "/sdcard/nanodroid_backups/${sysdir}"
		echo "  moving to /sdcard/nanodroid_backups: ${app}"
		rm -rf "/sdcard/nanodroid_backups/${sysdir}/${app}"
		mv "/system/${sysdir}/${app}" "/sdcard/nanodroid_backups/${sysdir}/"
	fi

	grep -q "^${app}" "${config}" || echo "${app}" >> "${config}"
}

overrides_add () {
	for app in ${@}; do
		[ -d /system/app/${app} ] && overrides_add_intern app ${app}
		[ -d /system/priv-app/${app} ] && overrides_add_intern priv-app ${app}
	done
}

overrides_remove_intern () {
	sysdir=${1}
	app=${2}

	if [[ ${NO_MAGISK} -eq 0 ]]; then
		echo "  removing overlay: ${app}"
		rm -rf "${OVERLAY_PATH}/system/${sysdir}/${app}"
	else
		if [[ -d /system/${sysdir}/${app} ]]; then
			echo "  removing old backup: ${app}"
			rm -rf "/sdcard/nanodroid_backups/${sysdir}/${app}"
		else
			echo "  reinstalling: ${app}"
			mv "/sdcard/nanodroid_backups/${sysdir}/${app}" "/system/${sysdir}/"
		fi
	fi

	sed -e "/^${app}$/d" -i "${config}"
}

overrides_remove () {
	for app in ${@}; do
		if [[ ${NO_MAGISK} -eq 0 ]]; then
			[ -f ${OVERLAY_PATH}/system/app/${app}/.replace ] && overrides_remove_intern app ${app}
			[ -f ${OVERLAY_PATH}/system/priv-app/${app}/.replace ] && overrides_remove_intern priv-app ${app}
		else
			[ -d /sdcard/nanodroid_backups/app/${app} ] && overrides_remove_intern app ${app}
			[ -d /sdcard/nanodroid_backups/priv-app/${app} ] && overrides_remove_intern priv-app ${app}
		fi
	done
}

overrides_list () {
	if [[ ${NO_MAGISK} -eq 0 ]]; then
		echo "Overrides for /system/app:"
		for app in $(find "${OVERLAY_PATH}/system/app" -name "*.replace"); do
			echo "  * $(basename $(dirname "${app}"))"
		done

		echo "Overrides for /system/priv-app:"
		for app in $(find "${OVERLAY_PATH}/system/priv-app" -name "*.replace"); do
			echo "  * $(basename $(dirname "${app}"))"
		done
	else
		echo "Moved-Out apps from /system/app:"
		for app in $(find "/sdcard/nanodroid_backups/app" -mindepth 1 -maxdepth 1 -type d); do
			echo "  * $(basename $(dirname "${app}"))"
		done

		echo "Moved-Out apps from /system/priv-app:"
		for app in $(find "/sdcard/nanodroid_backups/priv-app" -mindepth 1 -maxdepth 1 -type d); do
			echo "  * $(basename $(dirname "${app}"))"
		done
	fi
}

overrides_list_non () {
	if [[ ${NO_MAGISK} -eq 0 ]]; then
		echo "non-overriden apps from /system/app:"
		for app in $(find "/system/app" -mindepth 1 -maxdepth 1 -type d); do
			[[ ! -f ${OVERLAY_PATH}/${app}/.replace ]] && echo "  * $(basename ${app})"
		done

		echo "non-overriden apps from /system/priv-app:"
		for app in $(find "/system/priv-app" -mindepth 1 -maxdepth 1 -type d); do
			[[ ! -f ${OVERLAY_PATH}/${app}/.replace ]] && echo "  * $(basename ${app})"
		done
	else
		echo "non-moved-out apps from /system/app:"
		for app in $(find "/system/app" -mindepth 1 -maxdepth 1  -type d); do
			echo "  * $(basename ${app})"
		done

		echo "non-moved-out apps from /system/priv-app:"
		for app in $(find "/system/priv-app" -mindepth 1 -maxdepth 1  -type d); do
			echo "  * $(basename ${app})"
		done
	fi

}

overrides_clear () {
	if [[ ${NO_MAGISK} -eq 0 ]]; then
		echo "  removing Overlays from ${OVERLAY_PATH}"
		for app in $(find "${OVERLAY_PATH}/system/app" -name "*.replace") \
			   $(find "${OVERLAY_PATH}/system/priv-app" -name "*.replace"); do
			echo "  removing override: $(basename $(dirname ${app}))"
			rm -rf "$(dirname "${app}")"
		done
	else
		echo "  overrides_clear() does nothing in System Mode!"
	fi
}

overrides_genconfig () {
	if [[ ${NO_MAGISK} -eq 0 ]]; then
		rm -f ${config}
		export config="/data/.nanodroid-overlay"

		for app in $(find "${OVERLAY_PATH}/system/app" -name "*.replace") \
			   $(find "${OVERLAY_PATH}/system/priv-app" -name "*.replace"); do
			xapp=$(basename $(dirname ${app}))
			echo "  adding ${xapp} to configuration"
			grep -q "^${xapp}$" "${config}" || echo "${xapp}" >> "${config}"
		done
	else
		echo "  overrides_genconfig() does nothing in System Mode!"
	fi
}

# check for configuration files
config_locations="/sdcard /external_sd @ZIPDIR@ /data /dev/tmp/install"
get_config .nanodroid-overlay

if [[ -f ${config} ]]; then
	export config
else	overrides_genconfig
fi

opt=${1}
[[ -z ${opt} ]] && show_help || shift

case ${opt} in
	-l | --list )		overrides_list ;;
	-a | --add )		overrides_add "${@}" ;;
	-r | --remove )		overrides_remove "${@}" ;;
	-s | --show )		overrides_list_non ;;
	-c | --clear )		overrides_clear ;;
	-x | --create )		overrides_add "$(cat "${config}")" ;;
	-g | --genconfig )	overrides_genconfig ;;

	-u | --update )
		overrides_clear
		overrides_add "$(cat "${config}")"
	;;


	* ) show_help ;;
esac
