xref: /freebsd/usr.sbin/bsdconfig/share/packages/musthavepkg.subr (revision c4ba6882948e2f683d587057f6c8cfa2b04e24ef)
195d45410SDevin Teskeif [ ! "$_PACKAGES_MUSTHAVEPKG_SUBR" ]; then _PACKAGES_MUSTHAVEPKG_SUBR=1
295d45410SDevin Teske#
3*c4ba6882SDevin Teske# Copyright (c) 2014-2016 Devin Teske
495d45410SDevin Teske# All rights reserved.
595d45410SDevin Teske#
695d45410SDevin Teske# Redistribution and use in source and binary forms, with or without
795d45410SDevin Teske# modification, are permitted provided that the following conditions
895d45410SDevin Teske# are met:
995d45410SDevin Teske# 1. Redistributions of source code must retain the above copyright
1095d45410SDevin Teske#    notice, this list of conditions and the following disclaimer.
1195d45410SDevin Teske# 2. Redistributions in binary form must reproduce the above copyright
1295d45410SDevin Teske#    notice, this list of conditions and the following disclaimer in the
1395d45410SDevin Teske#    documentation and/or other materials provided with the distribution.
1495d45410SDevin Teske#
1595d45410SDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1695d45410SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1795d45410SDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1895d45410SDevin Teske# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1995d45410SDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2095d45410SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2195d45410SDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2295d45410SDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2395d45410SDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2495d45410SDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2595d45410SDevin Teske# SUCH DAMAGE.
2695d45410SDevin Teske#
2795d45410SDevin Teske# $FreeBSD$
2895d45410SDevin Teske#
2995d45410SDevin Teske############################################################ INCLUDES
3095d45410SDevin Teske
3195d45410SDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig"
3295d45410SDevin Teske. $BSDCFG_SHARE/common.subr || exit 1
3395d45410SDevin Teskef_dprintf "%s: loading includes..." packages/musthavepkg.subr
3495d45410SDevin Teskef_include $BSDCFG_SHARE/dialog.subr
3595d45410SDevin Teskef_include $BSDCFG_SHARE/mustberoot.subr
3695d45410SDevin Teske
3795d45410SDevin Teske############################################################ FUNCTIONS
3895d45410SDevin Teske
3995d45410SDevin Teske# f_musthavepkg_init
4095d45410SDevin Teske#
4195d45410SDevin Teske# Validate pkg(8) is installed and set $PKG_ABI global if not already set.
4295d45410SDevin Teske# Returns success unless pkg(8) is not installed and user refuses to install
4395d45410SDevin Teske# it (upon prompt when running interactively).
4495d45410SDevin Teske#
4595d45410SDevin Teskef_musthavepkg_init()
4695d45410SDevin Teske{
4795d45410SDevin Teske	local funcname=f_musthavepkg_init
48*c4ba6882SDevin Teske	local pkg_abi_awk=' # BEGIN-AWK
49*c4ba6882SDevin Teske		$1 ~ /^ABI/ && $0 = $NF, sub(/^"/, "") && sub(/".*/, "") {
50*c4ba6882SDevin Teske			print; found = 1; exit
51*c4ba6882SDevin Teske		} END { exit ! found }
52*c4ba6882SDevin Teske	' # END-AWK
5395d45410SDevin Teske
5495d45410SDevin Teske	if [ "$PKG_ABI" ]; then # Already set
5595d45410SDevin Teske		f_dprintf "PKG_ABI=[%s]" "$PKG_ABI"
5695d45410SDevin Teske		export PKG_ABI
5795d45410SDevin Teske		f_quietly pkg -N -vv # return status (pkg(8) functional?)
5895d45410SDevin Teske		return $?
5995d45410SDevin Teske	fi
6095d45410SDevin Teske
6195d45410SDevin Teske	# Attempt to get PKG_ABI without prematurely bootstrapping pkg(8)
6295d45410SDevin Teske	if f_eval_catch -k PKG_ABI $funcname pkg \
6395d45410SDevin Teske		"pkg -N -vv | awk '%s'" "$pkg_abi_awk"
6495d45410SDevin Teske	then
6595d45410SDevin Teske		f_dprintf "PKG_ABI=[%s]" "$PKG_ABI"
6695d45410SDevin Teske		export PKG_ABI
6795d45410SDevin Teske		return $SUCCESS
6895d45410SDevin Teske	fi
6995d45410SDevin Teske
7095d45410SDevin Teske	# pkg(8) not yet bootstrapped; ask for permission unless nonInteractive
7195d45410SDevin Teske	f_dialog_yesno "$msg_pkg_not_yet_installed_install_now" ||
7295d45410SDevin Teske		f_die 1 "$msg_must_have_pkg_to_execute" "$pgm"
7395d45410SDevin Teske
7495d45410SDevin Teske	f_mustberoot_init # Have to be root to install pkg(8)
7595d45410SDevin Teske
7695d45410SDevin Teske	# Bootstrap pkg(8)
7795d45410SDevin Teske	f_dialog_info "$msg_bootstrapping_pkg"
7895d45410SDevin Teske	f_eval_catch -k PKG_ABI $funcname pkg \
7995d45410SDevin Teske		"ASSUME_ALWAYS_YES=1 pkg -vv | awk '%s'" "$pkg_abi_awk" ||
8095d45410SDevin Teske		f_die 1 "$msg_must_have_pkg_to_execute" "$pgm"
8195d45410SDevin Teske
8295d45410SDevin Teske	f_dprintf "PKG_ABI=[%s]" "$PKG_ABI"
8395d45410SDevin Teske	export PKG_ABI
8495d45410SDevin Teske	return $SUCCESS
8595d45410SDevin Teske}
8695d45410SDevin Teske
8795d45410SDevin Teske############################################################ MAIN
8895d45410SDevin Teske
8995d45410SDevin Teskef_dprintf "%s: Successfully loaded." packages/musthavepkg.subr
9095d45410SDevin Teske
9195d45410SDevin Teskefi # ! $_PACKAGES_MUSTHAVEPKG_SUBR
92