#!/bin/bash

#09FEB2021 KM4ACK
#script to remove and lock out microsoft repository
#reference - https://www.reddit.com/r/linux/comments/lbu0t1/microsoft_repo_installed_on_all_raspberry_pis/
#reference - https://www.cyberciti.biz/linux-news/heads-up-microsoft-repo-secretly-installed-on-all-raspberry-pis-linux-os/

#Verify not running as root user
WHO=$(whoami)
if [ $WHO = 'root' ]; then
echo "No need to run this script as root"
exit 1
fi

TEMP=/run/user/$UID/hosts

echo "Would you like to put a hold on the"
read -p "raspberrypi-sys-mods package? y/n " ANS

if [ $ANS = 'y' ] || [ $ANS = 'Y' ];then
echo "A hold will be placed on the raspberrypi-sys-mods package"
echo "You can remove this hold at anytime by running"
echo "sudo apt-mark unhold raspberrypi-sys-mods"

#Hold raspberrypi-sys-mods package
sudo apt-mark hold raspberrypi-sys-mods
fi

#Redirect calls to packages.microsoft.com to localhost
cat /etc/hosts > $TEMP
echo "0.0.0.0 packages.microsoft.com" >> $TEMP
sudo cp $TEMP /etc/

#remove MS Key file
sudo rm -vf /etc/apt/trusted.gpg.d/microsoft.gpg

#create empty dummy key file
sudo touch /etc/apt/trusted.gpg.d/microsoft.gpg

#lock the dummy key file
sudo chattr +i /etc/apt/trusted.gpg.d/microsoft.gpg

#comment out vscode.list
sudo sed -i 's/deb/#deb/' /etc/apt/sources.list.d/vscode.list

#lock the vscode.list file
sudo chattr +i /etc/apt/sources.list.d/vscode.list

echo "All finished!"