From b290eddc70e426a3f28a711d517f943e28297aef Mon Sep 17 00:00:00 2001
From: Ainar Garipov <a.garipov@adguard.com>
Date: Wed, 9 Feb 2022 19:51:26 +0300
Subject: [PATCH] Pull request: scripts: imp install, sup wget

Closes #3637.

Squashed commit of the following:

commit 453094d9a016b0d5a7b7f584b2b492244703064d
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Feb 9 19:44:51 2022 +0300

    scripts: fix wget

commit d16f8d9ad4ac62ce0bc88bab7ceb24a4089e93c3
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Feb 9 19:17:22 2022 +0300

    scripts: imp install, sup wget
---
 scripts/install.sh | 71 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 56 insertions(+), 15 deletions(-)

diff --git a/scripts/install.sh b/scripts/install.sh
index 3b1777ed..7cf35adc 100644
--- a/scripts/install.sh
+++ b/scripts/install.sh
@@ -46,33 +46,31 @@ is_little_endian() {
 # Function check_required checks if the required software is available on the
 # machine.  The required software:
 #
-#   curl
 #   unzip (macOS) / tar (other unixes)
 #
+# curl/wget are checked in function configure.
 check_required() {
 	required_darwin="unzip"
 	required_unix="tar"
 	readonly required_darwin required_unix
 
-	# Split with space.
-	required="curl"
 	case "$os"
 	in
 	('freebsd'|'linux'|'openbsd')
-		required="$required $required_unix"
+		required="$required_unix"
 		;;
 	('darwin')
-		required="$required $required_darwin"
+		required="$required_darwin"
 		;;
 	(*)
-		# Generally shouldn't happen, since the OS has already been
-		# validated.
+		# Generally shouldn't happen, since the OS has already been validated.
 		error_exit "unsupported operating system: '$os'"
 		;;
 	esac
+	readonly required
 
 	# Don't use quotes to get word splitting.
-	for cmd in ${required}
+	for cmd in $required
 	do
 		log "checking $cmd"
 		if ! is_command "$cmd"
@@ -189,6 +187,9 @@ set_os() {
 		('OpenBSD')
 			os='openbsd'
 			;;
+		(*)
+			error_exit "unsupported operating system: '$os'"
+			;;
 		esac
 	fi
 
@@ -240,6 +241,9 @@ set_cpu() {
 			fi
 			cpu="${cpu}_softfloat"
 			;;
+		(*)
+			error_exit "unsupported cpu type: $cpu"
+			;;
 		esac
 	fi
 
@@ -297,6 +301,41 @@ fix_freebsd() {
 	fi
 }
 
+# download_curl uses curl(1) to download a file.  The first argument is the URL.
+# The second argument is optional and is the output file.
+download_curl() {
+	curl_output="${2:-}"
+	if [ "$curl_output" = '' ]
+	then
+		curl -L -S -s "$1"
+	else
+		curl -L -S -o "$curl_output" -s "$1"
+	fi
+}
+
+# download_wget uses wget(1) to download a file.  The first argument is the URL.
+# The second argument is optional and is the output file.
+download_wget() {
+	wget_output="${2:--}"
+
+	wget --no-verbose -O "$wget_output" "$1"
+}
+
+# Function set_download_func sets the appropriate function for downloading
+# files.
+set_download_func() {
+	if is_command 'curl'
+	then
+		# Go on and use the default, download_curl.
+		return 0
+	elif is_command 'wget'
+	then
+		download_func='download_wget'
+	else
+		error_exit "either curl or wget is required to install AdGuard Home via this script"
+	fi
+}
+
 # Function set_sudo_cmd sets the appropriate command to run a command under
 # superuser privileges.
 set_sudo_cmd() {
@@ -320,6 +359,7 @@ configure() {
 	set_os
 	set_cpu
 	fix_darwin
+	set_download_func
 	set_sudo_cmd
 	check_out_dir
 
@@ -384,11 +424,11 @@ rerun_with_root() {
 
 	log 'restarting with root privileges'
 	
-	# Group curl together with an echo, so that if curl fails before producing
-	# any output, the echo prints an exit command for the following shell to
-	# execute to prevent it from getting an empty input and exiting with a zero
-	# code in that case.
-	{ curl -L -S -s "$script_url" || echo 'exit 1'; }\
+	# Group curl/wget together with an echo, so that if the former fails before
+	# producing any output, the latter prints an exit command for the following
+	# shell to execute to prevent it from getting an empty input and exiting
+	# with a zero code in that case.
+	{ "$download_func" "$script_url" || echo 'exit 1'; }\
 		| $sudo_cmd sh -s -- -c "$channel" -C "$cpu" -O "$os" -o "$out_dir" "$r" "$u" "$v"
 
 	# Exit the script.  Since if the code of the previous pipeline is non-zero,
@@ -401,7 +441,7 @@ rerun_with_root() {
 download() {
 	log "downloading package from $url -> $pkg_name"
 
-	if ! curl -s "$url" --output "$pkg_name"
+	if ! "$download_func" "$url" "$pkg_name"
 	then
 		error_exit "cannot download the package from $url into $pkg_name"
 	fi
@@ -450,7 +490,7 @@ handle_existing() {
 		return 0
 	fi
 
-	if [ "$( ls -1 -A $agh_dir )" != '' ]
+	if [ "$( ls -1 -A "$agh_dir" )" != '' ]
 	then
 		log 'the existing AdGuard Home installation is detected'
 
@@ -522,6 +562,7 @@ cpu=''
 os=''
 out_dir='/opt'
 pkg_ext='tar.gz'
+download_func='download_curl'
 sudo_cmd='sudo'
 
 parse_opts "$@"