Browse code

Bug fixed?

Jaidyn Lev authored on 2019-01-20 23:27:35
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,155 @@
1
+#!/bin/sh
2
+##############################
3
+# name: gendl
4
+# lisc: gnu gplv3
5
+# desc: download to stdout or
6
+#       file, independent of
7
+#       a specific downloader.
8
+#       ftp/wget/curl support.
9
+# main: jadedctrl
10
+##############################
11
+
12
+# usage: gendl [-o output] url
13
+
14
+
15
+
16
+#---------------------------------------
17
+# backend bits
18
+
19
+# NIL --> STRING
20
+# return the download program you're using
21
+function download_program
22
+{
23
+	programs="ftp curl wget"
24
+	current=""
25
+
26
+	for program in $programs
27
+	do
28
+		if whereis $program > /dev/null
29
+		then
30
+			current=$program
31
+		fi
32
+	done
33
+
34
+	if uname -s | grep -e "LibertyBSD" -e "OpenBSD" > /dev/null
35
+	then
36
+		current="ftp"
37
+	fi
38
+
39
+	echo "$current"
40
+}
41
+
42
+# STRING PATH --> NIL
43
+# download URL $1 to stdout
44
+function download_stdout
45
+{
46
+	program=$(download_program)
47
+	url=$1
48
+
49
+	case "$program" in
50
+		"ftp")
51
+			output="$(ftp -VMo- $url)"
52
+			;;
53
+		"curl")
54
+			output="$(curl $url)"
55
+			;;
56
+		"wget")
57
+			output="$(wget --quiet -O $url)"
58
+			;;
59
+	esac
60
+
61
+	return_code=$?
62
+
63
+	echo "$output"
64
+	return $return_code
65
+}
66
+
67
+# STRING PATH --> NIL
68
+# download URL $1 to path $2
69
+function download_file
70
+{
71
+	program=$(download_program)
72
+	url=$1
73
+	path=$2
74
+
75
+	case "$program" in
76
+		"ftp")
77
+			ftp -VMU "." -o $path $url
78
+			;;
79
+		"curl")
80
+			curl -o $path $url
81
+			;;
82
+		"wget")
83
+			wget --quiet -O $path $url
84
+			;;
85
+	esac
86
+
87
+	return_code=$?
88
+
89
+	if test $return_code -ne 0 2>/dev/null
90
+	then
91
+		rm $path 2> /dev/null
92
+		# for consistency in behavior; wget saves 404s anyway, whereas
93
+		# ftp doesn't save anything from 404s, etc.
94
+	fi
95
+
96
+	return $return_code
97
+}
98
+
99
+
100
+
101
+# --------------------------------------
102
+# front-end string-manip
103
+
104
+# STRING --> STRING
105
+# return the last word in a string
106
+function last_word
107
+{
108
+	string="$1"
109
+
110
+	echo "$string" \
111
+	| rev \
112
+	| sed 's% .*%%' \
113
+	| rev
114
+}
115
+
116
+function usage
117
+{
118
+	echo "usage: gendl [-o output] URL"
119
+}
120
+
121
+
122
+
123
+# --------------------------------------
124
+# invocation
125
+
126
+args="$(getopt o: $*)"
127
+if test -z "$@" 2>/dev/null
128
+then
129
+	usage
130
+	exit 2
131
+fi
132
+
133
+
134
+set -- $args
135
+while test $# -ne 0
136
+do
137
+	case "$1"
138
+	in
139
+		-o)
140
+			download_path="$2"; shift; shift;;
141
+		--)
142
+			shift; break;;
143
+	esac
144
+done
145
+
146
+
147
+url="$(last_word "$@")"
148
+
149
+
150
+if test -n "$download_path" 2> /dev/null
151
+then
152
+	download_file $url $download_path
153
+else
154
+	download_stdout "$url"
155
+fi