2
##########################################################################
4
## mget is automatically generated, ##
5
## please do not modify! ##
7
##########################################################################
9
##########################################################################
12
## Author: Mark Grant ##
15
## Retrieves one or more files whose URL's are specified in a text file.##
17
## Syntax: mget [OPTIONS] ##
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 ##
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 ##
35
## Exit Codes: 0 & 64 - 113 as per C/C++ standard ##
37
## 64 - Invalid arguments ##
38
## 66 - File access error ##
39
## 67 - trap received ##
40
## 77 - Permissions error ##
42
##########################################################################
44
##########################################################################
48
## Date Author Version Description ##
50
## 29/04/2013 MG 1.0.1 Created.Does not implement mail ##
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 ##
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 ##
102
## 28/11/2014 MG 1.1.2 Switch to use GNU getopt to enable long ##
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. ##
113
##########################################################################
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
132
# wget defaults to verbose so make non-verbose default here.
141
# Output $1 to stdout or stderr depending on $2
146
echo "$outputprefix$1"
148
echo "$outputprefix$1" 1>&2
152
# Standard function to test command error ($1 is $?) and exit if non-zero
153
std_cmd_err_handler()
162
# Standard function to tidy up and return exit code
165
exit $script_exit_code
168
# Standard trap exit function
172
output "Script terminating due to trap received. Code: "$script_exit_code 1
177
trap trap_exit SIGHUP SIGINT SIGTERM
179
# Function to write config file.
182
echo "sourcefile=$sourcefile">>~/.mget
183
echo "targetdir=$targetdir">>~/.mget
189
# If config file exists, read it, else, create it.
190
if [ ! -f ~/$conffile ]
196
while read -u3 -ra input
200
sourcefile=${input[1]}
203
targetdir=${input[1]}
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 \
214
std_cmd_err_handler $?
216
eval set -- "$GETOPTTEMP"
217
std_cmd_err_handler $?
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."
263
exec 1>/dev/null 2>/dev/null
283
echo "$0 Script version "$version
284
echo "$0 Package version "$packageversion
296
*) script_exit_code=1
297
output "Internal error." 1
303
# Script does not accept other arguments.
307
output "Invalid argument." 1
311
# If only persist flag set then nothing to do, exit.
312
if [ $# = 1 -a "$1" = "-p" ]
318
# u and w flag cannot both be set.
319
if [ $windows = TRUE -a $unix = TRUE ]
322
output "-u and -w cannot both be set." 1
326
# Check source file exists and is readable.
327
if [ ! -f "$sourcefile" ]
330
output "Source file not valid." 1
334
if [ ! -r "$sourcefile" ]
337
output "Source file not accessible." 1
341
# Now ensure source file is in Unix text format.
342
wutconv --unix $sourcefile
343
std_cmd_err_handler $?
345
# Check target directory exists and is writable.
346
if [ "$targetdir" = "" ]
351
if [ ! -d "$targetdir" ]
354
output "Target directory not valid." 1
358
if [ ! -w "$targetdir" ]
361
output "Target directory not accessible." 1
368
while read -u3 -ra input
370
outputfile=${input[${#input[*]}-1]}
371
output "Attempting to get file - ""${input[*]}" 0
372
output "Attempting to save file to - ""$targetdir/$outputfile" 0
374
wget $verbosity --no-check-certificate -O "$targetdir/$outputfile" "${input[*]}"
377
output "File get completed with status "$status $status
378
((script_exit_code = script_exit_code + status))
380
if [ $script_exit_code = 0 ]
382
# If Windows CR/LF line ending required, convert.
383
if [ $windows = TRUE ]
385
wutconv --windows --verbose "$targetdir/$outputfile"
387
((script_exit_code = script_exit_code + status))
389
# If Unix LF line ending required, convert.
392
wutconv --unix --verbose "$targetdir/$outputfile"
394
((script_exit_code = script_exit_code + status))
399
chown $newowner "$targetdir/$outputfile"
401
output "Owner change complete with status "$status $status
402
((script_exit_code = script_exit_code + status))
405
if [ $ownergroup = TRUE ]
407
chown :$newownergroup "$targetdir/$outputfile"
409
output "Owner group change complete with status "$status $status
410
((script_exit_code = script_exit_code + status))
416
# If persist is TRUE, save parameters.
417
if [ $persist = TRUE ]
424
output "Script complete with exit code: "$script_exit_code $script_exit_code