name: telegram-desktop
adopt-info: telegram
icon: Telegram/Resources/art/icon512@2x.png

base: core24
grade: stable
confinement: strict
compression: lzo

apps:
  telegram-desktop:
    command: usr/bin/telegram-desktop
    common-id: org.telegram.desktop
    desktop: usr/share/applications/org.telegram.desktop.desktop
    autostart: telegram-desktop_telegram-desktop.desktop
    extensions: [gnome]
    plugs:
      - audio-playback
      - audio-record
      - camera
      - hardware-observe
      - home
      - network
      - network-bind
      - network-status
      - removable-media
    slots:
      - mpris

layout:
  /var/tmp:
    type: tmpfs

parts:
  telegram:
    plugin: cmake
    source: .
    source-type: git
    parse-info: [usr/share/metainfo/org.telegram.desktop.metainfo.xml]
    build-environment:
      - LDFLAGS: ${LDFLAGS:+$LDFLAGS} -Wl,--push-state,--no-as-needed,-ldav1d,-lhwy,--pop-state -s
    build-packages:
      - clang
      - libboost-regex-dev
      - libheif-dev
      - libjxl-dev
      - libssl-dev
      - libxcb-keysyms1-dev
      - libxcb-record0-dev
      - libxcb-screensaver0-dev
    cmake-generator: Ninja
    cmake-parameters:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/usr
      - -DTDESKTOP_API_ID=611335
      - -DTDESKTOP_API_HASH=d524b414d21f4d37f08684c1df41ac9c
    override-pull: |
      craftctl default

      version_file=Telegram/build/version
      version=$(sed -n "s/AppVersionStr[ ]\+\(.*\)\+/\1/p" $version_file)
      beta=$(sed -n "s/BetaChannel[ ]\+\(.*\)\+/\1/p" $version_file)

      if [ "$beta" != "0" ]; then
        version="$version-beta"
      fi

      version="${version}$(git describe --tags | sed 's,^v[^-]\+,,')"

      craftctl set version="$version"
    override-build: |
      craftctl default
      mv "$CRAFT_PART_INSTALL"/usr/bin/{Telegram,telegram-desktop}

      APP_ID=org.telegram.desktop
      sed -i "s/^Icon=$APP_ID$/Icon=snap.$CRAFT_PROJECT_NAME./g" "$CRAFT_PART_INSTALL/usr/share/applications/$APP_ID.desktop"
      for i in $(find "$CRAFT_PART_INSTALL/usr/share/icons" -name "$APP_ID*.svg" -o -name "$APP_ID*.png"); do
        PREFIXED="$(dirname "$i")/snap.$CRAFT_PROJECT_NAME.$(basename "$i" | sed "s/$APP_ID//")"
        mv "$i" "$PREFIXED"
        DEST="$(echo "$PREFIXED" | sed 's@usr/share@meta/gui@')"
        DESTDIR="$(dirname "$DEST")"
        mkdir -p "$DESTDIR"
        RELATIVE="$(realpath -s --relative-to="$DESTDIR" "$PREFIXED")"
        ln -sfn "$RELATIVE" "$DEST"
      done
    after:
      - ada
      - avif
      - ffmpeg
      - jpegli
      - openal
      - protobuf
      - qt
      - rnnoise
      - tde2e
      - webrtc

  pkgs:
    plugin: nil
    stage-packages:
      - gstreamer1.0-fdkaac
      - gstreamer1.0-plugins-bad
      - libgeoclue-2-0
      - libopenh264-7
      - libxcb-cursor0
      - libxcb-record0
      - libxcb-screensaver0
    stage:
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gstreamer-1.0/libgstde265.so
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gstreamer-1.0/libgstfdkaac.so
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gstreamer-1.0/libgstopenh264.so
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gstreamer-1.0/libgstsubenc.so
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gstreamer-1.0/libgstva.so
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/gstreamer-1.0/libgstvideoparsersbad.so
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libfdk-aac.so*
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libgeoclue-2.so*
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libopenh264.so*
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libxcb-cursor.so*
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libxcb-record.so*
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libxcb-screensaver.so*

  patches:
    source: https://github.com/desktop-app/patches.git
    source-depth: 1
    source-commit: 859556cab51d17585ff76d3db62ff1c7502bc850
    plugin: nil
    override-pull: |
      craftctl default
      cp -r . "$CRAFT_STAGE/patches"
    override-prime: |
      rm -rf patches

  ada:
    source: https://github.com/ada-url/ada.git
    source-depth: 1
    source-tag: v3.2.4
    plugin: cmake
    cmake-generator: Ninja
    cmake-parameters:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/usr
      - -DADA_TESTING=OFF
      - -DADA_TOOLS=OFF
      - -DADA_INCLUDE_URL_PATTERN=OFF
    prime: [-./*]

  avif:
    source: https://github.com/AOMediaCodec/libavif.git
    source-depth: 1
    source-tag: v1.3.0
    plugin: cmake
    build-packages:
      - libdav1d-dev
    cmake-generator: Ninja
    cmake-parameters:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/usr
      - -DBUILD_SHARED_LIBS=OFF
      - -DAVIF_CODEC_DAV1D=SYSTEM
      - -DAVIF_LIBYUV=OFF
    prime: [-./*]

  ffmpeg:
    source: https://github.com/FFmpeg/FFmpeg.git
    source-depth: 1
    source-branch: n6.1.1
    plugin: autotools
    build-packages:
      - nasm
      - libdav1d-dev
      - libopenh264-dev
      - libopus-dev
      - libvdpau-dev
      - libvpx-dev
      - libxv-dev
    autotools-configure-parameters:
      - --prefix=/usr
      - --libdir=/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR
      - --extra-cflags="-DCONFIG_SAFE_BITSTREAM_READER=1"
      - --extra-cxxflags="-DCONFIG_SAFE_BITSTREAM_READER=1"
      - --disable-debug
      - --disable-programs
      - --disable-doc
      - --disable-network
      - --disable-autodetect
      - --disable-everything
      - --enable-libdav1d
      - --enable-libopenh264
      - --enable-libopus
      - --enable-libvpx
      - --enable-vaapi
      - --enable-vdpau
      - --enable-xlib
      - --enable-libdrm
      - --enable-ffnvcodec
      - --enable-nvdec
      - --enable-cuvid
      - --enable-protocol=file
      - --enable-hwaccel=av1_vaapi
      - --enable-hwaccel=av1_nvdec
      - --enable-hwaccel=h264_vaapi
      - --enable-hwaccel=h264_vdpau
      - --enable-hwaccel=h264_nvdec
      - --enable-hwaccel=hevc_vaapi
      - --enable-hwaccel=hevc_vdpau
      - --enable-hwaccel=hevc_nvdec
      - --enable-hwaccel=mpeg2_vaapi
      - --enable-hwaccel=mpeg2_vdpau
      - --enable-hwaccel=mpeg2_nvdec
      - --enable-hwaccel=mpeg4_vaapi
      - --enable-hwaccel=mpeg4_vdpau
      - --enable-hwaccel=mpeg4_nvdec
      - --enable-hwaccel=vp8_vaapi
      - --enable-hwaccel=vp8_nvdec
      - --enable-decoder=aac
      - --enable-decoder=aac_fixed
      - --enable-decoder=aac_latm
      - --enable-decoder=aasc
      - --enable-decoder=ac3
      - --enable-decoder=alac
      - --enable-decoder=av1
      - --enable-decoder=av1_cuvid
      - --enable-decoder=eac3
      - --enable-decoder=flac
      - --enable-decoder=gif
      - --enable-decoder=h264
      - --enable-decoder=hevc
      - --enable-decoder=libdav1d
      - --enable-decoder=libvpx_vp8
      - --enable-decoder=libvpx_vp9
      - --enable-decoder=mp1
      - --enable-decoder=mp1float
      - --enable-decoder=mp2
      - --enable-decoder=mp2float
      - --enable-decoder=mp3
      - --enable-decoder=mp3adu
      - --enable-decoder=mp3adufloat
      - --enable-decoder=mp3float
      - --enable-decoder=mp3on4
      - --enable-decoder=mp3on4float
      - --enable-decoder=mpeg4
      - --enable-decoder=msmpeg4v2
      - --enable-decoder=msmpeg4v3
      - --enable-decoder=opus
      - --enable-decoder=pcm_alaw
      - --enable-decoder=pcm_f32be
      - --enable-decoder=pcm_f32le
      - --enable-decoder=pcm_f64be
      - --enable-decoder=pcm_f64le
      - --enable-decoder=pcm_lxf
      - --enable-decoder=pcm_mulaw
      - --enable-decoder=pcm_s16be
      - --enable-decoder=pcm_s16be_planar
      - --enable-decoder=pcm_s16le
      - --enable-decoder=pcm_s16le_planar
      - --enable-decoder=pcm_s24be
      - --enable-decoder=pcm_s24daud
      - --enable-decoder=pcm_s24le
      - --enable-decoder=pcm_s24le_planar
      - --enable-decoder=pcm_s32be
      - --enable-decoder=pcm_s32le
      - --enable-decoder=pcm_s32le_planar
      - --enable-decoder=pcm_s64be
      - --enable-decoder=pcm_s64le
      - --enable-decoder=pcm_s8
      - --enable-decoder=pcm_s8_planar
      - --enable-decoder=pcm_u16be
      - --enable-decoder=pcm_u16le
      - --enable-decoder=pcm_u24be
      - --enable-decoder=pcm_u24le
      - --enable-decoder=pcm_u32be
      - --enable-decoder=pcm_u32le
      - --enable-decoder=pcm_u8
      - --enable-decoder=pcm_zork
      - --enable-decoder=vorbis
      - --enable-decoder=vp8
      - --enable-decoder=wavpack
      - --enable-decoder=wmalossless
      - --enable-decoder=wmapro
      - --enable-decoder=wmav1
      - --enable-decoder=wmav2
      - --enable-decoder=wmavoice
      - --enable-encoder=aac
      - --enable-encoder=libopenh264
      - --enable-encoder=libopus
      - --enable-encoder=pcm_s16le
      - --enable-filter=atempo
      - --enable-parser=aac
      - --enable-parser=aac_latm
      - --enable-parser=flac
      - --enable-parser=gif
      - --enable-parser=h264
      - --enable-parser=hevc
      - --enable-parser=mpeg4video
      - --enable-parser=mpegaudio
      - --enable-parser=opus
      - --enable-parser=vorbis
      - --enable-demuxer=aac
      - --enable-demuxer=flac
      - --enable-demuxer=gif
      - --enable-demuxer=h264
      - --enable-demuxer=hevc
      - --enable-demuxer=matroska
      - --enable-demuxer=m4v
      - --enable-demuxer=mov
      - --enable-demuxer=mp3
      - --enable-demuxer=ogg
      - --enable-demuxer=wav
      - --enable-muxer=mp4
      - --enable-muxer=ogg
      - --enable-muxer=opus
      - --enable-muxer=wav
    override-pull: |
      craftctl default
      sed -i 's/disable ffnvcodec cuvid nvdec nvenc/:/g' configure
    prime: [-./*]
    after:
      - nv-codec-headers

  jpegli:
    source: https://github.com/libjxl/libjxl.git
    source-depth: 1
    source-tag: v0.11.1
    plugin: cmake
    build-packages:
      - libhwy-dev
    cmake-generator: Ninja
    cmake-parameters:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/usr
      - -DBUILD_SHARED_LIBS=OFF
      - -DBUILD_TESTING=OFF
      - -DJPEGXL_ENABLE_DEVTOOLS=OFF
      - -DJPEGXL_ENABLE_TOOLS=OFF
      - -DJPEGXL_INSTALL_JPEGLI_LIBJPEG=ON
      - -DJPEGXL_ENABLE_DOXYGEN=OFF
      - -DJPEGXL_ENABLE_MANPAGES=OFF
      - -DJPEGXL_ENABLE_BENCHMARK=OFF
      - -DJPEGXL_ENABLE_EXAMPLES=OFF
      - -DJPEGXL_ENABLE_JNI=OFF
      - -DJPEGXL_ENABLE_SJPEG=OFF
      - -DJPEGXL_ENABLE_OPENEXR=OFF
      - -DJPEGXL_ENABLE_SKCMS=OFF
      - -DJPEGXL_FORCE_SYSTEM_BROTLI=ON
      - -DJPEGXL_FORCE_SYSTEM_LCMS2=ON
      - -DJPEGXL_FORCE_SYSTEM_HWY=ON
    override-pull: |
      craftctl default
      curl -sSL https://github.com/libjxl/libjxl/commit/ee3955b1553bcc10304d45b85dfef9afa9349d72.patch | sed 's/offset + t/offset + i/' | git apply
    override-build: |
      craftctl default
      cp lib/libjpegli-static.a "$CRAFT_PART_INSTALL"/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libjpeg.a
      ar rcs "$CRAFT_PART_INSTALL"/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libjpeg.a lib/CMakeFiles/jpegli-libjpeg-obj.dir/jpegli/libjpeg_wrapper.cc.o
    stage:
      - ./usr/include/jconfig.h
      - ./usr/include/jmorecfg.h
      - ./usr/include/jpeglib.h
      - ./usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libjpeg.a
    prime: [-./*]

  openal:
    source: https://github.com/kcat/openal-soft.git
    source-depth: 1
    source-tag: 1.24.3
    plugin: cmake
    build-packages:
      - libpipewire-0.3-dev
    cmake-generator: Ninja
    cmake-parameters:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/usr
      - -DLIBTYPE=STATIC
      - -DALSOFT_DLOPEN=OFF
      - -DALSOFT_EXAMPLES=OFF
      - -DALSOFT_UTILS=OFF
      - -DALSOFT_INSTALL_CONFIG=OFF
      - -DALSOFT_BACKEND_PIPEWIRE=OFF
    prime: [-./*]

  nv-codec-headers:
    source: https://github.com/FFmpeg/nv-codec-headers.git
    source-depth: 1
    source-branch: n12.1.14.0
    plugin: make
    make-parameters:
      - PREFIX=/usr
      - LIBDIR=lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR
    prime: [-./*]

  protobuf:
    source: https://github.com/protocolbuffers/protobuf.git
    source-depth: 1
    source-tag: v30.2
    plugin: cmake
    cmake-generator: Ninja
    cmake-parameters:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/usr
      - -Dprotobuf_BUILD_TESTS=OFF
    prime: [-./*]

  qt:
    source: https://github.com/qt/qt5.git
    source-depth: 1
    source-tag: v6.10.1
    source-submodules:
      - qtbase
      - qtdeclarative
      - qtimageformats
      - qtshadertools
      - qtsvg
      - qtwayland
    plugin: cmake
    build-environment:
      - LDFLAGS: ${LDFLAGS:+$LDFLAGS} -Wl,--push-state,--no-as-needed,-lhwy,--pop-state
    build-packages:
      - libssl-dev
      - libxcb-cursor-dev
      - libxcb-glx0-dev
      - libxcb-icccm4-dev
      - libxcb-image0-dev
      - libxcb-keysyms1-dev
      - libxcb-randr0-dev
      - libxcb-render-util0-dev
      - libxcb-shape0-dev
      - libxcb-sync-dev
      - libxcb-util-dev
      - libxcb-xfixes0-dev
      - libxcb-xkb-dev
      - libxkbcommon-x11-dev
    cmake-generator: Ninja
    cmake-parameters:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/usr
      - -DINSTALL_LIBDIR=/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR
      - -DBUILD_SHARED_LIBS=OFF
      - -DQT_QPA_PLATFORMS="wayland;xcb"
      - -DFEATURE_eglfs=OFF
      - -DINPUT_openssl=linked
    override-pull: |
      craftctl default
      QT="$(grep 'set(QT_REPO_MODULE_VERSION' qtbase/.cmake.conf | sed -r 's/.*"(.*)".*/\1/')"
      cd qtbase
      find $CRAFT_STAGE/patches/qtbase_${QT} -type f -print0 | sort -z | xargs -r0 git apply
      cd ../qtwayland
      find $CRAFT_STAGE/patches/qtwayland_${QT} -type f -print0 | sort -z | xargs -r0 git apply
      cd ..
    prime: [-./*]
    after:
      - jpegli
      - patches

  rnnoise:
    source: https://github.com/xiph/rnnoise.git
    source-depth: 1
    source-tag: v0.2
    plugin: autotools
    build-packages:
      - wget
    autotools-configure-parameters:
      - --prefix=/usr
      - --libdir=\${exec_prefix}/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR
      - --disable-shared
      - --disable-examples
      - --disable-doc
    override-pull: |
      craftctl default
      sed -i 's/os_support.h/common.h/g;s/OPUS_CLEAR/RNN_CLEAR/g' src/vec{,_neon}.h
    prime: [-./*]

  tde2e:
    source: https://github.com/tdlib/td.git
    source-depth: 1
    source-commit: 51743dfd01dff6179e2d8f7095729caa4e2222e9
    plugin: cmake
    build-packages:
      - gperf
      - libssl-dev
    cmake-generator: Ninja
    cmake-parameters:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/usr
      - -DCMAKE_INSTALL_LIBDIR=lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR
      - -DTD_E2E_ONLY=ON
    prime: [-./*]

  webrtc:
    source: https://github.com/desktop-app/tg_owt.git
    source-depth: 1
    source-commit: 5c5c71258777d0196dbb3a09cc37d2f56ead28ab
    plugin: cmake
    build-packages:
      - libopenh264-dev
      - libopus-dev
      - libpipewire-0.3-dev
      - libssl-dev
      - libvpx-dev
    cmake-generator: Ninja
    cmake-parameters:
      - -DCMAKE_BUILD_TYPE=Release
      - -DCMAKE_INSTALL_PREFIX=/usr
    prime: [-./*]
    after:
      - ffmpeg
      - jpegli
