blob: 1d04de54b611ed78f46f4b3b9c41bf42f03bd44b [file] [log] [blame]
Steve Kondikd91ddb52015-11-14 23:56:10 +01001#!/system/bin/sh
2# wget-curl, a curl wrapper acting as a wget drop-in replacement - version git-HEAD
3# Usage: wget [wget args] [i need to fill this in later] <url(s)>
4# Download all URLs given using curl, but using wget's options.
5#
6#
7# End of help.
8# Copyright (c) 2015 Kylie McClain <somasis@exherbo.org>
9#
10# Permission to use, copy, modify, and/or distribute this software for any
11# purpose with or without fee is hereby granted, provided that the above
12# copyright notice and this permission notice appear in all copies.
13#
14# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20# PERFORMANCE OF THIS SOFTWARE.
21#
22# End of copyright.
23#
24
25set -o pipefail
26shopt -u shift_verbose >/dev/null 2>&1
27
28help() {
29 sed -n '/^#/!d;s/^# //;s/^#//;3,${p;}' "$0" | \
30 while IFS= read help_line;do
31 if [[ "$help_line" == "End of help." ]];then
32 exit 0
33 else
34 printf "%s\n" "$help_line"
35 fi
36 done
37 exit 0
38}
39
40version() {
41 sed 's/^# //;s/, .* - version / /;2q;$!d' "$0"
42 copyright
43 exit 0
44}
45
46copyright() {
47 sed -n '/^#/!d;s/^# //;s/^#//;/End of help./,${p;}' "$0" | \
48 while IFS= read copyright_line;do
49 if [[ "$copyright_line" == "End of help." ]];then
50 true
51 elif [[ "$copyright_line" == "End of copyright." ]];then
52 break
53 else
54 printf '%s\n' "$copyright_line"
55 fi
56 done
57}
58
59stderr() {
60 printf "$@" >&2
61}
62
63error() {
64 stderr "$0: $1\n"
65 exit "$2"
66}
67
68invalid_arg() {
69 error "invalid option -- '$1'" 2
70}
71
72append_opt() {
73 for opt in $@;do
74 CURL_OPTS="${CURL_OPTS} ${opt}"
75 done
76}
77
78curl() {
79 eval "command curl $@ ${CURL_RAW}"
80}
81
82append_raw_arg() {
83 CURL_RAW="$CURL_RAW $@"
84}
85
86has_opt() { # exit 0 if CURL_OPTS has arg, non-zero if doesn't
87 if [[ "$CURL_OPTS" == *" $1"* ]];then
88 return 0
89 else
90 return 1
91 fi
92}
93
94reexec_without() { # download afterwards without $1 in OPTS
95 reexec_args_without="$reexec_args_without $@"
96 reexec=1
97}
98
99reexec_only() {
100 for arg in $@;do
101 CURL_OPTS_REEXEC_ONLY="${CURL_OPTS_REEXEC_ONLY} $arg"
102 done
103}
104
105print_url() {
106 has_opt -s || printf "%s\n" "$1"
107}
108
109# 46ABDFHIKLNOPQRSTUVXabcdhiklm nH nc nd np nv opqrtvwx
110while getopts ':46ABDFHIKLNO:PQRST:U:VXa:bcdhiklmopqrtvwx' argument "$@";do
111 case "$argument" in
112 # a lot of these are noop right now because they are wget mirror args
113 # which curl doesn't really do, and i am not sure if i should implement them
114 4) append_opt -4 ;;
115 6) append_opt -6 ;;
116 A) true ;; # probably can't implement this easily...
117 B) true ;;
118 D) true ;;
119 E) true ;;
120 F) true ;; # curl doesn't care what the input is
121 H) true ;;
122 I) true ;;
123 K) true ;;
124 L) true ;;
125 N) true ;;
126 O) append_opt "-o $OPTARG" ;;
127 P) true ;;
128 Q) true ;;
129 R) true ;;
130 S) append_opt -I;reexec_without -I -s ;;
131 T) append_opt "-m $OPTARG" ;;
132 U) append_opt "--user-agent \"$OPTARG\"" ;;
133 V) version; curl --version; exit 0 ;;
134 X) true ;;
135 a) append_raw_arg "2>&1 | tee -a $OPTARG" ;;
136 b)
137 wget_log="wget-log"
138 i=1
139 while [[ -f "${wget_log}" ]];do
140 # if that exists, increment until we find something that doesn't
141 i=$(($i+1))
142 wget_log="wget-log.${i}"
143 done
144 append_raw_arg ">\"$wget_log\" 2>&1 &"
145 printf "Continuing in background, pid %s.\nOutput will be written to '$wget_log'.\n" "$$"
146 ;;
147 c) append_opt "-C -" ;;
148 d) append_opt "-v" ;;
149 e) true ;;
150 h) help ;;
151 i)
152 [[ ! -f "$OPTARG" ]] && error "$OPTARG does not exist" 3
153 for url in $(<"$OPTARG");do
154 URLS=( ${URLS[@]} "$url" )
155 done
156 ;;
157 q) append_opt "-s" ;;
158 esac
159 shift $(($OPTIND-1))
160done
161
162# set wget default equivilants
163append_opt -L # follow redirects
164append_opt -# # progress bar
165
166if [[ -z "${URLS[@]}" ]];then
167 URLS=( ${@} )
168fi
169
170for url in ${URLS[@]};do
171 url_file=${url##*/}
172 if [[ "$url" == "$url_file" ]];then
173 # has no remote file name and -o is not in CURL_OPTS... assume index.html
174 has_opt -o || append_opt "-o index.html"
175 fi
176
177 eval "print_url '$url';curl ${CURL_OPTS} -- $url"
178 if [[ "$reexec" ]];then
179 for reexec_arg in ${reexec_args_without};do
180 CURL_OPTS_REEXEC=$(echo "${CURL_OPTS_REEXEC:-$CURL_OPTS}" | sed "s# $reexec_arg##")
181 done
182 eval "print_url '$url';curl ${CURL_OPTS_REEXEC} ${CURL_OPTS_REEXEC_ONLY} -- $url"
183 fi
184done