#!/bin/bash
# dev/package/install - package installation

set -e

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


######
# Library Configuration

dev_package_install_config_init() {
	lib_setting_vars package_install_tarext
}

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


######
# Package installation

package_install_local() {
	has_args 1 "$@"
	local version=$2

	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/$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!"
}

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/$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!"
}

# package_install_script_header() - Generates header for a script.
package_install_script_header() {
	gen_bash_header "$@"
	echo "set -x"
	echo
}

# package_install_script_common() - Generates script to extract tarball,
# bootstrap it, install it, and then clean up after itself.
package_install_script_common() {
	has_args 1 "$@"
	local version=$1

	local dirname=$(package_dist_dirname '' "$version")
	cat <<SCRIPT
${PKG}_package_install() {
	local dirname="$dirname"
	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
SCRIPT
}

# 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}_net_install"
	package_install_script_header "$name" "${PKG} local installer"
	package_install_script_common "$version"
}

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

# 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"
	package_install_script_header "$name" "${PKG} network installer"
	package_install_script_net_download "$version"
	package_install_script_common "$version"
}

package_install_script_net_download() {
	has_args 1 "$@"
	local version=$1
	local tarext=$package_install_tarext

	local dirname=$(package_dist_dirname '' "$version")
	cat <<SCRIPT
${PKG}_package_download() {
	local urlbase="${DOWNLOAD_SCHEME}://${DOWNLOAD_HOST}${DOWNLOAD_PATH}"
	local dirname="$dirname"
	wget "\$urlbase/\$dirname/\$dirname.$tarext"
}

[ -f "$dirname.$tarext" ] || ${PKG}_package_download

SCRIPT
}

View the Script Reference Index


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