This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
forked from aegif/CmisSync
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathremovebom
executable file
·182 lines (138 loc) · 3.81 KB
/
removebom
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
#some file i found on the net which can remove BOM special
#charaters from files. use with care
set -o nounset
set -o errexit
DELETE_ORIG=true
DELETE_FLAG=""
RECURSIVE=false
PROCESSALLFILE=false
PROCESSING_FILES=false
PROCESSALLFILE_FLAG=""
SED_EXEC=sed
USE_EXT=false
FILE_EXT=""
TMP_CMD="mktemp"
TMP_OPTS="--tmpdir="
XDEV=""
ISDARWIN=false
if [ $(uname) == "SunOS" ] ; then
if [ -x /usr/gnu/bin/sed ] ; then
echo "Using GNU sed..."
SED_EXEC=/usr/gnu/bin/sed
fi
TMP_OPTS="-p "
fi
if [ $(uname) == "Darwin" ] ; then
TMP_OPTS="-t tmp"
SED_EXEC="perl -pe"
echo "Using perl..."
ISDARWIN=true
fi
function usage() {
echo "bom-remove [-adrx] [-s sed-name] [-e ext] files..."
echo ""
echo " -a Remove the BOM throughout the entire file."
echo " -e Look only for files with the chosen extensions."
echo " -d Do not overwrite original files and do not remove temp files."
echo " -r Scan subdirectories."
echo " -s Specify an alternate sed implementation."
echo " -x Don't descend directories in other filesystems."
}
function checkExecutable() {
if ( ! which "$1" > /dev/null 2>&1 ); then
echo "Cannot find executable:" $1
exit 4
fi
}
function parseArgs() {
while getopts "adfrs:e:x" flag
do
case $flag in
a) PROCESSALLFILE=true ; PROCESSALLFILE_FLAG="-a" ;;
r) RECURSIVE=true ;;
f) PROCESSING_FILES=true ;;
s) SED_EXEC=$OPTARG ;;
e) USE_EXT=true ; FILE_EXT=$OPTARG ;;
d) DELETE_ORIG=false ; DELETE_FLAG="-d" ;;
x) XDEV="-xdev" ;;
*) echo "Unknown parameter." ; usage ; exit 2 ;;
esac
done
shift $(($OPTIND - 1))
if [ $# == 0 ] ; then
usage;
exit 2;
fi
# fixing darwin
if [[ $ISDARWIN == true && $PROCESSALLFILE == false ]] ; then
PROCESSALLFILE=true
echo "Process all file is implicitly set on Darwin."
fi
FILES=("$@")
if [ ! -n "$FILES" ]; then
echo "No files specified. Exiting."
fi
if [ $RECURSIVE == true ] && [ $PROCESSING_FILES == true ] ; then
echo "Cannot use -r and -f at the same time."
usage
exit 1
fi
checkExecutable $SED_EXEC
checkExecutable $TMP_CMD
}
function processFile() {
if [ $(uname) == "Darwin" ] ; then
TEMPFILENAME=$($TMP_CMD $TMP_OPTS)
else
TEMPFILENAME=$($TMP_CMD $TMP_OPTS"$(dirname "$1")")
fi
echo "Processing $1 using temp file $TEMPFILENAME"
if [ $PROCESSALLFILE == false ] ; then
cat "$1" | $SED_EXEC '1 s/\xEF\xBB\xBF//' > "$TEMPFILENAME"
else
cat "$1" | $SED_EXEC 's/\xEF\xBB\xBF//g' > "$TEMPFILENAME"
fi
if [ $DELETE_ORIG == true ] ; then
if [ ! -w "$1" ] ; then
echo "$1 is not writable. Leaving tempfile."
else
echo "Removing temp file..."
mv "$TEMPFILENAME" "$1"
fi
fi
}
function doJob() {
# Check if the script has been called from the outside.
if [ $PROCESSING_FILES == true ] ; then
for i in $(seq 1 ${#FILES[@]})
do
echo ${FILES[$i-1]}
processFile "${FILES[$i-1]}"
done
else
# processing every file
for i in $(seq 1 ${#FILES[@]})
do
CURRFILE=${FILES[$i-1]}
# checking if file or directory exist
if [ ! -e "$CURRFILE" ] ; then echo "File not found: $CURRFILE. Skipping..." ; continue ; fi
# if a paremeter is a directory, process it recursively if RECURSIVE is set
if [ -d "$CURRFILE" ] ; then
if [ $RECURSIVE == true ] ; then
if [ $USE_EXT == true ] ; then
find "$CURRFILE" $XDEV -type f -name "*.$FILE_EXT" -exec "$0" $DELETE_FLAG $PROCESSALLFILE_FLAG -f "{}" \;
else
find "$CURRFILE" $XDEV -type f -exec "$0" $DELETE_FLAG $PROCESSALLFILE_FLAG -f "{}" \;
fi
else
echo "$CURRFILE is a directory. Skipping..."
fi
else
processFile "$CURRFILE"
fi
done
fi
}
parseArgs "$@"
doJob