# Copyright (C) 2019-2023 Savoir-faire Linux Inc.
#
# Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>
# Author: Amin Bandali <amin.bandali@savoirfairelinux.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# This file is based on earlier work by Dani Llewellyn, under the
# Expat License (also referred to as "the MIT License"), per the
# following notice:

# Copyright (C) 2017-2020 Dani Llewellyn <dani@bowlhat.net>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


name: jami
version: "RELEASE_VERSION"
adopt-info: jami
icon: common/icons/jami.svg
license: GPL-3.0+
summary: 'Privacy-oriented voice, video, chat, and conference platform and SIP phone'
description: |
  Jami is free software for universal communication that respects the
  freedom and privacy of its users.

  Jami is an end-to-end encrypted secure and distributed voice, video,
  and chat communication platform.  Jami requires no central server
  for these communications and leaves the power of privacy and freedom
  in the hands of users.

  Jami provides the following key features to its users:

  * Synchronized and enriched one-to-one conversations
  * Group conversations (Beta)
  * Audio/video calls and conferences
  * Screen sharing in video calls and conferences
  * Unlimited peer-to-peer file sharing
  * Recording and sending audio/video messages
  * Jami plugins SDK for additional functionality (green screen,
    watermark, audio filters, and more)
  * Use Jami as your SIP phone with a variety of media codecs
    and VoIP providers

  Client applications for GNU/Linux, Windows, macOS, iOS, Android,
  and Android TV are available, making Jami an interoperable and
  cross-platform communication framework.

  Seamlessly deploy Jami in your organization using
  Jami Account Management Server (JAMS).   With JAMS, you can enable
  your users to connect using their LDAP/ActiveDirectory credentials
  or create local accounts, enabling you to manage your own
  Jami community while taking advantage of Jami's distributed network
  architecture.  For more information, please visit https://jami.biz.

confinement: strict
grade: stable
base: core20

plugs:
  gnome-3-38-2004:
    interface: content
    target: $SNAP/gnome-platform
    default-provider: gnome-3-38-2004:gnome-3-38-2004
  gtk-3-themes:
    interface: content
    target: $SNAP/data-dir/themes
    default-provider: gtk-common-themes:gtk-3-themes
  icon-themes:
    interface: content
    target: $SNAP/data-dir/icons
    default-provider: gtk-common-themes:icon-themes
  sound-themes:
    interface: content
    target: $SNAP/data-dir/sounds
    default-provider: gtk-common-themes:sounds-themes

slots:
  dbus-jami:
    interface: dbus
    bus: session
    name: net.jami.Jami
  dbus-ring:
    interface: dbus
    bus: session
    name: cx.ring.Ring

layout:
  /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib:
    bind: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib
  /usr/share/alsa:
    bind: $SNAP/usr/share/alsa
  /usr/share/X11/xkb:
    bind: $SNAP/usr/share/X11/xkb

environment:
  XDG_CURRENT_DESKTOP: Unity
  QT_BASE_DIR: "/usr/lib/libqt-jami"
  LD_LIBRARY_PATH: "$LD_LIBRARY_PATH:$SNAP$QT_BASE_DIR/lib"

apps:
  jami:
    command: usr/bin/jami
    command-chain:
    - bin/desktop-launch
    - snap/command-chain/alsa-launch
    - bin/jami-wrapper
    autostart: jami.desktop
    common-id: net.jami.Jami
    desktop: usr/share/applications/jami.desktop
    slots:
    - dbus-jami
    - dbus-ring
    plugs:
    - alsa
    - audio-playback
    - audio-record
    - browser-support
    - camera
    - desktop
    - desktop-legacy
    - gsettings
    - hardware-observe
    - home
    - network
    - network-bind
    - network-manager
    - opengl
    - removable-media
    - screen-inhibit-control
    - system-observe
    - unity7
    - wayland
    - x11

package-repositories:
  - type: apt
    components: [main]
    suites: [jami]
    key-id: A295D773307D25A33AE72F2F64CD5FA175348F84
    url: https://dl.jami.net/nightly/ubuntu_20.04/

