diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:48:59 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:48:59 +0000 |
commit | c484829272cd13a738e35412498e12f2c9a194ac (patch) | |
tree | a1f5ec09629ee895bd3963fa8820b45f2f4c574b /bin/unpack-zipped-xml.sh | |
parent | Initial commit. (diff) | |
download | liborcus-upstream/0.19.2.tar.xz liborcus-upstream/0.19.2.zip |
Adding upstream version 0.19.2.upstream/0.19.2upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'bin/unpack-zipped-xml.sh')
-rwxr-xr-x | bin/unpack-zipped-xml.sh | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/bin/unpack-zipped-xml.sh b/bin/unpack-zipped-xml.sh new file mode 100755 index 0000000..ac22fe9 --- /dev/null +++ b/bin/unpack-zipped-xml.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +# global constants +outdir=xml + +function abort() +{ + (>&2 echo "$1") # output to stderr + exit 1 +} + +filepath="$1" + +if [ -z "$filepath" ]; then + abort "file path is not given." +fi + +shift + +# convert the file path to absolute path. +filepath=`realpath "$filepath"` + +# remove existing output directory if one exists. +if [ -d $outdir ]; then + rm -rf $outdir || abort "failed to remove the existing output directory '$outdir'." +fi + +mkdir $outdir || abort "failed to create an output directory '$outdir'." + +# unzip all inside the output directory. +cd $outdir +unzip "$filepath" > /dev/null || abort "failed to unzip $filepath." + +# temporarily replace bash's internal field separators to handle file names with spaces. +_IFS="$IFS" +IFS=$'\n' + +for _file in $(find . -type f); do + _mimetype=`file --mime-type --brief "$_file"` || abort "failed to determine the mime type of $_file." + if [ $_mimetype = "application/xml" ] || [ $_mimetype = "text/xml" ]; then + # beautify the XML file content. + _temp=$(mktemp) || abort "failed to create a temporary file." + xmllint --format $_file > $_temp || abort "failed to run xmllint on $_file." + mv $_temp $_file || abort "failed to update $_file." + fi +done + +# restore the original separators. +IFS="$_IFS" |