forked from SETI/rms-hst-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-apt-to-utf8
executable file
·62 lines (50 loc) · 1.23 KB
/
convert-apt-to-utf8
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
#!/bin/bash
die(){
echo $@ >&2
}
usage(){
die Usage: "$(basename $0)" '<apt-file>'
}
if [ $# -ne 1 ]; then
usage
fi
APT_FILE=$1
shift
# Backup the APT file.
# cp $APT_FILE $APT_FILE.orig
# Check the original encoding and set the comment to insert.
if grep -q '<?xml version="1.0" encoding="UTF-8"?>' $APT_FILE; then
# It's already UTF-8
ACTION="File reformatted"
else
# It's not UTF-8
ACTION="File encoding converted to UTF-8 and reformatted"
fi
COMMENT="<!-- $ACTION by the PDS RMS Node -->"
# Make a temporary directory for conversion.
TEMP_DIR=$(mktemp -d "${TMPDIR:-/tmp/}$(basename $0).XXXXXXXXXXXX")
trap "rm -rf $TEMP_DIR" EXIT
# Reencode.
UTF8_FILE=$TEMP_DIR/$(basename $APT_FILE).utf8
xsltproc xml/convert-to-utf8.xslt $APT_FILE > $UTF8_FILE
if [[ $? -ne 0 ]]; then
die xsltproc failed
exit 1
fi
# Comment.
COMMENTED_FILE=$TEMP_DIR/$(basename $APT_FILE).commented
sed "s/<?xml .*/&$COMMENT/" < $UTF8_FILE > $COMMENTED_FILE
# Reformat.
PRETTY_FILE=$TEMP_DIR/$(basename $APT_FILE).pretty
xmllint --format --output $PRETTY_FILE $COMMENTED_FILE
if [[ $? -ne 0 ]]; then
die xmllint failed
exit 1
fi
# Copy back.
mv $PRETTY_FILE $APT_FILE
if [[ $? -ne 0 ]]; then
die mv failed
exit 1
fi
exit 0