#!/bin/sh

# Systemlessly de-bloats system apps with Magisk.

[ "${EUID:-$(id -u)}" -gt 0 ] && { echo "You must run this with elevated privileges (such as with sudo)." ; exit 1 ;}

# Module path
module="/data/adb/modules/systemless-de-bloater"

err() { echo "Failed!" ; exit 1 ;}

debloat_dir() {
	dir="$1"
	echo "De-bloating '$dir'..."
	sudo mkdir -p "$module/$dir" || err
	sudo cp /dev/null "$module/$dir/.replace" || err
	echo "Done"
	}

debloat_apk() {
	apk="$1"
	echo "De-bloating '$apk'..."
	sudo mkdir -p "$module/${apk%/*}" || err
	sudo cp /dev/null "$module/$apk" || err
	echo "Done"
	}

debloat_capex() {
	package="$1"
	echo "Uninstalling '$package'... "
	sudo cmd package uninstall --user $user "$package" || err
	echo "Done"
	}

# Texting and calling:
# system/priv-app/TeleService
# system/priv-app/TelephonyProvider
# system/product/overlay/TeleService__lineage_walleye__auto_generated_rro_product.apk
# system/product/overlay/TeleService__nosdcard__auto_generated_characteristics_rro.apk
# system/product/overlay/TelephonyProvider__nosdcard__auto_generated_characteristics_rro.apk
# system/vendor/overlay/TeleService__lineage_walleye__auto_generated_rro_vendor.apk

# Bluetooth:
# system/apex/com.android.btservices.apex

# Printing:
# system/app/PrintRecommendationService
# system/app/PrintSpooler

# Ringtone, alarms, etc:
# system/priv-app/SoundPicker

# System colors:
# system/product/app/ThemesStub
# system/system_ext/priv-app/ThemePicker

# Work profile:
# system/priv-app/ManagedProvisioning
# system/product/overlay/ManagedProvisioning__lineage_walleye__auto_generated_rro_product.apk

# System updates:
# system/system_ext/priv-app/Updater
# system/product/overlay/Updater__lineage_walleye__auto_generated_rro_product.apk

