app-id: {{app-id}}
runtime: org.freedesktop.Platform
runtime-version: '23.08'
sdk: org.freedesktop.Sdk
base: org.electronjs.Electron2.BaseApp
base-version: '23.08'
sdk-extensions:
  - org.freedesktop.Sdk.Extension.node20
  - org.freedesktop.Sdk.Extension.rust-stable
command: run.sh
separate-locales: false
finish-args:
  # Share IPC namespace with the host (improves X11 performance)
  - --share=ipc
  # Show window using Wayland or X11
  - --socket=wayland
  - --socket=x11
  # Access sound devices using pulseaudio
  - --socket=pulseaudio
  # Allow access to network
  - --share=network
  # OpenGL rendering
  - --device=dri
  # Webcam access for video calls (portal doesn't yet seem to be supported)
  # https://github.com/electron/electron/issues/42608
  - --device=all
  # Access directories
  - --filesystem=xdg-download
  # Allow desktop notifications
  - --talk-name=org.freedesktop.Notifications
  # Allow writing to system secrets storage
  - --talk-name=org.freedesktop.secrets
build-options:
  append-path: /usr/lib/sdk/node20/bin:/usr/lib/sdk/rust-stable/bin
  cflags: -O2 -g
  cxxflags: -O2 -g
  env:
    NPM_CONFIG_LOGLEVEL: info
modules:
  - name: threema-desktop
    buildsystem: simple
    build-options:
      env:
        XDG_CACHE_HOME: /run/build/threema-desktop/flatpak-node/cache
        CARGO_HOME: /run/build/threema-desktop/cargo
        npm_config_cache: /run/build/threema-desktop/flatpak-node/npm-cache
        npm_config_offline: 'true'
      secret-env: ['SENTRY_DSN', 'MINIDUMP_ENDPOINT']
    sources:
      # Project source code
      - type: dir
        path: ../../
      # Node dependencies, updated with flatpak-node-generator
      - generated-npm-sources.json
      # Rust dependencies, updated with flatpak-cargo-generator
      - generated-cargo-sources.json
      # Wrapper to launch the app
      - type: script
        dest-filename: run.sh
        commands:
          - # Note: The following options were temporarily removed to work around a bug in Chromium or Electron:
            #
            # --ozone-platform-hint=auto \
            # --enable-features="WaylandWindowDecorations" \
            #
            # See https://github.com/electron/electron/issues/37531
            /app/main/ThreemaDesktopLauncher --launcher-target-bin /app/bin/zypak-wrapper.sh /app/main/ThreemaDesktop "$@"
    build-commands:
      # Build and install launcher binary
      - cd src/rust/launcher/ && cargo --offline fetch --manifest-path Cargo.toml --verbose
      - cd src/rust/launcher/ && THREEMA_BUILD_FLAVOR='{{flavor}}' cargo --offline build --release --features allow_path_override

      # Install npm dependencies
      #
      # Note: In `package.json`, we pin the node/npm versions to a specific
      # value. Because the Flatpak runtime does not ensure a specific minor or
      # patch version of node, we skip the strict engine checks in npm by
      # passing in `--engine-strict=false`.
      - npm --engine-strict=false install --offline --no-audit --no-fund
      # Remove previous build artifacts
      - rm -rf build/electron/packaged
      # Build Threema Desktop
      - |
        ln -s $XDG_CACHE_HOME/node-gyp $HOME/.electron-gyp # https://github.com/flatpak/flatpak-builder-tools/pull/260#discussion_r822084602
        SKIP_LAUNCHER_BINARY=true npm run dist:{{flavor}}

      # Bundle app and dependencies
      - ls -lah build/electron/packaged/
      - cp -a build/electron/packaged/Threema*-linux-* /app/main
      # Install app wrappers
      - install -Dm755 -t /app/bin/ run.sh
      - install -Dm755 -t /app/main/ src/rust/launcher/target/release/ThreemaDesktopLauncher
      # Install metadata
      - install -Dm644 packaging/metadata/ch.threema.threema-desktop.metainfo.xml /app/share/metainfo/{{app-id}}.metainfo.xml
      - install -Dm644 packaging/metadata/ch.threema.threema-desktop.desktop /app/share/applications/{{app-id}}.desktop
      - sed -i 's/ch.threema.threema-desktop/{{app-id}}/g' /app/share/metainfo/{{app-id}}.metainfo.xml
      - sed -i 's/ch.threema.threema-desktop/{{app-id}}/g' /app/share/applications/{{app-id}}.desktop
      - sed -i "s#name>Threema<#name>{{app-name}}<#" /app/share/metainfo/{{app-id}}.metainfo.xml
      - sed -i "s#Name=Threema#Name={{app-name}}#" /app/share/applications/{{app-id}}.desktop
      - sed -i "s#StartupWMClass=Threema#StartupWMClass={{app-name}}#" /app/share/applications/{{app-id}}.desktop
      # Install Icons
      - mkdir -p /app/share/icons/hicolor/{512x512,scalable}/apps/
      - |
        case {{flavor}} in
          consumer-sandbox) icon_base=consumer-sandbox ;;
          consumer-live) icon_base=consumer-live ;;
          work-sandbox) icon_base=work-sandbox ;;
          work-live) icon_base=work-live ;;
          work-onprem) icon_base=work-live ;;
          *) echo "Invalid flavor: {{flavor}}"; exit 1 ;;
        esac
        install -Dm644 packaging/assets/icons/flatpak/${icon_base}.svg /app/share/icons/hicolor/scalable/apps/{{app-id}}.svg
        install -Dm644 packaging/assets/icons/flatpak/${icon_base}.png /app/share/icons/hicolor/512x512/apps/{{app-id}}.png
