#!/bin/bash

echo ''
echo 'Setting "apt_news" value as "false" by `pro` command in Ubuntu...'
pro config set apt_news=false  
echo ''
echo 'Disabling apt-news & esm-cache from systemctl ...'
systemctl disable --now --no-block apt-news.service esm-cache.service
echo ''
### Reference: https://askubuntu.com/questions/1434512/how-to-get-rid-of-ubuntu-pro-advertisement-when-updating-apt
### https://archive.is/JH6xR
echo 'Removing message of the day file and disable Ubuntu Advantage...'
echo " -> '/etc/update-motd.d/88-esm-announce'"
rm /etc/update-motd.d/88-esm-announce
systemctl disable --now ubuntu-advantage
echo ''
echo ''
echo 'Disabling apt esm messages on updates ...'
echo " -> '/etc/apt/apt.conf.d/20apt-esm-hook.conf'"
###################
### MARK: Ver.1 ###
# sed -i 's/^/#/' '/etc/apt/apt.conf.d/20apt-esm-hook.conf'
# apt-get update && apt-get remove --purge -y ubuntu-advantage-tools motd-news-config && apt-get clean all && apt-get update && apt-get install -y python3-distupgrade python3-update-manager ubuntu-release-upgrader-core needrestart fwupd && apt-get clean all
###################
### MARK: Ver.2 ###
systemctl mask esm-cache.service
echo ''
dpkg-divert --rename --divert /etc/apt/apt.conf.d/20apt-esm-hook.conf.disabled --add /etc/apt/apt.conf.d/20apt-esm-hook.conf
echo ''
sed -i 's/^deb/#deb/' '/var/lib/ubuntu-advantage/apt-esm/etc/apt/sources.list.d/ubuntu-esm-apps.list'
echo ''
echo 'Updating apt package cache...'
echo '-> 'apt update''
apt update
echo ''
mv /etc/apt/apt.conf.d/20apt-esm-hook.conf /etc/apt/apt.conf.d/20apt-esm-hook.conf.bak
touch /etc/apt/apt.conf.d/20apt-esm-hook.conf
echo ''
echo ''
echo '============== Finished to remove Ubuntu pro ad(s) ==============='
echo ''
### MARK: Memo
# /var/lib/update-notifier/updates-available
# /var/lib/ubuntu-advantage/apt-esm/etc/apt/sources.list.d/ubuntu-esm-apps.sources