|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +APP_ROOT="/opt/wire-pod" |
| 5 | +DATA_ROOT="${WIREPOD_DATA_DIR:-/data}" |
| 6 | +DEFAULT_SOURCE="${APP_ROOT}/docker/default-source.sh" |
| 7 | + |
| 8 | +mkdir -p "${DATA_ROOT}" |
| 9 | + |
| 10 | +link_dir() { |
| 11 | + local rel_path="$1" |
| 12 | + local src_path="${APP_ROOT}/${rel_path}" |
| 13 | + local dest_path="${DATA_ROOT}/${rel_path}" |
| 14 | + |
| 15 | + mkdir -p "$(dirname "${dest_path}")" |
| 16 | + |
| 17 | + if [ ! -d "${dest_path}" ]; then |
| 18 | + if [ -d "${src_path}" ]; then |
| 19 | + cp -a "${src_path}" "${dest_path}" |
| 20 | + else |
| 21 | + mkdir -p "${dest_path}" |
| 22 | + fi |
| 23 | + fi |
| 24 | + |
| 25 | + if [ -e "${src_path}" ] && [ ! -L "${src_path}" ]; then |
| 26 | + rm -rf "${src_path}" |
| 27 | + fi |
| 28 | + |
| 29 | + ln -sfn "${dest_path}" "${src_path}" |
| 30 | +} |
| 31 | + |
| 32 | +link_file() { |
| 33 | + local rel_path="$1" |
| 34 | + local src_path="${APP_ROOT}/${rel_path}" |
| 35 | + local dest_path="${DATA_ROOT}/${rel_path}" |
| 36 | + |
| 37 | + mkdir -p "$(dirname "${dest_path}")" |
| 38 | + |
| 39 | + if [ ! -e "${dest_path}" ]; then |
| 40 | + if [ -f "${src_path}" ]; then |
| 41 | + cp -a "${src_path}" "${dest_path}" |
| 42 | + else |
| 43 | + : >"${dest_path}" |
| 44 | + fi |
| 45 | + fi |
| 46 | + |
| 47 | + if [ -e "${src_path}" ] && [ ! -L "${src_path}" ]; then |
| 48 | + rm -f "${src_path}" |
| 49 | + fi |
| 50 | + |
| 51 | + ln -sfn "${dest_path}" "${src_path}" |
| 52 | +} |
| 53 | + |
| 54 | +link_file_with_default() { |
| 55 | + local rel_path="$1" |
| 56 | + local default_path="$2" |
| 57 | + local src_path="${APP_ROOT}/${rel_path}" |
| 58 | + local dest_path="${DATA_ROOT}/${rel_path}" |
| 59 | + |
| 60 | + mkdir -p "$(dirname "${dest_path}")" |
| 61 | + |
| 62 | + if [ ! -e "${dest_path}" ]; then |
| 63 | + if [ -n "${default_path}" ] && [ -f "${default_path}" ]; then |
| 64 | + cp -a "${default_path}" "${dest_path}" |
| 65 | + elif [ -f "${src_path}" ]; then |
| 66 | + cp -a "${src_path}" "${dest_path}" |
| 67 | + else |
| 68 | + : >"${dest_path}" |
| 69 | + fi |
| 70 | + fi |
| 71 | + |
| 72 | + if [ -e "${src_path}" ] && [ ! -L "${src_path}" ]; then |
| 73 | + rm -f "${src_path}" |
| 74 | + fi |
| 75 | + |
| 76 | + ln -sfn "${dest_path}" "${src_path}" |
| 77 | +} |
| 78 | + |
| 79 | +persist_directories() { |
| 80 | + link_dir certs |
| 81 | + link_dir stt |
| 82 | + link_dir vosk |
| 83 | + link_dir whisper.cpp |
| 84 | + link_dir vector-cloud/build |
| 85 | + link_dir chipper/jdocs |
| 86 | + link_dir chipper/plugins |
| 87 | + link_dir chipper/session-certs |
| 88 | +} |
| 89 | + |
| 90 | +persist_files() { |
| 91 | + link_file chipper/apiConfig.json |
| 92 | + link_file chipper/botConfig.json |
| 93 | + link_file chipper/customIntents.json |
| 94 | + link_file chipper/pico.key |
| 95 | + link_file chipper/useepod |
| 96 | + link_file_with_default chipper/source.sh "${DEFAULT_SOURCE}" |
| 97 | +} |
| 98 | + |
| 99 | +update_export() { |
| 100 | + local key="$1" |
| 101 | + local value="$2" |
| 102 | + local file_path="$3" |
| 103 | + |
| 104 | + local escaped |
| 105 | + escaped=$(printf '%s' "${value}" | sed 's/[\\&/]/\\&/g') |
| 106 | + |
| 107 | + if grep -q "^export ${key}=" "${file_path}"; then |
| 108 | + sed -i "s/^export ${key}=.*/export ${key}=\"${escaped}\"/" "${file_path}" |
| 109 | + else |
| 110 | + printf 'export %s="%s"\n' "${key}" "${value}" >>"${file_path}" |
| 111 | + fi |
| 112 | +} |
| 113 | + |
| 114 | +apply_env_overrides() { |
| 115 | + local source_file="${APP_ROOT}/chipper/source.sh" |
| 116 | + |
| 117 | + if [ -n "${WIREPOD_DEBUG_LOGGING:-}" ]; then |
| 118 | + update_export "DEBUG_LOGGING" "${WIREPOD_DEBUG_LOGGING}" "${source_file}" |
| 119 | + fi |
| 120 | + |
| 121 | + if [ -n "${WIREPOD_STT_SERVICE:-}" ]; then |
| 122 | + update_export "STT_SERVICE" "${WIREPOD_STT_SERVICE}" "${source_file}" |
| 123 | + fi |
| 124 | + |
| 125 | + if [ -n "${WIREPOD_STT_LANGUAGE:-}" ]; then |
| 126 | + update_export "STT_LANGUAGE" "${WIREPOD_STT_LANGUAGE}" "${source_file}" |
| 127 | + fi |
| 128 | + |
| 129 | + if [ -n "${WIREPOD_USE_INBUILT_BLE:-}" ]; then |
| 130 | + update_export "USE_INBUILT_BLE" "${WIREPOD_USE_INBUILT_BLE}" "${source_file}" |
| 131 | + fi |
| 132 | + |
| 133 | + if [ -n "${WIREPOD_PICOVOICE_APIKEY:-}" ]; then |
| 134 | + update_export "PICOVOICE_APIKEY" "${WIREPOD_PICOVOICE_APIKEY}" "${source_file}" |
| 135 | + printf '%s\n' "${WIREPOD_PICOVOICE_APIKEY}" >"${DATA_ROOT}/chipper/pico.key" |
| 136 | + fi |
| 137 | +} |
| 138 | + |
| 139 | +persist_directories |
| 140 | +persist_files |
| 141 | + |
| 142 | +if [ ! -e /root/.vosk ]; then |
| 143 | + ln -sfn /opt/vosk /root/.vosk |
| 144 | +fi |
| 145 | + |
| 146 | +apply_env_overrides |
| 147 | + |
| 148 | +exec "$@" |
0 commit comments