2
#########################################################################
4
# wutconv is automatically generated, #
5
# please do not modify! #
7
#########################################################################
9
#########################################################################
11
# Script ID: wutconv #
12
# Author: Copyright (C) 2014-2017 Mark Grant #
14
# Released under the GPLv3 only. #
15
# SPDX-License-Identifier: GPL-3.0 #
18
# Converts one or more text files to windows or unix line endings, or, #
19
# if no text files are specified on the command line, then stdin is #
20
# converted to stdout. #
22
# Syntax: wutconv [OPTIONS] [TextFileName] [TextFileName1] ... #
24
# -h, --help Displays usage information. #
25
# -u, --unix Convert to Unix line endings. #
26
# -V, --version Displays version information. #
27
# -w, --windows Convert to Windows line endings. #
29
# Exit Codes: 0 - success #
32
#########################################################################
34
#########################################################################
38
# Date Author Version Description #
40
# 04/09/2014 MG 1.0.1 First release. #
41
# 29/11/2014 MG 1.0.2 Switch to use GNU getopt to enable long #
43
# 29/11/2014 MG 1.0.3 Add overall package version to -V. #
44
# 01/12/2014 MG 1.0.4 Implement conversion of stdin to stdout #
45
# if no text files are specified on the #
47
# 01/12/2014 MG 1.0.5 Implement verbose option. Default is #
48
# now quiet - no status output. #
49
# 01/12/2014 MG 1.0.6 Check that both -u and -w have not been #
51
# 26/06/2015 MG 1.0.7 Remove BSD support. #
52
# 23/10/2015 MG 1.0.8 Remove redundant osname look-up. #
53
# 01/07/2017 MG 1.0.9 Enforce 80 column rule. #
54
# 02/12/2017 MG 1.0.10 Add SPDX license tags to source files. #
55
# Adopt normal exit code policy; 0 on #
56
# success, 1 on failure. #
58
#########################################################################
65
version="1.0.10" # set version variable
66
outputprefix="wutconv: "
67
packageversion=@pkgversion@ # Version of the complete package
77
# Output $1 to stdout or stderr depending on $2
82
if [ "$verbosity" = "-v" ]
84
echo "$outputprefix$1"
87
echo "$outputprefix$1" 1>&2
91
# Standard function to test command error ($1 is $?) and exit if non-zero
101
# Standard function to tidy up and return exit code
104
exit $script_exit_code
107
# Standard trap exit function
111
output "Script terminating due to trap received. Code: "$script_exit_code 1
116
trap trap_exit SIGHUP SIGINT SIGTERM
121
# Process command line arguments with GNU getopt.
123
# Options u and w are mutually exclusive.
125
GETOPTTEMP=`getopt -o huvVw --long help,unix,verbose,version,windows \
127
std_cmd_err_handler $?
129
eval set -- "$GETOPTTEMP"
130
std_cmd_err_handler $?
136
echo "Usage is $0 [OPTIONS]" TextFileName TextFileName1 ...
137
echo " [OPTIONS] are:-"
138
echo " '-h or --help' Displays usage information."
139
echo " '-u or --unix' Convert to Unix line endings."
140
echo " '-v or --verbose' verbose output"
141
echo " '-V or --version' Displays version information."
142
echo " '-w or --windows' Convert to Windows line endings."
148
if [ $windows = TRUE ]
151
output "Cannot specify options u AND w." 1
157
-v|--verbose) verbosity="-v"
161
echo "$0 Script version "$version
162
echo "$0 Package version "$packageversion
171
output "Cannot specify options w AND u." 1
180
*) script_exit_code=1
181
output "Internal error." 1
187
# Either u or w flag must be set.
188
if [ $windows = FALSE -a $unix = FALSE ]
191
output "Either -u or -w must be set." 1
195
# u and w flag cannot both be set.
196
if [ $windows = TRUE -a $unix = TRUE ]
199
output "-u and -w cannot both be set." 1
203
# If no cmd line filenames then use stdin and stdout.
207
then # Convert to Unix line endings.
208
awk '{if (substr($0,length,1)=="\r")
211
else # Convert to Windows line endings.
212
awk '{if (substr($0,length,1)!="\r")
218
output "Script complete with exit code: "$script_exit_code \
223
# Otherwise process cmd line filenames in sequence.
224
# The for line == for arg in $@
227
if [ -f $arg ] # Is it a regular file.
229
if [ -r $arg -a -w $arg ] # Is it readable and writable.
232
then # Convert to Unix line endings.
233
awk '{if (substr($0,length,1)=="\r")
235
print;}' $arg > $arg.tmp \
236
&& mv -f $arg.tmp $arg
238
msg="Converted file "$arg" to Unix format "
239
msg+="complete with status "$status
240
output "$msg" $status
241
((script_exit_code=$script_exit_code + $status))
242
else # Convert to Windows line endings.
243
awk '{if (substr($0,length,1)!="\r")
245
print;}' $arg > $arg.tmp \
246
&& mv -f $arg.tmp $arg
248
msg="Converted file "$arg" to Windows format "
249
msg+="complete with status "$status
250
output "$msg" $status
251
((script_exit_code=$script_exit_code + $status))
254
msg="File "$arg" does not have the correct permissions."
256
((script_exit_code=$script_exit_code + 1))
259
output "File "$arg" does not exist or is not a regular file." 1
260
((script_exit_code=$script_exit_code + 1))
266
output "Script complete with exit code: "$script_exit_code $script_exit_code