#!/sbin/sh

# check for configuration files
# check for configuration files
config_locations="/sdcard /external_sd /tmp /data"

get_config () {
	config=""
	config_exists=0
	for path in ${config_locations}; do
		# rename config files if required (< 15.1)
		if test -r "${path}/.nanomod-${1}"; then
			mv "${path}/.nanomod-${1}" "${path}/.nanodroid-${1}"
		fi

		if test -r "${path}/.nanodroid-${1}"; then
			config="${path}/.nanodroid-${1}"
			config_exists=1
			return
		fi
	done
}

# check whether '.nanodroid-overlay' has new format
# and update if required
check_nanodroidoverlay () {
	if grep -q "APPS=(" "${config}"; then
		ui_print " ++ migrating ${config} to new format"
		sed -e 's/^.*APPS=//;s/(//g;s/)//g' -i "${config}"
		sed '/^\s*$/d' -i "${config}"
	fi
}

# check whether '.nanodroid-overlay' exists,
# if not, use fallback ${APPS}
get_config overlay
if [ "$config_exists" -eq 1 ]; then
	APPS="$(cat "${config}")"
	NO_NANODROIDOVERLAY=0
	check_nanodroidoverlay
else	NO_NANODROIDOVERLAY=1
APPS="BasicDreams
Browser
Calendar
Camera2
CMFileManager
Eleven
Email
Exchange2
FMRadio
Gallery2
Gello
Jelly
messaging
mGerrit
OmniSwitch
Phonograph
PhotoTable
Recorder
ResurrectionStats
Screencast
Slimperience
Snap
SnapdragonCamera
SoundRecorder
Stk
ViaBrowser
Wallpaper
WallpaperBackup
WallpaperPickerGoogle"
fi

is_mounted() {
	if [ ! -z "$2" ]; then
		cat /proc/mounts | grep $1 | grep $2, >/dev/null
	else
		cat /proc/mounts | grep $1 >/dev/null
	fi
	return $?
}

grep_cmdline() {
	local REGEX="s/^${1}=//p"
	sed -E 's/ +/\n/g' /proc/cmdline | \
		sed -n "${REGEX}" 2>/dev/null
}

# taken from Magisk, with minor modifications for NanoDroid
mount_partitions () {
	SLOT=$(grep_cmdline androidboot.slot_suffix)
	if [ -z ${SLOT} ]; then
		SLOT=_$(grep_cmdline androidboot.slot)
		[ "${SLOT}" = "_" ] && SLOT=
	fi

	is_mounted /data || mount /data

	${BOOTMODE} || mount -o bind /dev/urandom /dev/random

	! is_mounted /system && mount -o rw /system

	if [ ! -f /system/build.prop ]; then
		SYSTEMBLOCK=$(find /dev/block -iname system${SLOT} | head -n 1)
		mount -t ext4 -o rw ${SYSTEMBLOCK} /system
	fi

	[ -f /system/build.prop ] || is_mounted /system || exit 1

	if [ -f /system/init ]; then
		mkdir /system_root 2>/dev/null
		mount --move /system /system_root
		mount -o bind /system_root/system /system
	fi

	[ ! -f /system/build.prop ] && exit 1
}


debloat_system () {
	mount_partitions

	for app in ${APPS}; do
		if [[ -d /system/app/${app} ]]; then
			rm -rf /system/app/${app}
			echo "${app} removed from /system/app"
		elif [[ -d /system/priv-app/${app} ]]; then
			rm -rf /system/priv-app/${app}
			echo "${app} removed from /system/priv-app"
		fi
	done

	umount /system
}

test_debloat_system () {
	if (is_mounted /system); then
		mount -orw,remount /system || error "!! failed to remount system read-write"
	else	mount -orw /system || error "!! failed to mount system read-write"
	fi

	echo "***** TEST MODE *****"

	for app in ${APPS}; do
		if [[ -d /system/app/${app} ]]; then
			echo "${app} would be removed from /system/app"
		elif [[ -d /system/priv-app/${app} ]]; then
			rm -rf /system/priv-app/${app}
			echo "${app} would be removed from /system/priv-app"
		fi
	done

	echo "***** END TEST MODE *****

if that looks good to you, re-run the Script, else
edit the Script or your '.nanodroid-overlay' file"

	umount /system
}

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

[[ ! -d /twres ]] && error "Not running from TWRP, exiting"
is_mounted /data || mount /data

echo "NanoDroid force-debloat script

This script will actually remove apps from your ROM
"

if [[ ${NO_NANODROIDOVERLAY} -eq 0 ]]; then
	echo "using \"${config}\" file for values"
else	echo "using fallback values"
fi

[[ -f /data/adb/magisk/magisk ]] && echo "Magisk is installed!

instead of using this Script, consider using Magisk
to pseudo-debloat your ROM instead. NanoDroid includes
the 'nanodroid-overlay' Script to ease doing so."

echo "
Are you sure you want to proceed?

Enter [y] / [j]  to remove applications
Enter [t] / [d]  to test what would be done
"

read -r USER_INPUT

case ${USER_INPUT} in
	y | Y | j | J )
		debloat_system
	;;

	t | T | d | D )
		debloat_system --test
	;;

	*)
		error "Exiting"
	;;
esac
