env-wrapper 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env bash
  2. set -e
  3. OZW_LIB=$(dirname $(find "${SNAP}/usr/local" -name libopenzwave.so -print -quit))
  4. export LD_LIBRARY_PATH="${OZW_LIB}:${LD_LIBRARY_PATH}"
  5. function lprint {
  6. echo ${1}
  7. logger "${SNAP_NAME}: ${1}"
  8. }
  9. if ! snapctl is-connected raw-usb; then
  10. lprint "Please connect raw-usb interface!"
  11. lprint "Run: snap connect ${SNAP_NAME}:raw-usb"
  12. exit 1
  13. fi
  14. USB_PATH=$(snapctl get usb-path)
  15. STOP_ON_FAILURE=$(snapctl get stop-on-failure)
  16. MQTT_SERVER=$(snapctl get mqtt.server)
  17. MQTT_PORT=$(snapctl get mqtt.port)
  18. MQTT_USERNAME=$(snapctl get mqtt.username)
  19. export MQTT_PASSWORD=$(snapctl get mqtt.password)
  20. MQTT_TLS=$(snapctl get mqtt.tls)
  21. OZW_INSTANCE=$(snapctl get ozw.instance)
  22. export OZW_NETWORK_KEY=$(snapctl get ozw.network-key)
  23. OZW_CONFIG_DIR=$(snapctl get ozw.config-dir)
  24. OZW_USER_DIR=$(snapctl get ozw.user-dir)
  25. export OZW_AUTH_KEY=$(snapctl get ozw.auth-key)
  26. if [ ! -c "${USB_PATH}" ]; then
  27. lprint "usb-path (${USB_PATH}) does not exist,"
  28. lprint "or is not a Character Device"
  29. lprint "See: snap get ${SNAP_NAME} -d usb-path"
  30. exit 1
  31. fi
  32. OZW_ARGS+=(--serial-port "${USB_PATH}")
  33. if [ ${STOP_ON_FAILURE} == true ]; then
  34. OZW_ARGS+=(--stop-on-failure)
  35. elif [ ${STOP_ON_FAILURE} != false ]; then
  36. lprint "Invalid value for stop-on-failure (${STOP_ON_FAILURE})"
  37. lprint "See: snap get ${SNAP_NAME} -d stop-on-failure"
  38. exit 1
  39. fi
  40. OZW_ARGS+=(--mqtt-server "${MQTT_SERVER}")
  41. OZW_ARGS+=(--mqtt-port "${MQTT_PORT}")
  42. if [ ${MQTT_TLS} != false ]; then
  43. OZW_ARGS+=(--mqtt-username "${MQTT_USERNAME}")
  44. fi
  45. if [ ${MQTT_TLS} == true ]; then
  46. OZW_ARGS+=(--mqtt-tls)
  47. elif [ ${MQTT_TLS} != false ]; then
  48. lprint "Invalid value for mqtt.tls (${MQTT_TLS})"
  49. lprint "See: snap get ${SNAP_NAME} -d mqtt"
  50. exit 1
  51. fi
  52. OZW_ARGS+=(--mqtt-instance "${OZW_INSTANCE}")
  53. if [ ! -d "${OZW_CONFIG_DIR}" ] || [ ! -w "${OZW_CONFIG_DIR}" ]; then
  54. lprint "Directory: ${OZW_CONFIG_DIR}"
  55. lprint "- Does not exist or not writable."
  56. exit 125
  57. fi
  58. OZW_ARGS+=(--config-dir "${OZW_CONFIG_DIR}")
  59. if [ ! -d "${OZW_USER_DIR}" ] || [ ! -w "${OZW_USER_DIR}" ]; then
  60. lprint "Directory: ${OZW_USER_DIR}"
  61. lprint "- Does not exist or not writable."
  62. exit 126
  63. fi
  64. OZW_ARGS+=(--user-dir "${OZW_USER_DIR}")
  65. # if ! timeout "${MQTT_CONNECT_TIMEOUT:=30}" \
  66. # bash -c "until echo /dev/tcp/${MQTT_SERVER}/${MQTT_PORT}; do sleep 1; done" &>/dev/null
  67. # then
  68. # lprint "Could not connect to the MQTT broker after ${MQTT_CONNECT_TIMEOUT} seconds"
  69. # exit 124
  70. # fi
  71. set -- "$@" "${OZW_ARGS[@]}"
  72. lprint "Env. variables"
  73. lprint $(printenv | grep "OZW_")
  74. lprint $(printenv | grep "MQTT_")
  75. # ARGC=$#
  76. # ARGV=("$@")
  77. #
  78. lprint "Exec: $@"
  79. # echo "Arguments: "
  80. # for ((i = 0; i < ${ARGC}; i++)); do
  81. # echo "${ARGV[$i]}"
  82. # done
  83. exec "$@"