]> Wikimedia Canada | Git repositories - eccc_to_commons.git/blobdiff - eccc_fixer.sh
Complete process from ECCC website to Commons files generation
[eccc_to_commons.git] / eccc_fixer.sh
diff --git a/eccc_fixer.sh b/eccc_fixer.sh
new file mode 100755 (executable)
index 0000000..9d8cc91
--- /dev/null
@@ -0,0 +1,45 @@
+#!/bin/bash
+
+# eccc_fixer.sh - Create Environment and Climate change Canada historical data
+#                 files download list
+# Copyright (C) 2019-2020  Pierre Choffet
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+set -ex
+
+SOURCE="${1}"
+DESTINATION="${2}"
+
+if [ -z "${SOURCE}" ]||[ -z "${DESTINATION}" ]||[ -d "${DESTINATION}" ]
+then
+       echo 'Fix XMLs provided by Environment and Climate change Canada'
+       echo 'Usage: eccc_fixer.sh <source folder> <destination folder>'
+       echo ''
+       echo 'To prevent wrong usage, <destination folder> must not exist.'
+       exit 1
+fi
+
+# Recreate folders structure
+while IFS= read -r -d '' -u 9
+do
+       mkdir -p -- "${DESTINATION}/$(realpath --relative-to "${SOURCE}" "${REPLY}")"
+
+done 9< <( find "${SOURCE}" -type d -print0 )
+
+# Transform original files
+while IFS= read -r -d '' -u 9
+do
+       xmlstarlet tr eccc_fixer.xslt "${REPLY}" > "${DESTINATION}/$(realpath --relative-to "${SOURCE}" "${REPLY}")"
+done 9< <( find "${SOURCE}" -type f -name '*.xml' -print0 )