# 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_Homebrew_checkFunction()
{
	if core_compatibility_whichNoOutput brew; then
		core_dependency_packageManager="Homebrew"
		return 0
	fi
	return 1
}
core_functions_register _core_dependency_check_functions core_dependency_check_Homebrew_checkFunction

core_dependency_check_Homebrew_extractUnqualifiedPackageName()
{
	local fullyQualifiedPackageName="$1"
	core_compatibility_basename "$fullyQualifiedPackageName"
}

core_dependency_requires Homebrew brew
core_dependency_check_Homebrew_packageManager()
{
	# We can't cache this in an array as it changes on every loop
	_core_dependency_check_Homebrew_packageManager_tapExists()
	{
		local existingTap
		brew tap | while IFS='' read -r existingTap
		do
			if [ "$existingTap" = "$tap" ]; then
				return 0
			fi
		done
		return 1
	}
	
	local packageToInstall
	local package
	_core_dependency_check_Homebrew_packageManager_installTaps()
	{
		local tap
		for packageToInstall in "$@"
		do
			tap="$(core_compatibility_dirname "$packageToInstall")"
			if [ "$tap" = '.' ]; then
				continue
			fi
			if _core_dependency_check_Homebrew_packageManager_tapExists; then
				continue
			fi
			brew tap "$tap" 1>/dev/null 2>/dev/null
		done
	}
	_core_dependency_check_Homebrew_packageManager_installTaps "$@"

	core_message NOTICE "Updating Homebrew package manager (may take some time)"
	brew update
	
	if core_variable_isSet core_dependency_check_forcePackageManagerUpgradeDuringInstall; then
		if [ $core_dependency_check_forcePackageManagerUpgradeDuringInstall -eq 1 ]; then
			core_message INFO "Upgrading Homebrew package manager (may take some time)"
			brew upgrade
		fi
	fi
	
	for packageToInstall in "$@"
	do
		package="$(core_compatibility_basename "$packageToInstall")"
		if brew --prefix "$package" 1>/dev/null 2>/dev/null; then
			local checkFolderPath="$(brew --prefix "$package")"
			if [ -d "$checkFolderPath" ]; then
				continue
			fi
		fi
		
		brew install "$packageToInstall"
	done
	
	# fakeroot required a  brew link
}

core_dependency_check_Homebrew_installPackageManagerIfRequired()
{
	if core_compatibility_whichNoOutput brew; then
		return 0
	fi

	core_message NOTICE "Installing Homebrew package manager"
	ruby -e "$(curl -fsSL 'https://raw.githubusercontent.com/Homebrew/install/master/install')"

	core_message INFO "Doctoring (sic) Homebrew package manager"
	brew doctor
	
	# check is /usr/local/{s}bin is on the path
}
