-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.fold-artwork.sh
executable file
·129 lines (110 loc) · 2.52 KB
/
.fold-artwork.sh
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
#!/bin/bash
#
# the only reason why /bin/sh isn't being used
# is because "echo -n" is broken on the Mac.
print_usage() {
echo
echo "Wraps file representing IETF artwork at specified column"
echo "according to BCP XX. Note, this routine does nothing if"
echo "the infile has no lines longer than specified."
echo
echo "Usage: $0 [-r] [-c <col>] -i <infile> -o <outfile>"
echo
echo " -c: column to wrap on (default: 69)"
echo " -r: reverses the operation"
echo " -i: the input filename"
echo " -o: the output filename"
echo " -h: show this message"
echo
echo "Exit status code: zero on success, non-zero otherwise."
echo
}
# global vars, do not edit
reversed=0
infile=""
outfile=""
maxcol=70 # default, may be overridden by param
header="[Note: '\' line wrapping for formatting only]\n\n"
fold_it() {
# check if file needs folding
grep ".\{$maxcol\}" $infile >> /dev/null 2>&1
if [ $? -ne 0 ]; then
# nothing to do
cp $infile $outfile
return 1
fi
>&2 echo "folding $infile..."
echo -ne "$header" > $outfile
foldcol=`expr "$maxcol" - 2` # for the inserted '\' & '\n' chars
gsed "/.\{$maxcol\}/s/\(.\{$foldcol\}\)/\1\\\\\n/g" < $infile >> $outfile
return 0
}
unfold_it() {
# count lines in header
numlines=`echo -ne "$header" | wc -l`
# check if file needs unfolding
echo -ne "$header" > /tmp/header
head -n $numlines $infile > /tmp/header2
diff -q /tmp/header /tmp/header2 >> /dev/null
code=$?
rm /tmp/header /tmp/header2
if [ $code -ne 0 ]; then
# nothing to do
cp $infile $outfile
return 1
fi
awk "NR>$numlines" $infile > /tmp/wip
gsed ':x; /\\$/ { N; s/\\\n//; tx }' /tmp/wip > $outfile
rm /tmp/wip
return 0
}
process_input() {
while [ "$1" != "" ]; do
if [ "$1" == "-h" -o "$1" == "--help" ]; then
print_usage
exit 1
fi
if [ "$1" == "-c" ]; then
maxcol="$2"
shift
fi
if [ "$1" == "-r" ]; then
reversed=1
fi
if [ "$1" == "-i" ]; then
infile="$2"
shift
fi
if [ "$1" == "-o" ]; then
outfile="$2"
shift
fi
shift
done
if [ -z "$infile" ]; then
echo
echo "Error: infile parameter missing (use -h for help)"
echo
exit 1
fi
if [ -z "$outfile" ]; then
echo
echo "Error: outfile parameter missing (use -h for help)"
echo
exit 1
fi
}
main() {
if [ "$#" == "0" ]; then
print_usage
exit 1
fi
process_input $@
if [[ $reversed -eq 0 ]]; then
fold_it
else
unfold_it
fi
exit 0
}
main "$@"