~m-grant-prg/mget/focal-trunk

« back to all changes in this revision

Viewing changes to src/scripts/mget.in

  • Committer: Mark Grant
  • Date: 2015-06-26 14:04:54 UTC
  • Revision ID: m.grant.prg@gmail.com-20150626140454-ic5qk4mhb3136egx
Tags: upstream-1.1.5
ImportĀ upstreamĀ versionĀ 1.1.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env bash
 
2
##########################################################################
 
3
##                                                                      ##
 
4
##      mget is automatically generated,                                ##
 
5
##              please do not modify!                                   ##
 
6
##                                                                      ##
 
7
##########################################################################
 
8
 
 
9
##########################################################################
 
10
##                                                                      ##
 
11
## Script ID: mget                                                      ##
 
12
## Author: Mark Grant                                                   ##
 
13
##                                                                      ##
 
14
## Purpose:                                                             ##
 
15
## Retrieves one or more files whose URL's are specified in a text file.##
 
16
##                                                                      ##
 
17
## Syntax:      mget [OPTIONS]                                          ##
 
18
##                      [OPTIONS] are:-                                 ##
 
19
##      -g or --group group - Save target file with 'group' owner.      ##
 
20
##      -h or --help Displays usage information.                        ##
 
21
##      -o or --owner owner - Save target file with 'owner' owner.      ##
 
22
##      -p or --persist Saves command line options for future use.      ##
 
23
##      -q or --quiet Quiet mode, limited output.                       ##
 
24
##      -s or --silent Silent mode, no output.                          ##
 
25
##      -S or --sourcefile sourcefile Name of the file containing the   ##
 
26
##              URL's of files to be fetched.                           ##
 
27
##      -T or --targetdir targetdir Directory in which to store         ##
 
28
##              retieved files.                                         ##
 
29
##      -u or --unix Use Unix LF instead of Windows CR/LF line endings. ##
 
30
##      -v or --verbose Verbose mode.                                   ##
 
31
##      -V or --version Displays version information.                   ##
 
32
##      -w or --windows Use Windows CR/LF instead of Unix LF line       ##
 
33
##              endings.                                                ##
 
34
##                                                                      ##
 
35
## Exit Codes:  0 & 64 - 113 as per C/C++ standard                      ##
 
36
##              0 - success                                             ##
 
37
##              64 - Invalid arguments                                  ##
 
38
##              66 - File access error                                  ##
 
39
##              67 - trap received                                      ##
 
40
##              77 - Permissions error                                  ##
 
41
##                                                                      ##
 
42
##########################################################################
 
43
 
 
44
##########################################################################
 
45
##                                                                      ##
 
46
## Changelog                                                            ##
 
47
##                                                                      ##
 
48
## Date         Author  Version Description                             ##
 
49
##                                                                      ##
 
50
## 29/04/2013   MG      1.0.1   Created.Does not implement mail         ##
 
51
##                              notification.                           ##
 
52
## 30/04/2013   MG      1.0.2   Corrected Error status section on man   ##
 
53
##                              page. Introduced sed processing on      ##
 
54
##                              source file to ensure it has Unix type  ##
 
55
##                              line endings. Removed part implemented  ##
 
56
##                              'MailTo' option.                        ##
 
57
## 02/05/2013   MG      1.0.3   Introduce -q option to enable use of    ##
 
58
##                              fetch or wget quiet mode.               ##
 
59
## 03/05/2013   MG      1.0.4   Included source and target information  ##
 
60
##                              in script output to be more useful.     ##
 
61
## 05/05/2013   MG      1.0.5   Fixed ugly exit if 'sourcefile' = "" or ##
 
62
##                              'targetdir' = "". 'targetdir' is now    ##
 
63
##                              defaulted to 'PWD'.                     ##
 
64
## 22/08/2013   MG      1.0.6   Debian best practice documentation      ##
 
65
##                              requires that programs placed in the    ##
 
66
##                              PATH should not have extensions as the  ##
 
67
##                              programs may be rewritten in a          ##
 
68
##                              different language. This would then     ##
 
69
##                              require a name change, or, if left      ##
 
70
##                              as-is would cause confusion. So name    ##
 
71
##                              changed to remove .sh extension.        ##
 
72
## 13/12/2013   MG      1.0.7   Changed stdout & stderr message output  ##
 
73
##                              to use a function directing to one or   ##
 
74
##                              other based on a status.                ##
 
