#!/bin/bash
# Pi-hole: A black hole for Internet advertisements
# (c) 2017 Pi-hole, LLC (https://pi-hole.net)
# Network-wide ad blocking via your own hardware.
#
# Controller for all pihole scripts and functions.
#
# This file is copyright under the latest version of the EUPL.
# Please see LICENSE file for your rights under this license.

readonly PI_HOLE_SCRIPT_DIR="/opt/pihole"

readonly wildcardlist="/etc/dnsmasq.d/03-pihole-wildcard.conf"
# Must be root to use this tool
if [[ ! $EUID -eq 0 ]];then
  if [ -x "$(command -v sudo)" ];then
    exec sudo bash "$0" "$@"
    exit $?
  else
    echo "::: sudo is needed to run pihole commands.  Please run this script as root or install sudo."
    exit 1
  fi
fi

webpageFunc() {
  source /opt/pihole/webpage.sh
  main "$@"
  exit 0
}

whitelistFunc() {
 "${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
  exit 0
}

blacklistFunc() {
  "${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
  exit 0
}

wildcardFunc() {
  "${PI_HOLE_SCRIPT_DIR}"/list.sh "$@"
  exit 0
}

debugFunc() {
  local automated
  local web

  # Pull off the `debug` leaving passed call augmentation flags in $1
  shift
  if [[ "$@" == *"-a"* ]]; then
    automated="true"
  fi
  if [[ "$@" == *"-w"* ]]; then
    web="true"
  fi

  AUTOMATED=${automated:-} WEBCALL=${web:-} "${PI_HOLE_SCRIPT_DIR}"/piholeDebug.sh
  exit 0
}

flushFunc() {
  "${PI_HOLE_SCRIPT_DIR}"/piholeLogFlush.sh
  exit 0
}

updatePiholeFunc() {
  "${PI_HOLE_SCRIPT_DIR}"/update.sh
  exit 0
}

reconfigurePiholeFunc() {
  /etc/.pihole/automated\ install/basic-install.sh --reconfigure
  exit 0;
}

updateGravityFunc() {
  "${PI_HOLE_SCRIPT_DIR}"/gravity.sh "$@"
  exit 0
}

scanList(){
  domain="${1}"
  list="${2}"
  method="${3}"
  if [[ ${method} == "-exact" ]] ; then
    grep -i -E "(^|\s)${domain}($|\s)" "${list}"
  else
    grep -i "${domain}" "${list}"
  fi
}

