1268999Sdteskeif [ ! "$_PACKAGES_MUSTHAVEPKG_SUBR" ]; then _PACKAGES_MUSTHAVEPKG_SUBR=1
2268999Sdteske#
3294865Sdteske# Copyright (c) 2014-2016 Devin Teske
4268999Sdteske# All rights reserved.
5268999Sdteske#
6268999Sdteske# Redistribution and use in source and binary forms, with or without
7268999Sdteske# modification, are permitted provided that the following conditions
8268999Sdteske# are met:
9268999Sdteske# 1. Redistributions of source code must retain the above copyright
10268999Sdteske#    notice, this list of conditions and the following disclaimer.
11268999Sdteske# 2. Redistributions in binary form must reproduce the above copyright
12268999Sdteske#    notice, this list of conditions and the following disclaimer in the
13268999Sdteske#    documentation and/or other materials provided with the distribution.
14268999Sdteske#
15268999Sdteske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16268999Sdteske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17268999Sdteske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18268999Sdteske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19268999Sdteske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20268999Sdteske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21268999Sdteske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22268999Sdteske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23268999Sdteske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24268999Sdteske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25268999Sdteske# SUCH DAMAGE.
26268999Sdteske#
27268999Sdteske# $FreeBSD: releng/11.0/usr.sbin/bsdconfig/share/packages/musthavepkg.subr 294865 2016-01-27 00:09:53Z dteske $
28268999Sdteske#
29268999Sdteske############################################################ INCLUDES
30268999Sdteske
31268999SdteskeBSDCFG_SHARE="/usr/share/bsdconfig"
32268999Sdteske. $BSDCFG_SHARE/common.subr || exit 1
33268999Sdteskef_dprintf "%s: loading includes..." packages/musthavepkg.subr
34268999Sdteskef_include $BSDCFG_SHARE/dialog.subr
35268999Sdteskef_include $BSDCFG_SHARE/mustberoot.subr
36268999Sdteske
37268999Sdteske############################################################ FUNCTIONS
38268999Sdteske
39268999Sdteske# f_musthavepkg_init
40268999Sdteske#
41268999Sdteske# Validate pkg(8) is installed and set $PKG_ABI global if not already set.
42268999Sdteske# Returns success unless pkg(8) is not installed and user refuses to install
43268999Sdteske# it (upon prompt when running interactively).
44268999Sdteske#
45268999Sdteskef_musthavepkg_init()
46268999Sdteske{
47268999Sdteske	local funcname=f_musthavepkg_init
48294865Sdteske	local pkg_abi_awk=' # BEGIN-AWK
49294865Sdteske		$1 ~ /^ABI/ && $0 = $NF, sub(/^"/, "") && sub(/".*/, "") {
50294865Sdteske			print; found = 1; exit
51294865Sdteske		} END { exit ! found }
52294865Sdteske	' # END-AWK
53268999Sdteske
54268999Sdteske	if [ "$PKG_ABI" ]; then # Already set
55268999Sdteske		f_dprintf "PKG_ABI=[%s]" "$PKG_ABI"
56268999Sdteske		export PKG_ABI
57268999Sdteske		f_quietly pkg -N -vv # return status (pkg(8) functional?)
58268999Sdteske		return $?
59268999Sdteske	fi
60268999Sdteske
61268999Sdteske	# Attempt to get PKG_ABI without prematurely bootstrapping pkg(8)
62268999Sdteske	if f_eval_catch -k PKG_ABI $funcname pkg \
63268999Sdteske		"pkg -N -vv | awk '%s'" "$pkg_abi_awk"
64268999Sdteske	then
65268999Sdteske		f_dprintf "PKG_ABI=[%s]" "$PKG_ABI"
66268999Sdteske		export PKG_ABI
67268999Sdteske		return $SUCCESS
68268999Sdteske	fi
69268999Sdteske
70268999Sdteske	# pkg(8) not yet bootstrapped; ask for permission unless nonInteractive
71268999Sdteske	f_dialog_yesno "$msg_pkg_not_yet_installed_install_now" ||
72268999Sdteske		f_die 1 "$msg_must_have_pkg_to_execute" "$pgm"
73268999Sdteske
74268999Sdteske	f_mustberoot_init # Have to be root to install pkg(8)
75268999Sdteske
76268999Sdteske	# Bootstrap pkg(8)
77268999Sdteske	f_dialog_info "$msg_bootstrapping_pkg"
78268999Sdteske	f_eval_catch -k PKG_ABI $funcname pkg \
79268999Sdteske		"ASSUME_ALWAYS_YES=1 pkg -vv | awk '%s'" "$pkg_abi_awk" ||
80268999Sdteske		f_die 1 "$msg_must_have_pkg_to_execute" "$pgm"
81268999Sdteske
82268999Sdteske	f_dprintf "PKG_ABI=[%s]" "$PKG_ABI"
83268999Sdteske	export PKG_ABI
84268999Sdteske	return $SUCCESS
85268999Sdteske}
86268999Sdteske
87268999Sdteske############################################################ MAIN
88268999Sdteske
89268999Sdteskef_dprintf "%s: Successfully loaded." packages/musthavepkg.subr
90268999Sdteske
91268999Sdteskefi # ! $_PACKAGES_MUSTHAVEPKG_SUBR
92