# Create empty directories for system apps to be overlayed with.
for app in \
	system/apex/com.android.appsearch.capex \
	system/apex/com.android.cellbroadcast.capex \
	system/apex/com.android.healthfitness.apex \
	system/apex/com.android.mediaprovider.capex \
	system/apex/com.android.nfcservices.capex \
	system/apex/com.android.ondevicepersonalization.capex \
	system/apex/com.android.rkpd.apex \
	system/apex/com.android.uwb.capex \
	system/apex/com.android.virt.apex \
	system/app/BasicDreams \
	system/app/BluetoothMidiService \
	system/app/BookmarkProvider \
	system/app/CameraExtensionsProxy \
	system/app/CaptivePortalLogin \
	system/app/CarrierDefaultApp \
	system/app/CompanionDeviceManager \
	system/app/EasterEgg \
	system/app/HTMLViewer \
	system/app/PacProcessor \
	system/app/QAS_DVC_MSP_VZW \
	system/app/SecureElement \
	system/app/SimAppDialog \
	system/app/Stk \
	system/app/WallpaperBackup \
	system/app/ims \
	system/app/uceShimService \
	system/app/vzw_msdc_api \
	system/priv-app/AvatarPicker \
	system/priv-app/BackupRestoreConfirmation \
	system/priv-app/BlockedNumberProvider \
	system/priv-app/BuiltInPrintService \
	system/priv-app/CNEService \
	system/priv-app/CalendarProvider \
	system/priv-app/CallLogBackup \
	system/priv-app/CellBroadcastLegacyApp \
	system/priv-app/ContactsProvider \
	system/priv-app/CredentialManager \
	system/priv-app/DeviceAsWebcam \
	system/priv-app/DeviceDiagnostics \
	system/priv-app/DownloadProviderUi \
	system/priv-app/DynamicSystemInstallationService \
	system/priv-app/E2eeContactKeysProvider \
	system/priv-app/InputDevices \
	system/priv-app/LiveWallpapersPicker \
	system/priv-app/LocalTransport \
	system/priv-app/MmsService \
	system/priv-app/MtpService \
	system/priv-app/NfcNci \
	system/priv-app/ONS \
	system/priv-app/OemDmTrigger \
	system/priv-app/ProxyHandler \
	system/priv-app/SharedStorageBackup \
	system/priv-app/StatementService \
	system/priv-app/Tag \
	system/priv-app/Telecom \
	system/priv-app/UserDictionaryProvider \
	system/priv-app/qcrilmsgtunnel \
	system/product/app/Aperture \
	system/product/app/ApertureLensLauncher \
	system/product/app/Backgrounds \
	system/product/app/Camelot \
	system/product/app/DeskClock \
	system/product/app/Etar \
	system/product/app/ExactCalculator \
	system/product/app/Gallery2 \
	system/product/app/Glimpse \
	system/product/app/Jelly \
	system/product/app/LatinIME \
	system/product/app/LocalContactsBackup \
	system/product/app/PhotoTable \
	system/product/app/Recorder \
	system/product/app/Twelve \
	system/product/app/VZWAPNLib \
	system/product/app/messaging \
	system/product/overlay/DisplayCutoutEmulationCorner \
	system/product/overlay/DisplayCutoutEmulationDouble \
	system/product/overlay/DisplayCutoutEmulationHole \
	system/product/overlay/DisplayCutoutEmulationTall \
	system/product/overlay/DisplayCutoutEmulationWaterfall \
	system/product/overlay/FontLato \
	system/product/overlay/FontNotoSerifSource \
	system/product/overlay/FontRubik \
	system/product/overlay/IconPackCircularAndroid \
	system/product/overlay/IconPackCircularLauncher \
	system/product/overlay/IconPackCircularSettings \
	system/product/overlay/IconPackCircularSystemUI \
	system/product/overlay/IconPackFilledAndroid \
	system/product/overlay/IconPackFilledLauncher \
	system/product/overlay/IconPackFilledSettings \
	system/product/overlay/IconPackFilledSystemUI \
	system/product/overlay/IconPackKaiAndroid \
	system/product/overlay/IconPackKaiLauncher \
	system/product/overlay/IconPackKaiSettings \
	system/product/overlay/IconPackKaiSystemUI \
	system/product/overlay/IconPackRoundedAndroid \
	system/product/overlay/IconPackRoundedLauncher \
	system/product/overlay/IconPackRoundedSettings \
	system/product/overlay/IconPackRoundedSystemUI \
	system/product/overlay/IconPackSamAndroid \
	system/product/overlay/IconPackSamLauncher \
	system/product/overlay/IconPackSamSettings \
	system/product/overlay/IconPackSamSystemUI \
	system/product/overlay/IconPackVictorAndroid \
	system/product/overlay/IconPackVictorLauncher \
	system/product/overlay/IconPackVictorSettings \
	system/product/overlay/IconPackVictorSystemUI \
	system/product/overlay/IconShapePebble \
	system/product/overlay/IconShapeRoundedRect \
	system/product/overlay/IconShapeSquare \
	system/product/overlay/IconShapeSquircle \
	system/product/overlay/IconShapeTaperedRect \
	system/product/overlay/IconShapeTeardrop \
	system/product/overlay/IconShapeVessel \
	system/product/overlay/LineageNavigationBarNoHint \
	system/product/overlay/NavigationBarMode2Button \
	system/product/overlay/NavigationBarMode3Button \
	system/product/overlay/NotesRoleEnabled \
	system/product/overlay/TransparentNavigationBar \
	system/product/priv-app/AmbientSensePrebuilt \
	system/product/priv-app/AppDirectedSMSService \
	system/product/priv-app/CarrierServices \
	system/product/priv-app/CarrierSettings \
	system/product/priv-app/CarrierWifi \
	system/product/priv-app/ConnMO \
	system/product/priv-app/Contacts \
	system/product/priv-app/DCMO \
	system/product/priv-app/DMService \
	system/product/priv-app/DevicePersonalizationPrebuiltPixel2 \
	system/product/priv-app/DiagMon \
	system/product/priv-app/Dialer \
	system/product/priv-app/HardwareInfo \
	system/product/priv-app/HotwordEnrollmentOKGoogleWCD9340 \
	system/product/priv-app/HotwordEnrollmentXGoogleWCD9340 \
	system/product/priv-app/ImsServiceEntitlement \
	system/product/priv-app/MyVerizonServices \
	system/product/priv-app/OBDM_Permissions \
	system/product/priv-app/SettingsIntelligence \
	system/product/priv-app/SprintDM \
	system/product/priv-app/SprintHM \
	system/product/priv-app/TetheringEntitlement \
	system/product/priv-app/WfcActivation \
	system/product/priv-app/obdm_stub \
	system/system_ext/app/AccessibilityMenu \
	system/system_ext/app/PresencePolling \
	system/system_ext/app/Profiles \
	system/system_ext/app/RcsService \
	system/system_ext/app/WAPPushManager \
	system/system_ext/priv-app/AudioFX \
	system/system_ext/priv-app/CarrierConfig \
	system/system_ext/priv-app/CarrierSetup \
	system/system_ext/priv-app/ElmyraService \
	system/system_ext/priv-app/EmergencyInfo \
	system/system_ext/priv-app/LineageSetupWizard \
	system/system_ext/priv-app/QuickAccessWallet \
	system/system_ext/priv-app/Seedvault \
	system/system_ext/priv-app/StorageManager \
	system/vendor/app/ModemService \
	system/vendor/app/QtiTelephonyService
