#!/bin/bash -euE
#!/bin/bash
#   Copyright (c) 2012-2013 by Luke Shumaker <lukeshu@sbcglobal.net>
#
#   This program 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.
#
#   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

INCLUDE_CONF_SH=conf.sh

LIBREUSER="${SUDO_USER:-$USER}"
LIBREHOME="$(eval echo ~$LIBREUSER)"
if [[ -z ${XDG_CONFIG_HOME:-} ]]; then
	export XDG_CONFIG_HOME="${LIBREHOME}/.config"
fi
if [[ -z ${XDG_CACHE_HOME:-} ]]; then
	export XDG_CACHE_HOME="${LIBREHOME}/.cache"
fi

# Generic functions ############################################################

# Usage: list_files $slug
# Lists the configuration files to be considered for $slug.
# Later files should take precedence over earlier files.
list_files() {
	local slug=$1
	case $slug in
		abs)
			echo /etc/$slug.conf
			echo "$LIBREHOME/.$slug.conf"
			;;
		makepkg)
			if [[ $MAKEPKG_CONF != /etc/$slug.conf && -r $MAKEPKG_CONF ]]; then
				echo "$MAKEPKG_CONF"
			else
				echo /etc/$slug.conf
				echo "$LIBREHOME/.$slug.conf"
			fi
			;;
		libretools)
			echo /etc/$slug.conf
			echo "$XDG_CONFIG_HOME/libretools/$slug.conf"
			;;
		*)
			echo /etc/libretools.d/$slug.conf
			echo "$XDG_CONFIG_HOME/libretools/$slug.conf"
			;;
	esac
}

# Usage: load_files $slug
# Loads the configuration files for $slug in the proper order.
load_files() {
	for file in $(list_files $1); do
		if [[ -r $file ]]; then
			. "$file"
		fi
	done
}

# Usage: check_vars $slug VAR1 VAR2...
# Check whether the variables listed are properly set.
# If not, it prints a message saying to set them in the configuration file(s)
# for $slug.
check_vars() {
	local slug=$1
	shift
	local ret=0
	for VAR in "$@"; do
		if [[ -z ${!VAR:-} ]]; then
			if [[ $(list_files $slug|wc -l) > 1 ]]; then
				echo "Configure '$VAR' in one of:"
				list_files $slug | sed 's/./  -> &/'
			else
				echo "Configure '$VAR' in $(list_files $slug)"
			fi
			ret=1
		fi
	done >&2
	if [[ $ret != 0 ]]; then
		return 1
	fi
}

# makepkg configuration ########################################################

[[ -n ${MAKEPKG_CONF:-} ]] || MAKEPKG_CONF=/etc/makepkg.conf

load_conf_makepkg() {
	load_files makepkg
}

# Usage: get_conf_makepkg <var_name> <default_value>
get_conf_makepkg() (
	set +euE
	local setting=$1
	local default=$2
	load_conf_makepkg
	printf '%s\n' "${!setting:-${default}}"
)

set_conf_makepkg() {
	local key=$1
	local val=$2
	for file in $(list_files makepkg|tac); do
		if [[ -w $file ]]; then
			sed -i "/^\s*$key=/d" "$file"
			echo "$key='$val'" >> "$file"
			return 0
		fi
	done
	return 1
}

# libretools configuration #####################################################

check_conf_libretools() {
	check_vars libretools \
		PARABOLAHOST LIBREDESTDIR BLACKLIST WORKDIR REPOS ARCHES \
		ABSLIBREGIT DIFFTOOL FULLBUILDCMD SIGEXT SIGID
}

load_conf_libretools() {
	load_files libretools
	check_conf_libretools
}

load_conf_libretools_chroot() {
	load_files chroot
	# Exclude CHROOTEXTRAPKG from the checks because an empty value is valid
	if [[ -f /.arch-chroot ]]; then
		# inside of a chroot, only CHROOTEXTRAPKG needs to be set, but
		# as stated above, we don't check for it.
		:
	else
		check_vars chroot CHROOTDIR CHROOT
	fi
}

load_conf_libretools_librefetch() {
	load_files librefetch
	check_vars librefetch MIRROR DOWNLOADER
}