parts:
  desktop-launch:
    source: https://github.com/ubuntu/snapcraft-desktop-helpers.git
    plugin: nil
    build-packages:
    - dpkg-dev
    - gcc
    - g++
    - cmake
    - cmake-data
    - libqt-jami
    - devscripts
    - equivs
    override-pull: |
      snapcraftctl pull
      sed -i -E 's|\. \$SNAP/flavor-select|USE_gtk3=true|' gtk/launcher-specific
      sed -i -E 's|\. \$SNAP/flavor-select|USE_qt5=true|' qt/launcher-specific
      sed -i -E 's|(append_dir GTK_PATH \$RUNTIME/usr/lib/\$ARCH/gtk-2.0)|#\1|' qt/launcher-specific
      sed -i -E 's|\$RUNTIME|$SNAP|g' qt/launcher-specific
      sed -i -E 's|\$WITH_RUNTIME|no|g' qt/launcher-specific
    override-build: |
      snapcraftctl build
      gcc -Wall -O2 -o bindtextdomain.so -fPIC -shared src/bindtextdomain.c -ldl

      echo "#!/bin/bash" > desktop-launch
      cat common/init >> desktop-launch

      cat qt/runtime-exports >> desktop-launch
      cat gtk/runtime-exports >> desktop-launch

      cat common/desktop-exports >> desktop-launch

      cat qt/launcher-specific >> desktop-launch
      cat gtk/launcher-specific >> desktop-launch

      cat common/mark-and-exec >> desktop-launch

      install -D -m755 desktop-launch $SNAPCRAFT_PART_INSTALL/bin/desktop-launch
      install -D -m644 bindtextdomain.so $SNAPCRAFT_PART_INSTALL/lib/bindtextdomain.so

      mkdir -pv $SNAPCRAFT_PART_INSTALL/gnome-platform

  scripts:
    plugin: dump
    source: common/scripts
    organize:
      jami-wrapper: bin/jami-wrapper

  alsa-mixin:
    plugin: dump
    source: https://github.com/diddlesnaps/snapcraft-alsa.git
    source-subdir: snapcraft-assets
    stage:
      - -usr/lib/x86_64-linux-gnu/libp11-kit.so*
      - -usr/lib/x86_64-linux-gnu/libproxy.so*
      - -usr/share/doc/libp11-kit0/changelog.Debian.gz
      - -usr/share/doc/libproxy1v5/changelog.Debian.gz
    build-packages:
      - libasound2-dev
    stage-packages:
      - libasound2
      - libasound2-plugins
      - yad

  jami:
    after: [alsa-mixin]
    source: .
    plugin: nil
    parse-info: [usr/share/metainfo/jami.appdata.xml]
    stage:
      - -usr/lib/x86_64-linux-gnu/liblber-2.4.so*
      - -usr/lib/x86_64-linux-gnu/libldap_r-2.4.so*
      - -usr/share/doc/libldap-2.4-2/changelog.Debian.gz
      - -usr/share/doc/libldap-common/changelog.Debian.gz
    build-environment:
      - QT_BASE_DIR: "/usr/lib/libqt-jami"
      - PATH: "$QT_BASE_DIR/bin:$PATH"
      - LD_LIBRARY_PATH: "$QT_BASE_DIR/lib:$LD_LIBRARY_PATH"
      - PKG_CONFIG_PATH: "$QT_BASE_DIR/lib/pkgconfig:$PKG_CONFIG_PATH"
    override-pull: |
      ls
      snapcraftctl pull
      sed -i -E 's|(tmpName) << (PACKAGE_NAME << "_shm_")|\1 << "snap.jami." << \2|' ./daemon/src/media/video/sinkclient.cpp
      sed -i -E 's|^Icon=.*|Icon=${SNAP}/usr/share/icons/hicolor/scalable/apps/jami.svg|' extras/data/jami.desktop
    override-build: |
      cd $SNAPCRAFT_PART_BUILD/daemon/contrib
      mkdir -p native
      cd native
      ../bootstrap
      make .ffmpeg
      make

      cd $SNAPCRAFT_PART_BUILD/daemon
      ./autogen.sh
      ./configure --prefix=/usr --without-dbus
      make -j$SNAPCRAFT_PARALLEL_BUILD_COUNT
      DESTDIR=$SNAPCRAFT_PART_INSTALL make install

      cd $SNAPCRAFT_PART_BUILD/
      mkdir build
      cd build
      cmake .. -DENABLE_LIBWRAP=true \
               -DLIBJAMI_BUILD_DIR=$SNAPCRAFT_PART_BUILD/daemon/src \
               -DCMAKE_INSTALL_PREFIX=/usr \
               -DCMAKE_BUILD_TYPE=Release
      make -j$SNAPCRAFT_PARALLEL_BUILD_COUNT
      DESTDIR=$SNAPCRAFT_PART_INSTALL make install
    build-packages:
    - autoconf
    - autoconf-archive
    - automake
    - autopoint
    - bzip2
    - curl
    - gcc
    - g++
    - gettext
    - gzip
    - libnotify-dev
    - libqrencode-dev
    - libsndfile1-dev
    - libspeex-dev
    - python3
    - python3-dev
    - uuid-dev
    - yasm
    - wget
    - nasm # ffmpeg
    - libavcodec-dev
    - libavdevice-dev
    - libavformat-dev
    - libswscale-dev
    - libva-dev
    - libvdpau-dev
    - libargon2-0-dev # opendht
    - libexpat1-dev
    - libjsoncpp-dev
    - libnm-dev # connectivityChanged()
    - libsystemd-dev # sdbus-cpp
    - libpulse-dev # pulse
    - libudev-dev
    - libopus-dev # Avoid to build opus from contrib
    - libglu1-mesa-dev # Qt 6
    - zlib1g-dev
    - libgl-dev
    - libvulkan-dev
    - libgnutls28-dev # TLS
    - gnutls-bin
    - libssl-dev
    stage-packages:
    - libgnutls30
    - libavutil56
    - libdouble-conversion3
    - libegl1
    - libgbm1
    - libgudev-1.0-0
    - libjsoncpp1
    - libllvm12
    - libminizip1
    - libnm0
    - libnotify4
    - libnspr4
    - libnss3
    - libopengl0
    - libopus0
    - liborc-0.4-0
    - libpcre2-16-0
    - libqrencode4
    - libqt-jami
    - libsnappy1v5
    - libspeex1
    - libproxy1v5
    - libwayland-server0
    - libwebpdemux2
    - libwebpmux3
    - libxkbfile1
    - libxtst6
    - ocl-icd-libopencl1
    - libva-drm2
    - libva-glx2
    - libva-wayland2
    - libva2
    - libvdpau-va-gl1
    - libvdpau1
    - va-driver-all
    prime:
    - -usr/lib/libqt-jami/bin/
    - -usr/lib/libqt-jami/docs/
    - -usr/lib/libqt-jami/examples/
    - -usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libLLVM-*.so*
    - -usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libclang-*.so*
    - -usr/lib/$SNAPCRAFT_ARCH_TRIPLET/libflite*.so*
