name: freac
base: core20
adopt-info: freac
summary: Audio converter and CD ripper
description: |
  fre:ac is a free and open source audio converter. It comes with
  an integrated tag editor and converts between various formats.

grade: stable
confinement: strict

icon: icons/freac.png

apps:
  freac:
    command: bin/freac
    desktop: share/applications/org.freac.freac.desktop
    extensions: [ gnome-3-38 ]
    plugs:
      - gsettings
      - home
      - network
      - optical-drive
      - audio-playback
      - removable-media

  freaccmd:
    command: bin/freaccmd
    extensions: [ gnome-3-38 ]
    plugs:
      - home
      - network
      - optical-drive
      - removable-media

parts:
  patches:
    source: packaging/snapcraft/patches
    plugin: dump
    prime:
      - -*

  libcdio:
    source: https://ftp.gnu.org/gnu/libcdio/libcdio-2.1.0.tar.bz2
    source-checksum: sha256/8550e9589dbd594bfac93b81ecf129b1dc9d0d51e90f9696f1b2f9b2af32712b
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr, --disable-cxx, --disable-example-progs, --without-cd-drive, --without-cd-info, --without-cd-read, --without-cdda-player, --without-iso-info, --without-iso-read ]
    prime:
       - usr/lib/libcdio.so*

  libcdio-paranoia:
    after: [ libcdio ]
    source: https://ftp.gnu.org/gnu/libcdio/libcdio-paranoia-10.2+2.0.1.tar.bz2
    source-checksum: sha256/33b1cf305ccfbfd03b43936975615000ce538b119989c4bec469577570b60e8a
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr, --disable-cxx, --disable-example-progs ]
    prime:
      - usr/lib/libcdio_*.so*

  libfaad:
    after: [ patches ]
    source: https://github.com/knik0/faad2/archive/2_10_0.tar.gz
    source-checksum: sha256/0c6d9636c96f95c7d736f097d418829ced8ec6dbd899cc6cc82b728480a84bfb
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr ]
    build-environment:
      - PATH: "/usr/bin:$PATH"
      - CFLAGS: "$CFLAGS -O3 -funroll-loops"
    override-build: |
      patch -p1 < $SNAPCRAFT_STAGE/faad2-2.10.0-configure.patch
      chmod a+x configure
      snapcraftctl build
    prime:
      - usr/lib/libfaad.so*

  libfdk-aac:
    after: [ patches ]
    source: https://github.com/mstorsjo/fdk-aac/archive/v2.0.2.tar.gz
    source-checksum: sha256/7812b4f0cf66acda0d0fe4302545339517e702af7674dd04e5fe22a5ade16a90
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr ]
    build-environment:
      - CXXFLAGS: "$CXXFLAGS -O3 -funroll-loops"
    override-build: |
      patch -p1 < $SNAPCRAFT_STAGE/fdk-aac-2.0.2-drcfix.patch
      autoreconf -i
      snapcraftctl build
    prime:
      - usr/lib/libfdk-aac.so*

  libflac:
    after: [ libogg ]
    source: https://ftp.osuosl.org/pub/xiph/releases/flac/flac-1.4.2.tar.xz
    source-checksum: sha256/e322d58a1f48d23d9dd38f432672865f6f79e73a6f9cc5a5f57fcaa83eb5a8e4
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr, --disable-cpp-libs, --disable-vsx ]
    build-environment:
      - CFLAGS: "$CFLAGS -O3 -funroll-loops"
    prime:
      - usr/lib/libFLAC.so*

  libmac:
    source: https://freac.org/patches/MAC_920_SDK.zip
    source-checksum: sha256/54a99ff50b589d4afdb0e38aa86dba2c6a9fe1a086330209ecd563f09e46fd66
    source-subdir: .
    plugin: cmake
    cmake-parameters: [ -DCMAKE_INSTALL_PREFIX=/usr ]
    build-environment:
      - CXXFLAGS: "$CXXFLAGS -O3 -funroll-loops"
    override-build: |
      if [ `lscpu | grep -i 'byte order' | awk '{ print tolower($3) }'` = "little" ]; then
        snapcraftctl build
      fi
    prime:
      - usr/lib/libMAC.so*

  libmp3lame:
    after: [ patches ]
    source: https://sourceforge.net/projects/lame/files/lame/3.100/lame-3.100.tar.gz
    source-checksum: sha256/ddfe36cab873794038ae2c1210557ad34857a4b6bdc515785d1da9e175b1da1e
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr, --enable-nasm, --disable-frontend ]
    build-environment:
      - CFLAGS: "$CFLAGS -O3 -funroll-loops"
    override-build: |
      if [ "$SNAP_ARCH" = "amd64" ]; then
        patch -p0 < $SNAPCRAFT_STAGE/lame-3.100-sse-20171014.patch
      fi
      snapcraftctl build
    prime:
      - usr/lib/libmp3lame.so*

  libmp4v2:
    after: [ patches ]
    source: https://github.com/enzo1982/mp4v2/releases/download/v2.1.2/mp4v2-2.1.2.tar.bz2
    source-checksum: sha256/0b943133673cffd4625247783e34080797de7386142061a6613e0c26285953ef
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr ]
    build-environment:
      - CXXFLAGS: "$CXXFLAGS -Os"
    override-build: |
      patch -p1 < $SNAPCRAFT_STAGE/mp4v2-2.1.2-sfocache.patch
      snapcraftctl build
    prime:
      - usr/lib/libmp4v2.so*

  libmpg123:
    source: https://mpg123.de/download/mpg123-1.31.2.tar.bz2
    source-checksum: sha256/b17f22905e31f43b6b401dfdf6a71ed11bb7d056f68db449d70b9f9ae839c7de
    plugin: autotools
    build-environment:
      - CFLAGS: "$CFLAGS -O3 -funroll-loops"
    override-build: |
      if [ "$SNAP_ARCH" = "i386" ]; then
        ./configure --prefix=/usr --with-cpu=sse
      elif [ "$SNAP_ARCH" = "amd64" ]; then
        ./configure --prefix=/usr --with-cpu=x86-64
      elif [ "$SNAP_ARCH" = "armhf" ]; then
        ./configure --prefix=/usr --with-cpu=neon
      elif [ "$SNAP_ARCH" = "arm64" ]; then
        ./configure --prefix=/usr --with-cpu=neon64
      else
        ./configure --prefix=/usr
      fi
      make && sudo make install
    prime:
      - usr/lib/libmpg123.so*

  libogg:
    after: [ patches ]
    source: https://ftp.osuosl.org/pub/xiph/releases/ogg/libogg-1.3.5.tar.xz
    source-checksum: sha256/c4d91be36fc8e54deae7575241e03f4211eb102afb3fc0775fbbc1b740016705
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr ]
    build-environment:
      - CFLAGS: "$CFLAGS -O3 -funroll-loops"
    prime:
      - usr/lib/libogg.so*

  libopus:
    source: https://ftp.osuosl.org/pub/xiph/releases/opus/opus-1.3.1.tar.gz
    source-checksum: sha256/65b58e1e25b2a114157014736a3d9dfeaad8d41be1c8179866f144a2fb44ff9d
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr, --enable-float-approx ]
    build-environment:
      - CFLAGS: "$CFLAGS -O3 -funroll-loops"
    prime:
      - usr/lib/libopus.so*

  librnnoise:
    source: https://github.com/xiph/rnnoise.git
    source-commit: 9acc1e5a633e0961a5895a73204df24744f199b6
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr ]
    build-environment:
      - CFLAGS: "$CFLAGS -O3 -funroll-loops"
    override-build: |
      autoreconf -i
      snapcraftctl build
    prime:
      - usr/lib/librnnoise.so*

  libspeex:
    source: https://ftp.osuosl.org/pub/xiph/releases/speex/speex-1.2.1.tar.gz
    source-checksum: sha256/4b44d4f2b38a370a2d98a78329fefc56a0cf93d1c1be70029217baae6628feea
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr ]
    build-environment:
      - CFLAGS: "$CFLAGS -O3 -funroll-loops"
    prime:
      - usr/lib/libspeex.so*

  libvorbis:
    after: [ patches, libogg ]
    source: https://ftp.osuosl.org/pub/xiph/releases/vorbis/libvorbis-1.3.7.tar.xz
    source-checksum: sha256/b33cc4934322bcbf6efcbacf49e3ca01aadbea4114ec9589d1b1e9d20f72954b
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr ]
    build-environment:
      - CFLAGS: "$CFLAGS -O3"
    override-build: |
      patch -p1 < $SNAPCRAFT_STAGE/libvorbis-1.3.7-aotuv-b6.03.patch
      if [ "$SNAP_ARCH" = "amd64" ]; then
        patch -p1 < $SNAPCRAFT_STAGE/libvorbis-1.3.7-aotuv-b6.03-lancer.patch
      fi
      snapcraftctl build
    prime:
      - usr/lib/libvorbis*.so*

  ffmpeg:
    source: https://ffmpeg.org/releases/ffmpeg-6.0.tar.xz
    source-checksum: sha256/57be87c22d9b49c112b6d24bc67d42508660e6b718b3db89c44e47e289137082
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr, --disable-autodetect, --enable-gpl, --enable-small, --enable-runtime-cpudetect, --disable-vsx ]
    build-packages:
      - yasm
    prime:
      - usr/bin/ffmpeg

  icons:
    plugin: nil
    stage-packages:
      - gnome-icon-theme
    override-prime: |
      mkdir -p share/icons/gnome/32x32/status
      cp $SNAPCRAFT_STAGE/usr/share/icons/gnome/32x32/status/dialog-error.png share/icons/gnome/32x32/status
      cp $SNAPCRAFT_STAGE/usr/share/icons/gnome/32x32/status/dialog-information.png share/icons/gnome/32x32/status
      cp $SNAPCRAFT_STAGE/usr/share/icons/gnome/32x32/status/dialog-question.png share/icons/gnome/32x32/status
      cp $SNAPCRAFT_STAGE/usr/share/icons/gnome/32x32/status/dialog-warning.png share/icons/gnome/32x32/status

  smooth:
    after: [ patches ]
    source: https://github.com/enzo1982/smooth.git
    plugin: make
    make-parameters: [ prefix= ]
    build-environment:
      - CXXFLAGS: "$CXXFLAGS -g0 -Os -fno-rtti -fno-exceptions"
    build-packages:
      - libbz2-dev
      - libcurl4-gnutls-dev
      - libfribidi-dev
      - libjpeg8-dev
      - libxml2-dev
    stage-packages:
      - libcurl3-gnutls
      - libfribidi0

  boca:
    after: [ libcdio-paranoia, smooth ]
    source: https://github.com/enzo1982/BoCA.git
    plugin: make
    make-parameters: [ prefix= ]
    build-environment:
      - CXXFLAGS: "$CXXFLAGS -g0 -Os -fno-rtti -fno-exceptions"
    override-build: |
      patch -p1 < $SNAPCRAFT_STAGE/boca-1.1.6-pre-snap.patch
      snapcraftctl build
    build-packages:
      - libexpat1-dev
      - libpulse-dev
      - liburiparser-dev
    stage-packages:
      - librubberband2
      - liburiparser1
      - musepack-tools
      - wavpack

  freac:
    after: [ boca ]
    source: https://github.com/enzo1982/freac.git
    plugin: make
    make-parameters: [ prefix= ]
    build-environment:
      - CXXFLAGS: "$CXXFLAGS -g0 -Os -fno-rtti -fno-exceptions -DFREAC_DATE=\\\\\\\"`date +\"%Y%m%d\"`\\\\\\\""
    override-pull: |
      snapcraftctl pull
      snapcraftctl set-version "1.1.7"
      sed -i -e 's|Icon=org.freac.freac|Icon=${SNAP}/share/icons/hicolor/128x128/apps/org.freac.freac.png|g' metadata/org.freac.freac.desktop
    override-build: |
      patch -p1 < $SNAPCRAFT_STAGE/freac-1.1.6-pre-snap.patch
      snapcraftctl build
    build-packages:
      - libudev-dev
