# This file is part of shellfire core. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT. No part of shellfire core, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of shellfire core. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/shellfire-dev/core/master/COPYRIGHT.


core_dependency_check_FreeBSD_checkFunction()
{
	if [ -x /bin/freebsd-version ]; then
		return 0
	fi
	return 1
}
core_functions_register _core_dependency_check_functions core_dependency_check_FreeBSD_checkFunction

core_dependency_check_FreeBSD_extractUnqualifiedPackageName()
{
	printf '%s' "$1"
}

core_dependency_requires FreeBSD id pkg
core_dependency_check_FreeBSD_packageManager()
{
	local ourCurrentId="$(id -u)"
	
	local pkgPath
	if core_compatibility_whichNoOutput pkg; then
		pkgPath='pkg'
	elif [ ! -x /usr/sbin/pkg ]; then
		pkgPath=/usr/sbin/pkg
	else
		core_message INFO "Please bootstrap FreeBSD's pkg manager by running /usr/sbin/pkg"
		return 0
	fi
	
	if [ "$ourCurrentId" = 0 ]; then
		"$pkgPath" install -q -y "$@"
	else
		local sudoCommandPath
		if core_compatibility_whichNoOutput sudo; then
			sudoCommandPath=sudo
		elif [ -x /usr/local/bin/sudo ]; then
			sudoCommandPath=/usr/local/bin/sudo
		else
			core_message INFO "Not running as root and sudo not found, so not installing packages using '$pkgPath'"
			return 0
		fi
		
		"$sudoCommandPath" "$pkgPath" install -q -y "$@"
	fi
}
