|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -eu |
| 4 | + |
| 5 | +#### Goto repository root |
| 6 | +cd "$(git rev-parse --show-toplevel)" >/dev/null |
| 7 | + |
| 8 | +SCRIPT_DIR="$(dirname "$0")" |
| 9 | +WRAPPER_SOURCE="$SCRIPT_DIR/stubs/.devcontainer/.zscaler/zscaler-curl.sh" |
| 10 | +CURLRC_SOURCE="$SCRIPT_DIR/stubs/.devcontainer/.zscaler/root/.curlrc" |
| 11 | +WRAPPER_TARGET="/usr/local/bin/zscaler-curl" |
| 12 | +CURL_BIN="/usr/bin/curl" |
| 13 | +CURL_REAL_BIN="/usr/bin/curl.real" |
| 14 | +CURRENT_USER="${SUDO_USER:-$(id -un)}" |
| 15 | +CURRENT_HOME="$(getent passwd "$CURRENT_USER" | cut -d: -f6)" |
| 16 | + |
| 17 | +run_as_root() { |
| 18 | + if [ "$(id -u)" -eq 0 ]; then |
| 19 | + "$@" |
| 20 | + else |
| 21 | + sudo "$@" |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | +if [ ! -f "$WRAPPER_SOURCE" ]; then |
| 26 | + zz_log e "Missing wrapper source: {U $WRAPPER_SOURCE}" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +if [ ! -f "$CURLRC_SOURCE" ]; then |
| 31 | + zz_log e "Missing curl config source: {U $CURLRC_SOURCE}" |
| 32 | + exit 1 |
| 33 | +fi |
| 34 | + |
| 35 | +if [ -z "$CURRENT_HOME" ] || [ ! -d "$CURRENT_HOME" ]; then |
| 36 | + zz_log e "Could not resolve home directory for user: {U $CURRENT_USER}" |
| 37 | + exit 1 |
| 38 | +fi |
| 39 | + |
| 40 | +zz_log i "Installing Zscaler curl wrapper to {U $WRAPPER_TARGET}" |
| 41 | +run_as_root install -m 755 "$WRAPPER_SOURCE" "$WRAPPER_TARGET" |
| 42 | + |
| 43 | +if [ -L "$CURL_BIN" ] && [ "$(readlink -f "$CURL_BIN")" = "$WRAPPER_TARGET" ]; then |
| 44 | + zz_log i "curl is already routed through Zscaler wrapper" |
| 45 | +else |
| 46 | + if [ ! -e "$CURL_REAL_BIN" ]; then |
| 47 | + zz_log i "Preserving original curl as {U $CURL_REAL_BIN}" |
| 48 | + run_as_root mv "$CURL_BIN" "$CURL_REAL_BIN" |
| 49 | + fi |
| 50 | + |
| 51 | + zz_log i "Linking {U $CURL_BIN} to {U $WRAPPER_TARGET}" |
| 52 | + run_as_root ln -sfn "$WRAPPER_TARGET" "$CURL_BIN" |
| 53 | +fi |
| 54 | + |
| 55 | +zz_log i "Installing curl configuration to {U $CURRENT_HOME/.curlrc}" |
| 56 | +run_as_root install -m 644 "$CURLRC_SOURCE" "$CURRENT_HOME/.curlrc" |
| 57 | +run_as_root chown "$CURRENT_USER":"$(id -gn "$CURRENT_USER")" "$CURRENT_HOME/.curlrc" |
| 58 | + |
| 59 | +zz_log s "Zscaler curl wrapper configured for {U $CURRENT_USER}" |
0 commit comments