#!/bin/bash
# dev/package/install - Package Installation Support

set -e

lib_load 'dev/package/dist'
lib_load 'dev/gen/bash'


######
# Library Configuration

dev_package_install_config_init() {
	# $package_install_tarext - Extension to use for tarballs.
	# See the ``sys/tool/tar`` library for popular options.
	# Default: ``tar.gz``
	lib_setting_vars package_install_tarext
}

dev_package_install_config_check() {
	package_install_tarext=${package_install_tarext:-tar.gz}
}


######
# Local Host

# package_install_local() - Performs a local installation of the given
# package version ($1).
package_install_local() {
	has_args 1 "$@"
	local version=$1

	local tmp
	tmp=$(cmd_tempfile)
	package_install_script_local "$version" >"$tmp"

	local tarext=$package_install_tarext
	local archive=$(package_dist_tarname "" "$version" $tarext)
	local distfile="$(package_dist_reldir "$version")/$archive"
	[ -f "$distfile" ] || error "$archive: does not exist"

	info "localhost: $PKG v$version: extracting..."
	run cp -a "$distfile" "$cmd_tempdir"

	info "localhost: $PKG v$version: installing..."
	(cd "$cmd_tempdir" && run_sudo bash $tmp)

	info "localhost: $PKG v$version: ... done!"
}

######
# Remote Host

# package_install_remote() - Performs a remote installation of the given
# package version ($1).
package_install_remote() {
	min_args 2 "$@"
	local version=$1
	shift

	if [ ${#*} -gt 1 ]; then
		for_each "package_install_remote $PKG $version" "$@"
		return
	fi

	local name=$1
	app_echo "remote: $name: $PKG v$version: installing..."

	# generate installer script
	local tmp
	tmp=$(cmd_tempfile)
	package_install_script_remote "$version" >"$tmp"

	local remote_installer="${PKG}_${version}_auto_install.sh"
	local tarext=$package_install_tarext
	local archive=$(package_dist_tarname '' "$version" $tarext)
	local distfile="$(package_dist_reldir "$version")/$archive"
	[ -f "$distfile" ] || error "$archive: does not exist"

	lib_load 'net/remote'

	app_echo "$name: $PKG: scrubbing previous installer files"
	remote_run "$name" rm -fr "$remote_installer" "$archive"*

	app_echo "$name: $PKG: copying files"
	remote_copy_to "$name" "$tmp" "$remote_installer"
	remote_copy_to "$name" "$distfile" "$archive"

	app_echo "$name: $PKG: running remote installer"
	remote_sudo "$name" bash "$remote_installer"

	app_echo "$name: $PKG v$version: ... done!"
}

######
# Install Script Support

# package_install_script_header() - Generates header for a script.
package_install_script_header() {
	local version=$1
	shift
	gen_bash_header "$@"
	cat <<HEADER
set -x

package="$PKG"
version="$version"
pkgvers="\$package-\$version"
HEADER
}

# package_install_script_common() - Generates script to extract tarball,
# bootstrap it, install it, and then clean up after itself.
package_install_script_common() {
	cat <<SCRIPT

${PKG}_package_install() {
	local dirname=\$1
	rm -rf "\$dirname"
	tar -xf "\$dirname.$package_install_tarext"
	cd "\$dirname"
	./bootstrap.sh
	rm system.i7
	make --quiet install
	cd ..
	rm -r "\$dirname"
}
${PKG}_package_install "\$pkgvers"
SCRIPT
}

######
# Install Scripts

# package_install_script_net() - Generates script to install on the local host.
package_install_script_local() {
	has_args 1 "$@"
	local version=$1

	local name="${PKG}-${version}-install"
	local desc="${PKG} local installer"
	package_install_script_header "$version" "$name" "$desc"
	package_install_script_common
}

# package_install_script_remote() - Generates script to install on a remote host
package_install_script_remote() { package_install_script_local "$@"; }


######
# Network Install Script

# package_install_script_net() - Generates script to download a package
# tarball and install it on the local machine.
package_install_script_net() {
	has_args 1 "$@"
	local version=$1

	local name="${PKG}-${version}-net-install"
	local desc="${PKG} network installer"
	package_install_script_header "$version" "$name" "$desc"
	package_install_script_net_download
	package_install_script_common
}

# package_install_script_net_download() - Generates the portion of a
# network installer script that downloads the package tarball from
# the location specified by the package settings.
#
# The download URL is generated from the following package settings:
#
#	``DOWNLOAD_SCHEME DOWNLOAD_HOST DOWNLOAD_PATH``
package_install_script_net_download() {
	cat <<SCRIPT

${PKG}_package_download() {
	local dirname=\$1
	local urlbase="${DOWNLOAD_SCHEME}://${DOWNLOAD_HOST}${DOWNLOAD_PATH}"
	local tarext="$package_install_tarext"
	[ ! -f "\$dirname.\$tarext" ] || return 0
	wget "\$urlbase/releases/\$dirname/\$dirname.\$tarext"
}
${PKG}_package_download "\$pkgvers"
SCRIPT
}


######
# All-In-One Installer Script

# package_install_script_complete() - Generates a script that contains
# the distribution files for the given package version ($1) encoded
# as a here document that can extract itself when the installer is run.
#
# $1 - Package version
#
# This function performs the following steps to generate the script:
package_install_script_complete() {
	has_args 1 "$@"
	local version=$1

	#   - Generates but saves the contents function.  We do this
	#     first, as it will fail if the distribution file is missing.
	local tmp
	local func=package_install_script_complete_contents
	file_capture_func tmp "$func" "$version"

	#   - Prints the script header
	local name="${PKG}-${version}-net-install"
	local desc="${PKG} network installer"
	package_install_script_header "$version" "$name" "$desc"

	#   - Prints the saved function that contains the encoded
	#     distribution contents.
	echo
	cat "$tmp"

	#   - Prints a function to unpack the embedded script contents.
	package_install_script_complete_unpack

	#   - Prints a function to performs the local installation
	package_install_script_common
}

# package_install_script_complete_unpack() - Generates portion of all-in-one
# installer script that unpacks the embedded distribution files.
package_install_script_complete_unpack() {
	cat <<SCRIPT

${PKG}_package_unpack() {
	local dirname=\$1
	${PKG}_package_contents >"\$dirname.$package_install_tarext"
}
${PKG}_package_unpack "\$pkgvers"
SCRIPT
}

# package_install_script_complete_contents() - Generates the portion of
# all-in-one installer script that contains the embedded distribution
# contents.  The package files are embedded as an encoded here document
# that decodes its contents when called.
# $1 - Version of package distribution to include in the installer script.
package_install_script_complete_contents() {
	local version=$1

	local name="${PKG}_package_contents"
	local docs="Outputs the decoded package distribution contents."
	local tarext=$package_install_tarext
	local file=$(package_dist_tarfile '' "$version" "$tarext")
	[ -f "$file" ] || error "$file: distribution file not found"

	gen_bash_here_func "$name" "$docs" "$file"
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:35:25 PDT 2017 by mcsh d14 v0.23.0.