# 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_NetBSD_checkFunction()
{
	if [ ! -f /etc/release ]; then
		return 1
	fi
	
	local operatingSystem
	local versionSlashArchitecture
	IFS=' ' read -r operatingSystem versionSlashArchitecture </etc/release
	
	# To work correctly, this needs to be the PATH
	# PATH=/usr/pkg/bin:/usr/pkg/sbin:"$PATH"
	if [ "$operatingSystem" = 'NetBSD' ]; then
		core_dependency_packageManager='NetBSD'
		return 0
	fi
	return 1
}
core_functions_register _core_dependency_check_functions core_dependency_check_NetBSD_checkFunction

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

core_dependency_requires NetBSD id
core_dependency_check_NetBSD_packageManager()
{
	local ourCurrentId="$(id -u)"

	if core_compatibility_whichNoOutput pkgin; then
		core_dependency_check_NetBSD_packageManager_pkgin pkgin "$@"
	elif [ -x /usr/pkg/bin/pkgin ]; then
		core_dependency_check_NetBSD_packageManager_pkgin /usr/pkg/bin/pkgin "$@"
	elif core_compatibility_whichNoOutput pkg_add; then
		core_dependency_check_NetBSD_packageManager_pkgSrc pkg_add "$@"
	elif [ -x /usr/pkg/sbin/pkg_add ]; then
		core_dependency_check_NetBSD_packageManager_pkgSrc /usr/pkg/sbin/pkg_add "$@"
	else
		core_message INFO "No package manager (pkgin or pkg_add) found, so not installing packages"
	fi
}

core_dependency_requires NetBSD uname cut sudo pkgin
core_dependency_check_NetBSD_packageManager_pkgin()
{
	local pathToPkgin="$1"
	shift 1
	
	{
		if [ ! -s /usr/pkg/etc/pkgin/repositories.conf ]; then
			if core_variable_isUnset PKG_REPOS; then
				export PKG_REPOS="https://pub.allbsd.org/pub/pkgsrc/packages/$(uname -s)/$(uname -m)/$(uname -r|cut -f '1 2' -d.)/All"
			fi
		fi
	
		if [ "$ourCurrentId" = 0 ]; then
			"$pathToPkgin" -y update
			"$pathToPkgin" -y install "$@"
		else
			local sudoCommandPath
			if core_compatibility_whichNoOutput sudo; then
				sudoCommandPath=sudo
			elif [ -x /usr/pkg/bin/sudo ]; then
				sudoCommandPath=/usr/pkg/bin/sudo
			else
				core_message INFO "Not running as root and sudo not found, so not installing packages using pkgin"
				return 0
			fi
			
			if core_variable_isUnset PKG_REPOS; then
				"$sudoCommandPath" -p "Please enter the password for %p for sudo to update the package list using pkgin: " "$pathToPkgin" -y update
				"$sudoCommandPath" -p "Please enter the password for %p for sudo to install packages using pkgin: " "$pathToPkgin" -y install "$@"
			else
				"$sudoCommandPath" -p "Please enter the password for %p for sudo to update the package list using pkgin: " "$pathToPkgin" -y update
				"$sudoCommandPath" -p "Please enter the password for %p for sudo to install packages using pkgin: " env PKG_REPOS="$PKG_REPOS" "$pathToPkgin" -y install "$@"
			fi
		fi
	}
}

core_dependency_requires NetBSD uname cut sudo pkg_add env
core_dependency_check_NetBSD_packageManager_pkgSrc()
{
	local pathToPkgAdd="$1"
	shift 1
	
	{
		if core_variable_isUnset PKG_PATH; then
			# Weird breakages, try ftp.allbsd.org instead
			# export PKG_PATH="http://ftp.NetBSD.org/pub/pkgsrc/packages/$(uname -s)/$(uname -m)/$(uname -r|cut -f '1 2' -d.)/All"
			export PKG_PATH="https://pub.allbsd.org/pub/pkgsrc/packages/$(uname -s)/$(uname -m)/$(uname -r|cut -f '1 2' -d.)/All"
		fi
	
		if [ "$ourCurrentId" = 0 ]; then
			"$pathToPkgAdd" "$@"
		else
			if core_compatibility_whichNoOutput sudo; then
				sudo -p "Please enter the password for %p for sudo to install packages using pkg_add: " env PKG_PATH="$PKG_PATH" "$pathToPkgAdd" "$@"
			elif [ -x /usr/pkg/bin/sudo ]; then
				/usr/pkg/bin/sudo -p "Please enter the password for %p for sudo to install packages using pkg_add: " env PKG_PATH="$PKG_PATH" "$pathToPkgAdd" "$@"
			else
				core_message INFO "Not running as root and sudo not found, so not installing packages using pkg_add"
				return 0
			fi
		fi
	}
}