processWildcards() {
  IFS="." read -r -a array <<< "${1}"
  for (( i=${#array[@]}-1; i>=0; i-- )); do
    ar=""
    for (( j=${#array[@]}-1; j>${#array[@]}-i-2; j-- )); do
      if [[ $j == $((${#array[@]}-1)) ]]; then
        ar="${array[$j]}"
      else
        ar="${array[$j]}.${ar}"
      fi
    done
    echo "${ar}"
  done
}

queryFunc() {
  domain="${2}"
  method="${3}"
  lists=( /etc/pihole/list.* /etc/pihole/blacklist.txt)
  for list in ${lists[@]}; do
    if [ -e "${list}" ]; then
      result=$(scanList ${domain} ${list} ${method})
      # Remove empty lines before couting number of results
      count=$(sed '/^\s*$/d' <<< "$result" | wc -l)
      echo "::: ${list} (${count} results)"
      if [[ ${count} > 0 ]]; then
        echo "${result}"
      fi
      echo ""
    else
      echo "::: ${list} does not exist"
      echo ""
    fi
  done

  # Scan for possible wildcard matches
  if [ -e "${wildcardlist}" ]; then
    local wildcards=($(processWildcards "${domain}"))
    for domain in ${wildcards[@]}; do
      result=$(scanList "\/${domain}\/" ${wildcardlist})
      # Remove empty lines before couting number of results
      count=$(sed '/^\s*$/d' <<< "$result" | wc -l)
      if [[ ${count} > 0 ]]; then
        echo "::: Wildcard blocking ${domain} (${count} results)"
        echo "${result}"
        echo ""
      fi
    done
  fi
  exit 0
}

chronometerFunc() {
  shift
  "${PI_HOLE_SCRIPT_DIR}"/chronometer.sh "$@"
  exit 0
}


uninstallFunc() {
  "${PI_HOLE_SCRIPT_DIR}"/uninstall.sh
  exit 0
}

versionFunc() {
  shift
  "${PI_HOLE_SCRIPT_DIR}"/version.sh "$@"
  exit 0
}

restartDNS() {
  dnsmasqPid=$(pidof dnsmasq)
  if [[ ${dnsmasqPid} ]]; then
    # service already running - reload config
    if [ -x "$(command -v systemctl)" ]; then
      systemctl restart dnsmasq
    else
      service dnsmasq restart
    fi
  else
    # service not running, start it up
    if [ -x "$(command -v systemctl)" ]; then
      systemctl start dnsmasq
    else
      service dnsmasq start
    fi
  fi
}

piholeEnable() {
  if [[ "${1}" == "0" ]] ; then
    #Disable Pihole
    sed -i 's/^addn-hosts=\/etc\/pihole\/gravity.list/#addn-hosts=\/etc\/pihole\/gravity.list/' /etc/dnsmasq.d/01-pihole.conf
    echo "::: Blocking has been disabled!"
    if [[ $# > 1 ]] ; then
      if [[ ${2} == *"s"* ]] ; then
        tt=${2%"s"}
        echo "::: Blocking will be re-enabled in ${tt} seconds"
        nohup bash -c "sleep ${tt}; pihole enable" </dev/null &>/dev/null &
      elif [[ ${2} == *"m"* ]] ; then
        tt=${2%"m"}
        echo "::: Blocking will be re-enabled in ${tt} minutes"
        tt=$((${tt}*60))
        nohup bash -c "sleep ${tt}; pihole enable" </dev/null &>/dev/null &
      else
        echo "::: Unknown format for delayed reactivation of the blocking!"
        echo "::: Example:"
        echo ":::    pihole disable 5s    -    will disable blocking for 5 seconds"
        echo ":::    pihole disable 7m    -    will disable blocking for 7 minutes"
        echo "::: Blocking will not automatically be re-enabled!"
      fi
    fi
  else
    #Enable pihole
    echo "::: Blocking has been enabled!"
    sed -i 's/^#addn-hosts/addn-hosts/' /etc/dnsmasq.d/01-pihole.conf
  fi
  restartDNS
}

piholeLogging() {
  shift

  if [[ "${1}" == "off" ]] ; then
    #Disable Logging
    sed -i 's/^log-queries/#log-queries/' /etc/dnsmasq.d/01-pihole.conf
    sed -i 's/^QUERY_LOGGING=true/QUERY_LOGGING=false/' /etc/pihole/setupVars.conf
    pihole -f
    echo "::: Logging has been disabled!"
  elif [[ "${1}" == "on" ]] ; then
    #Enable logging
    sed -i 's/^#log-queries/log-queries/' /etc/dnsmasq.d/01-pihole.conf
    sed -i 's/^QUERY_LOGGING=false/QUERY_LOGGING=true/' /etc/pihole/setupVars.conf
    echo "::: Logging has been enabled!"
  else
    echo "::: Invalid option passed, please pass 'on' or 'off'"
    exit 1
  fi
  restartDNS
}

piholeStatus() {
  if [[ $(netstat -plnt | grep -c ':53 ') > 0 ]]; then
    if [[ "${1}" != "web" ]] ; then
      echo "::: DNS service is running"
    fi
  else
    if [[ "${1}" == "web" ]] ; then
      echo "-1";
    else
      echo "::: DNS service is NOT running"
    fi
    return
  fi

  if [[ $(grep -i "^#addn-hosts=/" /etc/dnsmasq.d/01-pihole.conf) ]] ; then
    #list is commented out
    if [[ "${1}" == "web" ]] ; then
      echo 0;
    else
      echo "::: Pi-hole blocking is Disabled";
    fi
  elif [[ $(grep -i "^addn-hosts=/" /etc/dnsmasq.d/01-pihole.conf) ]] ; then
    #list set
    if [[ "${1}" == "web" ]] ; then
      echo 1;
    else
      echo "::: Pi-hole blocking is Enabled";
    fi
  else
    #addn-host not found
    if [[ "${1}" == "web" ]] ; then
      echo 99
    else
      echo ":::  No hosts file linked to dnsmasq, adding it in enabled state"
    fi
    #add addn-host= to dnsmasq
    echo "addn-hosts=/etc/pihole/gravity.list" >> /etc/dnsmasq.d/01-pihole.conf
    restartDNS
  fi
}

tailFunc() {
  echo "Press Ctrl-C to exit"
  tail -F /var/log/pihole.log
  exit 0
}

piholeCheckoutFunc() {
  source "${PI_HOLE_SCRIPT_DIR}"/piholeCheckout.sh
  shift
  checkout "$@"
}

helpFunc() {
	cat << EOM
::: Control all PiHole specific functions!
:::
::: Usage: pihole [options]
:::		Add -h after -w (whitelist), -b (blacklist), -c (chronometer), or -a (admin)  for more information on usage
:::
::: Options:
:::  -w, whitelist            Whitelist domain(s)
:::  -b, blacklist            Blacklist domain(s) (exact match)
:::  -wild, wildcard          Blacklist whole domain(s) (wildcard)
:::  -d, debug                Start a debugging session
:::                             Automated debugging can be enabled with `-a`.
:::                             'pihole -d -a'
:::  -f, flush                Flush the 'pihole.log' file
:::  -t, tail                 Output the last lines of the 'pihole.log' file. Lines are appended as the file grows
:::  -up, updatePihole        Update Pi-hole components
:::  -r, reconfigure          Reconfigure or Repair Pi-hole
:::  -g, updateGravity        Update the list of ad-serving domains
:::  -c, chronometer          Calculates stats and displays to an LCD
:::  -h, help                 Show this help dialog
:::  -v, version              Show installed versions of Pi-Hole and Web-Admin
:::  -q, query                Query the adlists for a specific domain
:::                             'pihole -q domain -exact' shows exact matches only
:::  -l, logging              Enable or Disable logging (pass 'on' or 'off')
:::  -a, admin                Admin webpage options
:::  uninstall                Uninstall Pi-Hole from your system :(!
:::  status                   Is Pi-Hole Enabled or Disabled
:::  enable                   Enable Pi-Hole DNS Blocking
:::  disable                  Disable Pi-Hole DNS Blocking
:::                             Blocking can also be disabled only temporarily, e.g.,
:::                             'pihole disable 5m' - will disable blocking for 5 minutes
:::  restartdns               Restart dnsmasq
:::  checkout                 Check out different branches
EOM
  exit 0
}

if [[ $# = 0 ]]; then
  helpFunc
fi

# Handle redirecting to specific functions based on arguments
case "${1}" in
  "-w" | "whitelist"            ) whitelistFunc "$@";;
  "-b" | "blacklist"            ) blacklistFunc "$@";;
  "-wild" | "wildcard"          ) wildcardFunc "$@";;
  "-d" | "debug"                ) debugFunc "$@";;
  "-f" | "flush"                ) flushFunc;;
  "-up" | "updatePihole"        ) updatePiholeFunc;;
  "-r"  | "reconfigure"         ) reconfigurePiholeFunc;;
  "-g" | "updateGravity"        ) updateGravityFunc "$@";;
  "-c" | "chronometer"          ) chronometerFunc "$@";;
  "-h" | "help"                 ) helpFunc;;
  "-v" | "version"              ) versionFunc "$@";;
  "-q" | "query"                ) queryFunc "$@";;
  "-l" | "logging"              ) piholeLogging "$@";;
  "uninstall"                   ) uninstallFunc;;
  "enable"                      ) piholeEnable 1;;
  "disable"                     ) piholeEnable 0 $2;;
  "status"                      ) piholeStatus "$2";;
  "restartdns"                  ) restartDNS;;
  "-a" | "admin"                ) webpageFunc "$@";;
  "-t" | "tail"                 ) tailFunc;;
  "checkout"                    ) piholeCheckoutFunc "$@";;
  *                             ) helpFunc;;
esac