#!/usr/bin/bash
set -euo pipefail
IFS=$'\n\t'

CONFIGFILE='/usr/lib64/librenms/snmp/pi-hole.conf'

API_AUTH_KEY=""
API_URL="localhost/api"
URL_READ_ONLY="/stats/summary"
PICONFIGFILE='/etc/pihole/setupVars.conf'
DHCPLEASEFILE='/etc/pihole/dhcp.leases'

if [ -f $CONFIGFILE ]; then
	# shellcheck disable=SC1090
	. $CONFIGFILE
fi

# read in pi-hole variables for DHCP range
if [ -f $PICONFIGFILE ]; then
	# shellcheck disable=SC1090
	. $PICONFIGFILE
fi

#/ Description: BASH script to get Pi-hole stats
#/ Examples: ./pi-hole-stats.sh
#/ Options:
#/   --help: Display this help message
#/   --debug: Brief check of system env and script vars
usage() {
	grep '^#/' "$0" | cut -c4- ;
	exit 0 ;
}

debug() {
	if ! [ -x "$(command -v tr)" ]; then
		echo '[error] tr binary not available, please install it'
	else
		echo '[ok] tr bin';
	fi

	if ! [ -x "$(command -v jq)" ]; then
		echo '[error] jq binary not available, please install it'
	else
		echo '[ok] jq bin';
	fi

	if ! [ -x "$(command -v curl)" ]; then
		echo '[error] curl binary not available, please install it'
	else
		echo '[ok] curl bin'
	fi

	if [ -z "$API_URL" ]; then
		echo '[error] API_URL is not set'
	else
		echo '[ok] API_URL is set'
	fi

	if [ -z "$API_AUTH_KEY" ]; then
		echo '[warning] API_AUTH_KEY is not set, some values will not be available'
	else
		echo '[ok] API_AUTH_KEY is set'
	fi

	if [ -z "${URL_READ_ONLY}" ]; then
		echo '[error] URL_READ_ONLY is not set'
	else
		echo '[ok] URL_READ_ONLY is set'
	fi

	if [ -f $PICONFIGFILE ]; then
		echo '[ok] Pi-Hole config file exists, DHCP stats will be captured if scope active'
	else
		echo '[error] Pi-Hole config file does not exist, DHCP stats will not be captured if used'
	fi
	if [ -f $DHCPLEASEFILE ]; then
		echo '[ok] DHCP lease file exists, DHCP stats will be captured if scope active'
	else
		echo '[error] DHCP lease file does not exist, DHCP stats will not be captured if used'
	fi
}

exportdata() {
	SESSION_SID=""

	if ! [ -z "$API_AUTH_KEY" ]; then
		SESSION_SID=$(curl -X POST --data "{\"password\":\"${API_AUTH_KEY}\"}" -s "${API_URL}/auth" | jq -r '.session.sid')
	fi

	# domains_being_blocked / dns_query_total / ads_blocked_today / ads_percentage_today
	# unique_domains / queries_forwarded / queries_cached / A / AAAA / PTR / SRV
	GET_STATS=$(curl -H "X-FTL-SID: ${SESSION_SID}" -s "${API_URL}${URL_READ_ONLY}" | jq '.gravity.domains_being_blocked, .queries.total, .queries.blocked, .queries.percent_blocked, .queries.unique_domains, .queries.forwarded, .queries.cached, .queries.types.A, .queries.types.AAAA, .queries.types.PTR, .queries.types.SRV')
	echo "$GET_STATS" | tr " " "\n"

	# Find number of DHCP address in scope and current lease count
	# case-insensitive compare, just in case :)
	if [ -n "${DHCP_ACTIVE+x}" ] && [ "${DHCP_ACTIVE,,}" = "true" ]; then
		# Max IP addresses in scope
		# Convert IPs to decimal and subtract
		IFS="." read -r -a array <<< "$DHCP_START"
		DHCPSTARTDECIMAL=$(( (array[0]*256**3) + (array[1]*256**2) + (array[2]*256) + array[3] ))
		IFS="." read -r -a array <<< "$DHCP_END"
		DHCPENDDECIMAL=$(( (array[0]*256**3) + (array[1]*256**2) + (array[2]*256) + array[3] ))
		echo $(( DHCPENDDECIMAL - DHCPSTARTDECIMAL ))
		# Current lease count
		wc -l < ${DHCPLEASEFILE}
	else
		echo 0
		echo 0
	fi
}

if [ -z "$*" ]; then
	exportdata
fi
expr "$*" : ".*--help" > /dev/null && usage
expr "$*" : ".*--debug" > /dev/null && debug