75
## 14/12/2013   MG      1.0.8   Changed so that output function is the  ##
 
76
##                              only place to insert the program name   ##
 
77
##                              and it uses $0.                         ##
 
78
## 15/12/2013   MG      1.0.9   Use command name instead of $0.         ##
 
79
## 01/01/2014   MG      1.0.10  Tidied up use of output function.       ##
 
80
## 05/01/2014   MG      1.0.11  Introduced new standardised error       ##
 
81
##                              handler routine to test previous        ##
 
82
##                              command success.                        ##
 
83
##                              Changed command line options to use -V  ##
 
84
##                              as the version option, S as Source URL  ##
 
85
##                              and -T as target directory. Introduced  ##
 
86
##                              -q, -s & -v as quiet, silent and        ##
 
87
##                              verbose options. Fixed error handling   ##
 
88
##                              on existing file deletion.              ##
 
89
## 06/01/2014   MG      1.0.12  Persist option code was in the file     ##
 
90
##                              processing loop and never got           ##
 
91
##                              executed if no files were to be         ##
 
92
##                              processed. Moved out of loop.           ##
 
93
## 27/08/2014   MG      1.0.13  Change name to mget to avoid package    ##
 
94
##                              name duplication in Ubuntu.             ##
 
95
## 03/09/2014   MG      1.1.1   Change to utilise configure.ac's check  ##
 
96
##                              on whether wget or fetch is in use.     ##
 
97
##                              Change to use awk script to convert to  ##
 
98
##                              and from Windows / Unix text file line  ##
 
99
##                              endings to ensure the existing ending   ##
 
100
##                              is tested to determine if conversion is ##
 
101
##                              required.                               ##
 
102
## 28/11/2014   MG      1.1.2   Switch to use GNU getopt to enable long ##
 
103
##                              options.                                ##
 
104
## 28/11/2014   MG      1.1.3   Add overall package version to -V.      ##
 
105
## 01/12/2014   MG      1.1.4   Replaced windows line ending conversion ##
 
106
##                              code wuth use of wutconv.               ##
 
107
## 01/12/2014   MG      1.1.5   Allow mget to retrieve specified file   ##
 
108
##                              with no line ending conversion as       ##
 
109
##                              default. Add --unix option as complement##
 
110
##                              to --windows option.                    ##
 
111
## 26/06/2015   MG      1.1.6   Remove BSD support.                     ##
 
112
##                                                                      ##
 
113
##########################################################################
 
114
 
 
115
 
 
116
####################
 
117
## Init variables ##
 
118
####################
 
119
script_exit_code=0
 
120
version="1.1.6"                         # set version variable
 
121
etclocation=@sysconfdir@                # Path to etc directory
 
122
outputprefix="mget: "
 
123
packageversion=@pkgversion@             # Version of the complete package
 
124
 
 
125
conffile=".mget"
 
126
ownergroup=FALSE
 
127
owner=FALSE
 
128
persist=FALSE
 
129
sourcefile=""
 
130
targetdir=""
 
131
 
 
132
# wget defaults to verbose so make non-verbose default here.
 
133
verbosity="-nv"
 
134
windows=FALSE
 
135
unix=FALSE
 
136
 
 
137
###############
 
138
## Functions ##
 
139
###############
 
140
 
 
141
# Output $1 to stdout or stderr depending on $2
 
142
output()
 
143
{
 
144
        if [ $2 = 0 ]
 
145
        then
 
146
                echo "$outputprefix$1"
 
147
        else
 
148
                echo "$outputprefix$1" 1>&2
 
149
        fi
 
150
}
 
151
 
 
152
# Standard function to test command error ($1 is $?) and exit if non-zero
 
153
std_cmd_err_handler()
 
154
{
 
155
        if [ $1 != 0 ]
 
156
        then
 
157
                script_exit_code=$1
 
158
                script_exit
 
159
        fi
 
160
}
 
161
 
 
162
# Standard function to tidy up and return exit code
 
163
script_exit()
 
164
{
 
165
        exit $script_exit_code
 
166
}
 
167
 
 
168
# Standard trap exit function
 
169
trap_exit()
 
170
{
 
171
script_exit_code=67
 
172
output "Script terminating due to trap received. Code: "$script_exit_code 1
 
173
script_exit
 
174
}
 
175
 
 
176
# Setup trap
 
177
trap trap_exit SIGHUP SIGINT SIGTERM
 
178
 
 
179
# Function to write config file.
 
180
write_file()
 
