name: zwavejs2mqtt
base: core20
version: 'v7.2.0'
summary: Fully configurable Zwave to MQTT Gateway and Control Panel.
description: |
  ** Fully configurable Zwave to MQTT Gateway and Control Panel**

  - Zwave to Mqtt Gateway: Configure how nodes and nodes values are mapped between the two protocols
  - Secured: Supports HTTPS and users authentication
  - Control Panel UI: Directly control all your nodes and their
    values from the UI, some of control panel features:
      - Nodes management: check all nodes discovered in the z-wave network and manage their values
      - Firmware updates: update devices firmware, just select the controller action 
        _Begin Firmware Update_ and upload the firmware file
      - Groups associations: add/edit nodes associations
      - Full zwave-js APIs support
      - Custom scenes management: create scenes and trigger them by using MQTT apis (also supports timeouts)
      - Log debug in UI: See debug logs directly from the UI
      - Store directory management: all files are stored in store folder, you can download/edit files 
        inside this folder directly from the UI
      - Network graph: see how nodes are communicating with the controller, useful for diagnostics purposes
      - Home Assistant integration: you can use official Zwavejs integration by enabling zwavejs server or 
        use integrated MQTT discovery integration

  **Channels in `latest`**
  This is how we use the different of channels in the `latest` track, to give the user a chance to choose 
  how often they want updates.

  - `latest/stable`: Latest version of the last *major* release. Will only update once per major release 
    and most likely by the end of that release cycle. E.g when there is a new version `b.a.a` then version 
    `a.b.c` will be will be available in this track.
  - `latest/candidate`:  Latest minor or patch release of latest major release. E.g when version `a.b.c` 
    changes to `a.b.d` or `a.c.a`, then version `a.b.d` or `a.c.a` will be available. Consider this the 
    track for rolling releases.
  - `latest/edge` and `latest/edge/dev`: Every build (including tests) might be pushed to these channels.
    A.K.A **Do NOT** use it if you don't know what you're up to - **and** expect problems: Do not report bugs!
grade: stable
confinement: strict
license: MIT
assumes:
  - command-chain
architectures:
  - build-on: amd64
  - build-on: arm64
  - build-on: armhf

apps:
  zwavejs2mqtt:
    daemon: simple
    restart-condition: always
    environment:
      STORE_DIR: $SNAP_DATA
      ZWAVEJS_EXTERNAL_CONFIG: $SNAP_DATA/.ext-config
      DAEMONIZED: 1
    command: bin/yarn start
    command-chain:
      - helper/env-wrapper
    plugs:
      - network
      - network-bind
      - raw-usb
      - serial-port
      - hardware-observe
      - log-observe
  exec:
    environment:
      STORE_DIR: $SNAP_DATA
      ZWAVEJS_EXTERNAL_CONFIG: $SNAP_DATA/.ext-config
    command: bin/yarn start
    command-chain:
      - helper/env-wrapper
    plugs:
      - network
      - network-bind
      - raw-usb
      - serial-port
      - hardware-observe
  help:
    command: helper/env-wrapper --help
  enable:
    command: bin/daemonize
  disable:
    command: bin/de-daemonize
  restart:
    command: bin/restart
  copy-2-zui:
    command: bin/cpy-2-zui

layout:
  /usr/local/share/.cache/yarn:
    bind: $SNAP/.cache/yarn
  /usr/local/share/.yarn:
    symlink: $SNAP/lib/node_modules

plugs:
  zui-store:
    interface: content
    content: vscs-content
    target: $SNAP_COMMON/zui-store
    default-provider: zwave-js-ui

parts:
  zwavejs2mqtt:
    after: [nodejs, dependencies]
    plugin: dump
    source: https://github.com/zwave-js/zwave-js-ui.git
    #source-branch: "master"
    #source-tag: "v7.1.0"
    source-tag: $SNAPCRAFT_PROJECT_VERSION
    build-packages:
      - gcc
      - build-essential

    build-environment:
      - LD_LIBRARY_PATH: "${LD_LIBRARY_PATH}:$(dirname $(find ${SNAPCRAFT_STAGE} -name libonig.so.2 -print0))"
      - PATH: "${PATH}:${SNAPCRAFT_PART_BUILD}/bin"
    override-build: |
      # Used when testing an unreleased version of Z-wavejs
      # sed -i 's/"zwave-js".*$/"zwave-js"\: "10.0.5-0-pr-4836-ed10b36"/g' ${SNAPCRAFT_PART_BUILD}/package.json

      # Link the bundled yarn 
      cd "${SNAPCRAFT_PART_BUILD}/bin"
      YARN="$(find ${SNAPCRAFT_PART_BUILD} -name yarn-*.cjs -print0)"
      ln -s "../${YARN:${#SNAPCRAFT_PART_BUILD}+1}" ./yarn
      chmod +x ./yarn
      cd "${SNAPCRAFT_PART_BUILD}"

      # Necessary for the remote build; sometimes.
      if [ -n "${http_proxy-}" ]; then
        yarn config set httpProxy "${http_proxy}"
      fi
      if [ -n "${https_proxy-}" ]; then
        yarn config set httpsProxy "${https_proxy}"
      fi

      yarn install
      yarn build:server
      yarn build:ui
      yarn remove $(cat "${SNAPCRAFT_PART_BUILD}/package.json" | jq -r '.devDependencies | keys | join(" ")')
      
      # Copy built version into install directory
      mkdir -p "${SNAPCRAFT_PART_INSTALL}/lib/" \
        && cp --archive --link --no-dereference "${SNAPCRAFT_PART_BUILD}" \
          "${SNAPCRAFT_PART_INSTALL}/lib/zwavejs2mqtt"
      
      # Link yarn in install directory
      mkdir "${SNAPCRAFT_PART_INSTALL}/bin" \
        && cd "${SNAPCRAFT_PART_INSTALL}/bin" \
        && ln -s "../lib/zwavejs2mqtt/bin/yarn" "yarn"

    filesets:
      zwavejs2mqtt-include:
        - bin
        - lib/zwavejs2mqtt/.git
        - lib/zwavejs2mqtt/*.md
        - lib/zwavejs2mqtt/bin/yarn
        - lib/zwavejs2mqtt/dist
        - lib/zwavejs2mqtt/hass
        - lib/zwavejs2mqtt/node_modules
        - lib/zwavejs2mqtt/server
        - lib/zwavejs2mqtt/snippets
        - lib/zwavejs2mqtt/package.json
        - lib/zwavejs2mqtt/views
        - lib/zwavejs2mqtt/.yarn
        - lib/zwavejs2mqtt/yarn.lock
        - lib/zwavejs2mqtt/.yarnrc.yml
        - lib/zwavejs2mqtt/LICENSE
    stage:
      - $zwavejs2mqtt-include
  
  dependencies:
    plugin: nil
    stage-snaps:
      - jq
      - yq
    stage-packages:
      - git
      - udev
      - iproute2

  nodejs:
    plugin: dump
    source:
      - on amd64: https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-x64.tar.gz
      - on arm64: https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-arm64.tar.gz
      - on armhf: https://nodejs.org/dist/v16.13.0/node-v16.13.0-linux-armv7l.tar.gz
    stage-packages:
      - on armhf:
          - libatomic1
    organize:
      '*.md': nodejs/
      LICENSE: nodejs/
    prime:
      - -include
      - -share
      - -lib/node_modules
      - -bin/npm
      - -bin/npx
      - -bin/corepack
    override-stage: |
      corepack disable
      snapcraftctl stage

  local:
    plugin: dump
    source: src/
    stage-packages:
      - uuid
    organize:
      hooks: snap/hooks