| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # ========================================================================== |
| 3 | # _ _ _ ____ ____ _____ |
| 4 | # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __ |
| 5 | # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__| |
| 6 | # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ | |
| 7 | # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_| |
| 8 | # |
| 9 | # --- High-Performance Connectivity Tracer (HiPerConTracer) --- |
| 10 | # https://www.nntb.no/~dreibh/hipercontracer/ |
| 11 | # ========================================================================== |
| 12 | # |
| 13 | # High-Performance Connectivity Tracer (HiPerConTracer) |
| 14 | # Copyright (C) 2015-2026 by Thomas Dreibholz |
| 15 | # |
| 16 | # This program is free software: you can redistribute it and/or modify |
| 17 | # it under the terms of the GNU General Public License as published by |
| 18 | # the Free Software Foundation, either version 3 of the License, or |
| 19 | # (at your option) any later version. |
| 20 | # |
| 21 | # This program is distributed in the hope that it will be useful, |
| 22 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | # GNU General Public License for more details. |
| 25 | # |
| 26 | # You should have received a copy of the GNU General Public License |
| 27 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | # |
| 29 | # Contact: [email protected] |
| 30 | |
| 31 | # Bash options: |
| 32 | set -euo pipefail |
| 33 | |
| 34 | |
| 35 | # ###### Usage ############################################################## |
| 36 | usagescip-shell shell shell-project 0.0.0 usage(). () { |
| 37 | local exitCode="$1" |
| 38 | echoscip-shell system . . echo. >&2 "Usage: $0 database_configuration_file ... [--join] [-h|--help] [-v|--version]" |
| 39 | exit "${exitCode}" |
| 40 | } |
| 41 | |
| 42 | |
| 43 | # ###### Version ############################################################ |
| 44 | versionscip-shell shell shell-project 0.0.0 version(). () { |
| 45 | echoscip-shell system . . echo. "Make-DBeaver-Configuration @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@" |
| 46 | exit 0 |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | # ###### Main program ####################################################### |
| 52 | |
| 53 | # ====== Handle arguments =================================================== |
| 54 | if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then |
| 55 | echoscip-shell system . . echo. "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!" |
| 56 | exit 1 |
| 57 | fi |
| 58 | UNAME="$(unamescip-shell system . . uname.)" |
| 59 | case "${UNAME}" in |
| 60 | FreeBSD) |
| 61 | GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || truescip-shell system . . true.)" |
| 62 | ;; |
| 63 | NetBSD) |
| 64 | GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || truescip-shell system . . true.)" |
| 65 | ;; |
| 66 | OpenBSD) |
| 67 | GETOPT="$(command -v gnugetopt || truescip-shell system . . true.)" |
| 68 | ;; |
| 69 | Darwin) |
| 70 | GETOPT="$(envscip-shell system . . env. PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || truescip-shell system . . true.)" |
| 71 | ;; |
| 72 | *) |
| 73 | GETOPT="$(command -v getopt || truescip-shell system . . true.)" |
| 74 | ;; |
| 75 | esac |
| 76 | if "${GETOPT}" -T >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 || [[ $? -ne 4 ]]; then |
| 77 | echoscip-shell system . . echo. >&2 "ERROR: Enhanced/GNU getopt is required!" |
| 78 | exit 1 |
| 79 | fi |
| 80 | options="$(${GETOPT} -o jhv --long join,help,version -a -- "$@")" |
| 81 | # shellcheck disable=SC2181 |
| 82 | if [[ $? -ne 0 ]]; then |
| 83 | usagescip-shell shell shell-project 0.0.0 usage(). 1 |
| 84 | fi |
| 85 | |
| 86 | JOIN=0 |
| 87 | eval set -- "${options}" |
| 88 | while [scip-shell system . . `[`. $# -gt 0 ] ; do |
| 89 | case "$1" in |
| 90 | -j | --join) |
| 91 | JOIN=1 |
| 92 | ;; |
| 93 | -h | --help) |
| 94 | usagescip-shell shell shell-project 0.0.0 usage(). 0 |
| 95 | ;; |
| 96 | -v | --version) |
| 97 | versionscip-shell shell shell-project 0.0.0 version(). |
| 98 | ;; |
| 99 | --) |
| 100 | shift |
| 101 | break |
| 102 | ;; |
| 103 | *) |
| 104 | # This should not happen: wrong getopt parameters, or missing case? |
| 105 | echoscip-shell system . . echo. >&2 "INTERNAL ERROR: Unhandled option $1!" |
| 106 | exit 1 |
| 107 | ;; |
| 108 | esac |
| 109 | shift |
| 110 | done |
| 111 | if [scip-shell system . . `[`. $# -lt 1 ] ; then |
| 112 | usagescip-shell shell shell-project 0.0.0 usage(). 1 |
| 113 | fi |
| 114 | DIRNAME="$(dirnamescip-shell system . . dirname. "$0")" |
| 115 | |
| 116 | |
| 117 | # ====== Process configuration files ======================================== |
| 118 | echoscip-shell system . . echo. -e "\x1b[34m$(datescip-shell system . . date. +"%F %H:%M:%S"): Generating DBeaver configuration ...\x1b[0m" |
| 119 | tempDirectory="$(mktempscip-shell system . . mktemp. -d -t "make-dbeaver-configuration-XXXXXX")" |
| 120 | trap 'rm -rf "${tempDirectory}"' EXIT |
| 121 | |
| 122 | dbeaverConfig="" |
| 123 | dbeaverCredentials="" |
| 124 | while [scip-shell system . . `[`. $# -gt 0 ] ; do |
| 125 | echoscip-shell system . . echo. -e "\x1b[37mProcessing $(basenamescip-shell system . . basename. "$1") ...\x1b[0m" |
| 126 | "${DIRNAME}/dbshell" "$1" --dry-run --quiet --write-dbeaver-config "${tempDirectory}/$(basenamescip-shell system . . basename. "${1//.conf/}")" |
| 127 | shift |
| 128 | done |
| 129 | |
| 130 | |
| 131 | # ====== Read existing configuration ======================================== |
| 132 | if [scip-shell system . . `[`. ${JOIN} -eq 1 ] ; then |
| 133 | echoscip-shell system . . echo. -e "\x1b[37mLooking for existing DBeaver configuration ...\x1b[0m" |
| 134 | for dbeaverDirectory in \ |
| 135 | "${HOME}/snap/dbeaver-ce/current/.local/share/DBeaverData/workspace6/General/.dbeaver" \ |
| 136 | "${HOME}/.local/share/DBeaverData/workspace6/General/.dbeaver" ; do |
| 137 | if [scip-shell system . . `[`. -e "${dbeaverDirectory}" ] ; then |
| 138 | if [scip-shell system . . `[`. -e "${dbeaverDirectory}/data-sources.json" ] ; then |
| 139 | dbeaverConfig="${dbeaverDirectory}/data-sources.json" |
| 140 | echoscip-shell system . . echo. -e "\x1b[37mJoining data sources in ${dbeaverConfig}\x1b[0m" |
| 141 | fi |
| 142 | if [scip-shell system . . `[`. -e "${dbeaverDirectory}/credentials-config.json" ] ; then |
| 143 | dbeaverCredentials="${tempDirectory}/credentials-config.json" |
| 144 | echoscip-shell system . . echo. -e "\x1b[37mJoining credentials in ${dbeaverDirectory}/credentials-config.json\x1b[0m" |
| 145 | "${DIRNAME}/decrypt-dbeaver-configuration" \ |
| 146 | "${dbeaverDirectory}/credentials-config.json" \ |
| 147 | "${dbeaverCredentials}" |
| 148 | fi |
| 149 | break |
| 150 | fi |
| 151 | done |
| 152 | if [scip-shell system . . `[`. ! -e "${dbeaverDirectory}" ] ; then |
| 153 | echoscip-shell system . . echo. >&2 "ERROR: Unable to find DBeaver configuration directory!" |
| 154 | exit 1 |
| 155 | fi |
| 156 | fi |
| 157 | |
| 158 | |
| 159 | # ====== Join new DBeaver configuration files =============================== |
| 160 | echoscip-shell system . . echo. -e "\x1b[37mJoining configurations ...\x1b[0m" |
| 161 | # shellcheck disable=SC2086 |
| 162 | jqscip-shell system . . jq. --sort-keys --slurp 'reduce .[] as $item ({}; . * $item)' ${dbeaverConfig} "${tempDirectory}"/*-data-source.json >"${tempDirectory}/data-sources.json" |
| 163 | # shellcheck disable=SC2086 |
| 164 | jqscip-shell system . . jq. --sort-keys --slurp 'reduce .[] as $item ({}; . * $item)' ${dbeaverCredentials} "${tempDirectory}"/*-credentials-config.json >"${tempDirectory}/credentials-config.json.plaintext" |
| 165 | |
| 166 | # Encrypt the new credentials files: |
| 167 | "${DIRNAME}/encrypt-dbeaver-configuration" \ |
| 168 | "${tempDirectory}/credentials-config.json.plaintext" \ |
| 169 | "${tempDirectory}/credentials-config.json" |
| 170 | |
| 171 | |
| 172 | # ====== Join with existing configuration =================================== |
| 173 | if [scip-shell system . . `[`. ${JOIN} -eq 1 ] ; then |
| 174 | if [scip-shell system . . `[`. "${dbeaverConfig}" != "" ] ; then |
| 175 | if ! diffscip-shell system . . diff. "${tempDirectory}/data-sources.json" "${dbeaverDirectory}/data-sources.json" >/dev/nullscip-shell filesystem . . `/dev/null`. ; then |
| 176 | dbeaverConfigBackup="${dbeaverConfig}.backup-$(datescip-shell system . . date. +"%F-%H:%M:%S")" |
| 177 | echoscip-shell system . . echo. -e "\x1b[37mMaking data sources backup in ${dbeaverConfigBackup} ...\x1b[0m" |
| 178 | cpscip-shell system . . cp. "${dbeaverConfig}" "${dbeaverConfigBackup}" |
| 179 | fi |
| 180 | fi |
| 181 | echoscip-shell system . . echo. -e "\x1b[37mUpdating data sources ${dbeaverDirectory}/data-sources.json ...\x1b[0m" |
| 182 | mvscip-shell system . . mv. "${tempDirectory}/data-sources.json" "${dbeaverDirectory}/data-sources.json" |
| 183 | |
| 184 | if [scip-shell system . . `[`. "${dbeaverCredentials}" != "" ] ; then |
| 185 | if ! diffscip-shell system . . diff. "${dbeaverCredentials}" "${tempDirectory}/credentials-config.json.plaintext" >/dev/nullscip-shell filesystem . . `/dev/null`. ; then |
| 186 | dbeaverCredentialsBackup="${dbeaverDirectory}/credentials-config.json-$(datescip-shell system . . date. +"%F-%H:%M:%S")" |
| 187 | echoscip-shell system . . echo. -e "\x1b[37mKeeping backup in ${dbeaverCredentialsBackup} ...\x1b[0m" |
| 188 | cpscip-shell system . . cp. "${dbeaverCredentials}" "${dbeaverCredentialsBackup}" |
| 189 | fi |
| 190 | fi |
| 191 | echoscip-shell system . . echo. -e "\x1b[37mUpdating credentials ${dbeaverDirectory}/credentials-config.json ...\x1b[0m" |
| 192 | mvscip-shell system . . mv. "${tempDirectory}/credentials-config.json" "${dbeaverDirectory}/credentials-config.json" |
| 193 | |
| 194 | else |
| 195 | |
| 196 | # Just generated the configuration files -> no joining was performed! |
| 197 | mvscip-shell system . . mv. "${tempDirectory}/data-sources.json" "${tempDirectory}/credentials-config.json" . |
| 198 | echoscip-shell system . . echo. "Generated configuration files:" |
| 199 | lsscip-shell system . . ls. -l credentials-config.json data-sources.json |
| 200 | |
| 201 | fi |
| 202 | |
| 203 | |
| 204 | # ====== Clean up =========================================================== |
| 205 | echoscip-shell system . . echo. -e "\x1b[37mDone!\x1b[0m" |
| 206 | rmscip-shell system . . rm. -rf "${tempDirectory}" |