#!/bin/bash
#
# Common Helper script utilities for Ubiquiti UniFi Network
#

# General parameters
NAME="unifi"
BASEDIR="/usr/lib/unifi"

# Constants

### Common helper functions ###

function does_not_have_ubnt_tools() {
	if [[ ! -f /sbin/ubnt-tools ]]; then
		return 0
	else
		return 1
	fi
}

function log() {
	if does_not_have_ubnt_tools; then
		logger -s -t "${NAME}" "${@}"
	else
		logger -s -t "${NAME}" -p 3 "${@}"
	fi
}

function log_unifi_service_cpu_time() {
	local serviceProcessPid=$(systemctl show --property MainPID --value unifi)
	local serviceProcessTime=$(ps -p ${serviceProcessPid} -o time --no-headers)
	local cpuTimeMsg="Consumed CPU time during 'unifi.service' startup: ${serviceProcessTime}"
	if [[ ! -f "${BASEDIR}/logs/startup.log" ]]; then
		log "${cpuTimeMsg}"
	else
		echo "${cpuTimeMsg}" >> "${BASEDIR}/logs/startup.log"
	fi
}