181
{
 
182
        echo "sourcefile=$sourcefile">>~/.mget
 
183
        echo "targetdir=$targetdir">>~/.mget
 
184
}
 
185
 
 
186
##########
 
187
## Main ##
 
188
##########
 
189
# If config file exists, read it, else, create it.
 
190
if [ ! -f ~/$conffile ]
 
191
then
 
192
        write_file
 
193
else
 
194
        IFS="="
 
195
        exec 3<~/$conffile
 
196
        while read -u3 -ra input
 
197
        do
 
198
                case ${input[0]} in
 
199
                sourcefile)
 
200
                        sourcefile=${input[1]}
 
201
                        ;;
 
202
                targetdir)
 
203
                        targetdir=${input[1]}
 
204
                        ;;
 
205
                esac
 
206
        done
 
207
        exec 3<&-
 
208
fi
 
209
 
 
210
# Process command line arguments with GNU getopt.
 
211
GETOPTTEMP=`getopt -o g:ho:pqsS:T:uvVw \
 
212
--long group:,help,owner:,persist,quiet,silent,sourcefile:,targetdir:,unix,verbose,version,windows \
 
213
-n "$0" -- "$@"`
 
214
std_cmd_err_handler $?
 
215
 
 
216
eval set -- "$GETOPTTEMP"
 
217
std_cmd_err_handler $?
 
218
 
 
219
while true
 
220
do
 
221
        case "$1" in
 
222
        -g|--group)
 
223
                ownergroup=TRUE
 
224
                newownergroup=$2
 
225
                shift 2
 
226
                ;;
 
227
        -h|--help)
 
228
                echo "Usage is $0 [OPTIONS]"
 
229
                echo "  [OPTIONS] are:-"
 
230
                echo "  '-g or --group group' Save target file with 'group' owner."
 
231
                echo "  '-h or --help' Displays usage information."
 
232
                echo "  '-o or --owner owner' Save target file with 'owner' owner."
 
233
                echo "  '-p or --persist' Saves command line options for future use."
 
234
                echo "  '-q or --quiet' Quiet mode, limited output."
 
235
                echo "  '-s or --silent' Silent mode, no output."
 
236
                echo "  '-S or --sourcefile sourcefile' Name of file containing source URL's."
 
237
                echo "          Can be persisted with -p."
 
238
                echo "  '-T or --targetdir targetdir' Name of directory for storing retrieved files."
 
239
                echo "          Can be persisted with -p."
 
240
                echo "  '-u or --unix' Use Unix LF line endings instead of Windows CR/LF line endings."
 
241
                echo "  '-v or --verbose' Verbose mode."
 
242
                echo "  '-V or --version' Displays version information."
 
243
                echo "  '-w or --windows' Use Windows CR/LF line endings instead of Unix LF line endings."
 
244
                shift
 
245
                script_exit_code=0
 
246
                script_exit
 
247
                ;;
 
248
        -o|--owner)
 
249
                owner=TRUE
 
250
                newowner=$2
 
251
                shift 2
 
252
                ;;
 
253
        -p|--persist)
 
254
                persist=TRUE
 
255
                shift
 
256
                ;;
 
257
        -q|--quiet)
 
258
                verbosity="-q"
 
259
                shift
 
260
                ;;
 
261
        -s|--silent)
 
262
                verbosity=""
 
263
                exec 1>/dev/null 2>/dev/null
 
264
                shift
 
265
                ;;
 
266
        -S|--sourcefile)
 
267
                sourcefile=$2
 
268
                shift 2
 
269
                ;;
 
270
        -T|--targetdir)
 
271
                targetdir=$2
 
272
                shift 2
 
273
                ;;
 
274
        -u|--unix)
 
275
                unix=TRUE
 
276
                shift
 
277
                ;;
 
278
        -v|--verbose)
 
279
                verbosity="-v"
 
280
                shift
 
281
                ;;
 
282
        -V|--version)
 
283
                echo "$0 Script version "$version
 
284
                echo "$0 Package version "$packageversion
 
285
                shift
 
286
                script_exit_code=0
 
287
                script_exit
 
288
                ;;
 
289
        -w|--windows)
 
290
                windows=TRUE
 
291
                shift
 
292
                ;;
 
293
        --)     shift
 
294
                break
 
295
                ;;
 
296
        *)      script_exit_code=1
 
297
                output "Internal error." 1
 
298
                script_exit
 
299
                ;;
 
