#!/usr/bin/env bash function ensure_zwavejs_config { ZWAVE_JS_CONF="${SNAP}/usr/lib/zwavejs2mqtt/node_modules/@zwave-js/config/config" if [ "$(find ${ZWAVE_JS_CONF} -maxdepth 0 -empty -exec echo empty \;)" == "empty" ]; then logger "Config directory is empty, copying data from ${SNAP}/snap/zwave-js/config" rsync -raz "${SNAP}/snap/zwave-js/config" "${ZWAVE_JS_CONF}/.." fi return 0 } function same_network_key { if [ -f "${SNAP_DATA}/settings.json" ]; then SETTINGS_KEY=$(jq '.zwave.networkKey' -r ${SNAP_DATA}/settings.json) if [ "${SETTINGS_KEY}" != "" ] && [ "${1}" != "${SETTINGS_KEY}" ]; then echo "${SETTINGS_KEY}" return 1 fi return 0 fi return 0 } function plug_connected { if ! snapctl is-connected "${1}"; then echo "Missing plug: «${1}»" >&2 echo "Connect with:" echo "$ sudo snap connect ${SNAP_NAME}:${1}" return 1 fi return 0 } function plugs_connected { MISSING=0 RAW_OUT=$(plug_connected "raw-usb") RAW_RES=$? SERIAL_OUT=$(plug_connected "serial-port") SERIAL_RES=$? if [ $RAW_RES -ne 0 -a $SERIAL_RES -ne 0 ]; then echo $RAW_OUT echo "----- OR -----" echo $SERIAL_OUT MISSING=1 elif [ $RAW_RES -ne 0 ]; then echo "OPTIONAL plug:" echo $RAW_OUT elif [ $SERIAL_RES -ne 0 ]; then echo "OPTIONAL plug" echo $SERIAL_OUT fi plug_connected "hardware-observe" if [ $? -ne 0 ]; then MISSING=1 fi if [ $MISSING -ne 0 ]; then return 1 fi return 0 } function is_root { if [ ${EUID:-$(id -u)} -eq 0 ]; then return 0 fi return 1 } function require_root { is_root if [ $? -eq 1 ]; then echo "Running as root is required." >&2 echo "Re-run with sudo." exit 1 fi } function testnset_config { logger "${SNAP_NAME}: Testing ${1}, or setting ${2}" RES=$(snapctl get ${1}) if [ $? -ne 0 ] || [ -z "${RES}" ]; then logger "${SNAP_NAME}: Setting ${1}=${2}" RES=$(snapctl set ${1}=${2}) if [ $? -ne 0 ]; then logger "${RES}" exit 1 fi return 0 else logger "${SNAP_NAME}: ${1} has ${RES}, leaving as is" fi return 0 } function test_default_config { testnset_config "server.ssl" false testnset_config "server.host" "localhost" testnset_config "server.port" 8091 testnset_config "session.cookie-secure" false testnset_config "session.secret" $(uuid) }