#!/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 Firefox and Firefox-ESR
#
# 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 Firefox successfully
#                1             Firefox and Firefox-ESR 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
blocked_urls=""

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

  urlBlocklist=("$@")

  firefox_policy="{
    \"policies\": {
        \"WebsiteFilter\": {
            \"Block\": BLOCKEDURLSFILLER
        }
    }
}"

  ##################################
  ### Blocked URLs Configuration ###
  ##################################
  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 Firefox ###
  # #######################################

  # Trying all known firefox installation directories.
  errorCode=0

  if [[ -d "/usr/lib/firefox" ]]; then
    echo "Firefox is installed in /usr/lib/firefox. Enforcing Policy."
    mkdir -p /usr/lib/firefox/distribution
    echo "${firefox_policy/BLOCKEDURLSFILLER/"$blocked_urls_string"}" >/usr/lib/firefox/distribution/policies.json
  elif [[ -d "/usr/lib64/firefox" ]]; then
    echo "Firefox is installed in /usr/lib64/firefox. Enforcing Policy."
    mkdir -p /usr/lib64/firefox/distribution
    echo "${firefox_policy/BLOCKEDURLSFILLER/"$blocked_urls_string"}" >/usr/lib64/firefox/distribution/policies.json
  else
    errorCode=1
    echo "Firefox is not installed or installation directory is unknown."
  fi

  if [[ -d "/usr/lib/firefox_esr" ]]; then
    echo "Firefox is installed in /usr/lib/firefox_esr. Enforcing Policy."
    mkdir -p /usr/lib/firefox_esr/distribution
    echo "${firefox_policy/BLOCKEDURLSFILLER/"$blocked_urls_string"}" >/usr/lib/firefox_esr/distribution/policies.json
    if [[ "$errorCode" == 1 ]]; then
      errorCode=0
    fi
  elif [[ -d "/usr/lib64/firefox_esr" ]]; then
    echo "Firefox is installed in /usr/lib64/firefox_esr. Enforcing Policy."
    mkdir -p /usr/lib64/firefox_esr/distribution
    echo "${firefox_policy/BLOCKEDURLSFILLER/"$blocked_urls_string"}" >/usr/lib64/firefox_esr/distribution/policies.json
    if [[ "$errorCode" == 1 ]]; then
      errorCode=0
    fi
  else
    echo "Firefox ESR is not installed or installation directory is unknown."
  fi

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

errorFunc
