#!/bin/sh -e
#
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
# This explorer determines the path of dma's auth.conf file

# No dma.conf -> use default
test -f /etc/dma/dma.conf || {
	echo /etc/dma/auth.conf
	exit 0
}
test -r /etc/dma/dma.conf || {
	echo 'Cannot read /etc/dma/dma.conf' >&2
	exit 1
}

# Get AUTHPATH from dma.conf
awk -F'[ \t]' '
{
	sub(/#.*$/, "", $0)  # remove comments
	if (!$0) next  # ignore empty lines
}
$1 == "AUTHPATH" {
	# Store authpath. In dma conf parsing last wins.
	if ($2) authpath = substr($0, index($0, " ") + 1)
}
END {
	if (authpath) {
		print authpath
		exit 0
	} else exit 1
}
' /etc/dma/dma.conf \
|| echo /etc/dma/auth.conf  # default
