#!/bin/bash

# DISCLAIMER   :   It is recomended to test this script on a test machine.
#				           ManageEngine will not be responsible for any
#                  damage/loss to the data/setup based on the behavior of the script.
#
# DESCRIPTION  :   Script to block URL(s) in Chrome and Chromium-based browser
#
# ARGUMENT(S):
#
#  1) To block URL(s) with domain
#
#       ARGUMENT FORMAT: "<url_domain_1> <url_domain_2>"
#       EXAMPLE        : "amazon.com youtube.com"
#
#     The blocked URL(s) will be ["*://*.amazon.com/*", "*://*.youtube.com/*"]
#     All the urls from this domains will be blocked with the regex expression.
#
#           RETURN VALUE                  MEANING
#
#                0                    URL(s) blocked in Chrome successfully
#                1                    Chrome and chromium installed directory unknown
#                2                    Invalid Arguments
#
# IMPORTANT NOTE:
#   Existing policies will be over-written while deploying this script.
#   So, Only domains given as arguments while deploying the scripts will be blocked.
#   To see the script output, Kindly enable the option Enable logging in Troubleshooting while deploying configuration.

errorCode=2

errorFunc() {
  exit $errorCode
}

if [[ $euid -ne 0 ]]; then
  echo "ERROR : This script must be run as root."
  exit 1
fi

for i in 1; do

  if [ $# -ne 1 ]; then
    echo "Incorrect Usage : Arguments mismatch."
    echo "Refer ARGUMENT(S) section in the script."
    break
  fi

  me_block_policy="{
  \"URLBlocklist\": BLOCKEDURLSFILLER
}"

  urlBlocklist=("$@")
  blocked_urls_string="["

  for url in ${urlBlocklist[@]}; do
    blocked_urls_string="${blocked_urls_string}\"${url}\", "
  done

  blocked_urls_string=${blocked_urls_string%, *}
  blocked_urls_string="${blocked_urls_string}]"

  ######################################
  ### Enforcing the Policy in Chrome ###
  ######################################

  errorCode=0

  if [ -d "/etc/opt/chrome" ] || [ $(which google-chrome) ]; then
    echo "Chrome is installed. Enforcing Policy."
    mkdir -p "/etc/opt/chrome/policies/managed"
    rm -f /etc/opt/chrome/policies/managed/*.json
    echo "${me_block_policy/BLOCKEDURLSFILLER/"$blocked_urls_string"}" >/etc/opt/chrome/policies/managed/me_block_policy.json
  else
    errorCode=1
    echo "Chrome is not installed or installation directory is unknown."
  fi

  ########################################
  ### Enforcing the Policy in Chromium ###
  ########################################

  if [ -d "/etc/chromium" ] || [ $(which chromium-browser) ]; then
    echo "Chromium is installed. Enforcing Policy."
    mkdir -p "/etc/chromium/policies/managed"
    rm -f /etc/chromium/policies/managed/*.json
    echo "${me_block_policy/BLOCKEDURLSFILLER/"$blocked_urls_string"}" >/etc/chromium/policies/managed/me_block_policy.json
    if [[ "$errorCode" == 1 ]]; then
      errorCode=0
    fi
  else
    echo "Chromium is not installed or installation directory is unknown."
  fi

  if [[ "$errorCode" == 0 ]]; then
    echo "Blocked URLs : $blocked_urls_string"
  fi

done

errorFunc
