#!/bin/bash
#  apt - Debian/Ubuntu apt command support

set -e

lib_load 'net/remote'
lib_load 'sys/tool/chroot'

######
# Settings

sys_tool_apt_config_init() {
	lib_setting_vars apt_pretend

	lib_setting_arrays \
		apt_update_opts apt_install_opts apt_upgrade_opts
}

# sys_tool_apt_config_check - scans libraries and script for package settings
sys_tool_apt_config_check() {
	apt_pretend=${apt_pretend:-false}

	for_each_lib _sys_tool_apt_config_check
	_sys_tool_apt_config_check "$script_name"
}

# scans one library/script for apt package settings
_sys_tool_apt_config_check() {
	local name=$1
	for ext in client server; do
		local var="$(lib_symbol $name)_${ext}_packages"
		if eval "[ \"\${${var}[*]}\" ]"; then
			settings_add_arrays "apt" "$var"
		fi
	done
}


######
# Internal functions

apt_options() {
	local var=$1
	list_append $var '--yes'
	list_append $var --quiet
	$verbose || list_append $var --quiet
	! $apt_pretend || list_append $var '--dry-run'
}

apt_deps_append() {
	has_args 3 "$@"
	local var=$1
	local class=$2
	local prefix=$3

	local sym
	sym=$(lib_symbol $prefix)
	local name="${sym}_${class}_packages"

	local -a packages
	eval "packages=( \"\${$name[@]}\" )"
	if [ "${packages[*]}" ]; then
		info "$prefix: adding $class packages: ${packages[*]}"
		list_append $var "${packages[@]}"
	else
		debug "$prefix: no $class packages"
	fi
}


######
# Public interface

apt_install() {
	local -a opts
	apt_options opts
	run_sudo apt install "${opts[@]}" "$@"
}

apt_upgrade() {
	local -a opts
	apt_options opts
	run_sudo apt "${opts[@]}" update
	run_sudo apt "${opts[@]}" dist-upgrade
	run_sudo apt "${opts[@]}" autoremove
}


######
# Package class support

#  apt_class_usage - Prints common command usage for client/server CLI
apt_class_usage() {
	has_args 1 "$@"
	local class=$1
	cat <<USAGE
<cmd> ...
Package Commands:
	deps				List $class packages
	install				Install $class packages
USAGE
}

_apt_class_deps() {
	has_args 2 "$@"
	local var=$1
	local class=$2

	for_each_lib apt_deps_append $var $class
	apt_deps_append $var $class $script_name
}

apt_class_deps() {
	has_args 1 "$@"
	local class=$1
	local -a deps
	_apt_class_deps deps "$class"
	IFS=$'\n' echo "${deps[*]}"
}

apt_class_install() {
	has_args 1 "$@"
	local class=$1

	local -a deps
	_apt_class_deps deps $class
	apt_install "${deps[@]}"
}


######
# Apt inside chroots

apt_chroot() {
	local action=$1
	local name=$2
	shift 2
	local -a cmd
	list_append cmd env "DEBIAN_FRONTEND=noninteractive"
	list_append cmd apt "$action" --yes
#	list_append cmd -o 'Dpkg::Options::="--force-confdef"'
#	list_append cmd -o 'Dpkg::Options::="--force-confold"'
	local var="apt_${action}_opts"
	eval "list_append cmd \"\${$var[@]}\""
	run_chroot "$name" "${cmd[@]}" "$@"
}

apt_chroot_update() { apt_chroot update "$@"; }
apt_chroot_install() { apt_chroot install "$@"; }
apt_chroot_upgrade() { apt_chroot upgrade "$@"; }

View the Script Reference Index


Generated on Tue Apr 25 21:21:21 PDT 2017 by mcsh i7 v0.18.0.