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