musthavepkg.subr revision 268999
1268999Sdteskeif [ ! "$_PACKAGES_MUSTHAVEPKG_SUBR" ]; then _PACKAGES_MUSTHAVEPKG_SUBR=1
2268999Sdteske#
3268999Sdteske# Copyright (c) 2014 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: head/usr.sbin/bsdconfig/share/packages/musthavepkg.subr 268999 2014-07-22 23:10:12Z 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
48268999Sdteske	local pkg_abi_awk='$1~/^ABI/{print $NF; exit}'
49268999Sdteske
50268999Sdteske	if [ "$PKG_ABI" ]; then # Already set
51268999Sdteske		f_dprintf "PKG_ABI=[%s]" "$PKG_ABI"
52268999Sdteske		export PKG_ABI
53268999Sdteske		f_quietly pkg -N -vv # return status (pkg(8) functional?)
54268999Sdteske		return $?
55268999Sdteske	fi
56268999Sdteske
57268999Sdteske	# Attempt to get PKG_ABI without prematurely bootstrapping pkg(8)
58268999Sdteske	if f_eval_catch -k PKG_ABI $funcname pkg \
59268999Sdteske		"pkg -N -vv | awk '%s'" "$pkg_abi_awk"
60268999Sdteske	then
61268999Sdteske		f_dprintf "PKG_ABI=[%s]" "$PKG_ABI"
62268999Sdteske		export PKG_ABI
63268999Sdteske		return $SUCCESS
64268999Sdteske	fi
65268999Sdteske
66268999Sdteske	# pkg(8) not yet bootstrapped; ask for permission unless nonInteractive
67268999Sdteske	f_dialog_yesno "$msg_pkg_not_yet_installed_install_now" ||
68268999Sdteske		f_die 1 "$msg_must_have_pkg_to_execute" "$pgm"
69268999Sdteske
70268999Sdteske	f_mustberoot_init # Have to be root to install pkg(8)
71268999Sdteske
72268999Sdteske	# Bootstrap pkg(8)
73268999Sdteske	f_dialog_info "$msg_bootstrapping_pkg"
74268999Sdteske	f_eval_catch -k PKG_ABI $funcname pkg \
75268999Sdteske		"ASSUME_ALWAYS_YES=1 pkg -vv | awk '%s'" "$pkg_abi_awk" ||
76268999Sdteske		f_die 1 "$msg_must_have_pkg_to_execute" "$pgm"
77268999Sdteske
78268999Sdteske	f_dprintf "PKG_ABI=[%s]" "$PKG_ABI"
79268999Sdteske	export PKG_ABI
80268999Sdteske	return $SUCCESS
81268999Sdteske}
82268999Sdteske
83268999Sdteske############################################################ MAIN
84268999Sdteske
85268999Sdteskef_dprintf "%s: Successfully loaded." packages/musthavepkg.subr
86268999Sdteske
87268999Sdteskefi # ! $_PACKAGES_MUSTHAVEPKG_SUBR
88