#!/bin/bash
# net/tool/wget - ``wget`` Support

set -e

net_tool_wget_config_init() {
	lib_setting_vars wget_mirror
	lib_setting_vars wget_continue
	lib_setting_vars wget_directories
	lib_setting_vars wget_prereqs
	lib_setting_vars wget_munge_exts
	lib_setting_vars wget_convert_links
	lib_setting_vars wget_host_dirs
	lib_setting_vars --null wget_prefix
	lib_setting_vars --null wget_output
	lib_setting_vars --null wget_wait_secs
}

######
# ``wget`` Support

# run_wget() - Runs the ``wget`` command with options determined by
# the library settings.
run_wget() {
	min_args 1 "$@"
	local -a opts
	$verbose || opts+=( --quiet )
	! $debug || opts+=( --verbose )
	! $wget_mirror || opts+=( --mirror )
	! $wget_continue || opts+=( --continue )
	$wget_directories || opts+=( --no-directories )
	! $wget_prereq || opts+=( --page-requisites )
	! $wget_munge_exts || opts+=( --adjust-extension )
	! $wget_convert_links || opts+=( --convert-links )
	$wget_host_dirs || opts+=( --no-host-directories )
	[ -z "$wget_cut_dirs" ] || opts+=( --cut-dirs "$wget_cut_dirs" )
	[ -z "$wget_prefix" ] || opts+=( --directory-prefix "$wget_prefix" )
	[ -z "$wget_wait_secs" ] || opts+=( --wait "$wget_wait_secs" )
	[ -z "$wget_output" ] || opts+=( --output-document "$wget_output" )

	opts+=( --show-progress --progress=bar:noscroll )
	run wget "${opts[@]}" "$@"
}

wget_mirror() {
	min_args 1 "$@"
	local wget_mirror=true
	local wget_prereqs=true
	local wget_munge_exts=true
	local wget_convert_links=true
	local wget_host_dirs=true
	local wget_prefix="/"
	run_wget "$@"
}

wget_files() {
	min_args 1 "$@"
	local wget_continue=true
	local wget_directories=false
	run_wget "$@"
}

# wget_exec() - Downloads a file from a URL ($1) and exectues it with
# the given arguments ($@).
wget_exec() {
	min_args 1 "$@"
	local url=$1
	shift

	local urlname=${url##*/}
	urlname=${urlname%\?*}
	urlname=${urlname%#*}
	local name=${wget_output:-$urlname}

	local wget_output
	wget_output=${wget_output:-$(cmd_tempfile)}
	local wget_prereqs=false

	info "$name: downloading to $wget_output..."
	run_wget "$url"

	info "$name: running '$wget_output $*'..."
	run chmod u+x $wget_output
	run $wget_output "$@"
}

View the Developer Guide Index

View the Reference Manual Index


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