300
        esac
 
301
done
 
302
 
 
303
# Script does not accept other arguments.
 
304
if [ $# -gt 0 ]
 
305
then
 
306
        script_exit_code=64
 
307
        output "Invalid argument." 1
 
308
        script_exit
 
309
fi
 
310
 
 
311
# If only persist flag set then nothing to do, exit.
 
312
if [ $# = 1 -a "$1" = "-p" ]
 
313
then
 
314
        script_exit_code=0
 
315
        script_exit
 
316
fi
 
317
 
 
318
# u and w flag cannot both be set.
 
319
if [ $windows = TRUE -a $unix = TRUE ]
 
320
then
 
321
        script_exit_code=64
 
322
        output "-u and -w cannot both be set." 1
 
323
        script_exit
 
324
fi
 
325
 
 
326
# Check source file exists and is readable.
 
327
if [ ! -f "$sourcefile" ]
 
328
then
 
329
        script_exit_code=66
 
330
        output "Source file not valid." 1
 
331
        script_exit
 
332
fi
 
333
 
 
334
if [ ! -r "$sourcefile" ]
 
335
then
 
336
        script_exit_code=77
 
337
        output "Source file not accessible." 1
 
338
        script_exit
 
339
fi
 
340
 
 
341
# Now ensure source file is in Unix text format.
 
342
wutconv --unix $sourcefile
 
343
std_cmd_err_handler $?
 
344
 
 
345
# Check target directory exists and is writable.
 
346
if [ "$targetdir" = "" ]
 
347
then
 
348
        targetdir=$PWD
 
349
fi
 
350
 
 
351
if [ ! -d "$targetdir" ]
 
352
then
 
353
        script_exit_code=66
 
354
        output "Target directory not valid." 1
 
355
        script_exit
 
356
fi
 
357
 
 
358
if [ ! -w "$targetdir" ]
 
359
then
 
360
        script_exit_code=77
 
361
        output "Target directory not accessible." 1
 
362
        script_exit
 
363
fi
 
364
 
 
365
# Now get the files
 
366
IFS="/"
 
367
exec 3<"$sourcefile"
 
368
while read -u3 -ra input
 
369
do
 
370
        outputfile=${input[${#input[*]}-1]}
 
371
        output "Attempting to get file - ""${input[*]}" 0
 
372
        output "Attempting to save file to - ""$targetdir/$outputfile" 0
 
373
 
 
374
        wget $verbosity --no-check-certificate -O "$targetdir/$outputfile" "${input[*]}"
 
375
        status=$?
 
376
        
 
377
        output "File get completed with status "$status $status
 
378
        ((script_exit_code = script_exit_code + status))
 
379
        
 
380
        if [ $script_exit_code = 0 ]
 
381
        then
 
382
                # If Windows CR/LF line ending required, convert.
 
383
                if [ $windows = TRUE ]
 
384
                then
 
385
                        wutconv --windows --verbose "$targetdir/$outputfile"
 
386
                        status=$?
 
387
                        ((script_exit_code = script_exit_code + status))
 
388
                fi
 
389
                # If Unix LF line ending required, convert.
 
390
                if [ $unix = TRUE ]
 
391
                then
 
392
                        wutconv --unix --verbose "$targetdir/$outputfile"
 
393
                        status=$?
 
394
                        ((script_exit_code = script_exit_code + status))
 
395
                fi
 
396
                # Change owner?
 
397
                if [ $owner = TRUE ]
 
398
                then
 
399
                        chown $newowner "$targetdir/$outputfile"
 
400
                        status=$?
 
401
                        output "Owner change complete with status "$status $status
 
402
                        ((script_exit_code = script_exit_code + status))
 
403
                fi
 
404
                #Change group?
 
405
                if [ $ownergroup = TRUE ]
 
406
                then
 
407
                        chown :$newownergroup "$targetdir/$outputfile"
 
408
                        status=$?
 
409
                        output "Owner group change complete with status "$status $status
 
410
                        ((script_exit_code = script_exit_code + status))
 
411
                fi
 
412
        fi
 
413
done
 
414
exec 3<&-
 
415
 
 
416
# If persist is TRUE, save parameters.
 
417
if [ $persist = TRUE ]
 
418
then
 
419
        rm -f ~/$conffile
 
420
        write_file
 
421
fi
 
422
 
 
423
# And exit
 
424
output "Script complete with exit code: "$script_exit_code $script_exit_code
 
425
script_exit