do
	debloat_app "$app"
done

# Create blank files for system apps to be overlayed with.
for apk in \
	system/product/overlay/ApertureFrameworksBaseOverlay.apk \
	system/product/overlay/CompanionDeviceManager__nosdcard__auto_generated_characteristics_rro.apk \
	system/product/overlay/DeviceDiagnostics__lineage_walleye__auto_generated_rro_product.apk \
	system/product/overlay/Dialer__lineage_walleye__auto_generated_rro_product.apk \
	system/product/overlay/ElmyraService__lineage_walleye__auto_generated_rro_product.apk \
	system/product/overlay/EmergencyInfo__lineage_walleye__auto_generated_rro_product.apk \
	system/product/overlay/GlimpseFrameworksBaseOverlay.apk \
	system/product/overlay/ImsServiceEntitlement__lineage_walleye__auto_generated_rro_product.apk \
	system/product/overlay/LatinIME__lineage_walleye__auto_generated_rro_product.apk \
	system/product/overlay/LineageSetupWizard__lineage_walleye__auto_generated_rro_product.apk \
	system/product/overlay/StorageManager__lineage_walleye__auto_generated_rro_product.apk \
	system/vendor/overlay/Dialer__lineage_walleye__auto_generated_rro_vendor.apk
do
	debloat_apk "$apk"
done

# .capex system apps can't be debloated normally. So ADB is used to "uninstall"
# them. The system files are not deleted.

# Set user number for package
user=0

for capex in \
	com.android.adservices.api \
	com.android.appsearch.apk \
	com.android.cellbroadcastreceiver.module \
	com.android.federatedcompute.services \
	com.android.nfc \
	com.android.ondevicepersonalization.services \
	com.android.photopicker \
	com.android.server.deviceconfig.resources \
	com.android.hotspot2.osulogin \
	com.android.safetycenter.resources \
	com.android.uwb.resources
do
	debloat_capex "$capex"
done

echo "Finished"
