name: freac
base: core24
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

platforms:
  amd64:
  arm64:

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

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

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

  libcdio:
    source: https://github.com/libcdio/libcdio/releases/download/2.3.0/libcdio-2.3.0.tar.bz2
    source-checksum: sha256/53e83d284667535a767fd2d31edad1a6701591960459df373a10f1f21e80a7ed
    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://github.com/libcdio/libcdio-paranoia/releases/download/release-10.2+2.0.2/libcdio-paranoia-10.2+2.0.2.tar.bz2
    source-checksum: sha256/186892539dedd661276014d71318c8c8f97ecb1250a86625256abd4defbf0d0c
    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.11.2.tar.gz
    source-checksum: sha256/3fcbd305e4abd34768c62050e18ca0986f7d9c5eca343fb98275418013065c0e
    plugin: cmake
    cmake-parameters: [ -DCMAKE_INSTALL_PREFIX=/usr ]
    build-environment:
      - CFLAGS: "${CFLAGS-} -O3 -funroll-loops"
    prime:
      - usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libfaad.so*

  libfdk-aac:
    source: https://github.com/mstorsjo/fdk-aac/archive/v2.0.3.tar.gz
    source-checksum: sha256/e25671cd96b10bad896aa42ab91a695a9e573395262baed4e4a2ff178d6a3a78
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr ]
    build-environment:
      - CXXFLAGS: "${CXXFLAGS-} -O3 -funroll-loops"
    override-build: |
      autoreconf -i
      craftctl default
    prime:
      - usr/lib/libfdk-aac.so*

  libflac:
    after: [ libogg ]
    source: https://ftp.osuosl.org/pub/xiph/releases/flac/flac-1.5.0.tar.xz
    source-checksum: sha256/f2c1c76592a82ffff8413ba3c4a1299b6c7ab06c734dee03fd88630485c2b920
    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_1260_SDK.zip
    source-checksum: sha256/0bdbc0abbb2663b7911eb04a566351335ed76dabeae8156af40d264ebbb2e0d6
    source-subdir: .
    plugin: cmake
    cmake-parameters: [ -DCMAKE_INSTALL_PREFIX=/usr ]
    build-environment:
      - CXXFLAGS: "${CXXFLAGS-} -O3 -funroll-loops"
    prime:
      - usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/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 [ "$CRAFT_ARCH_BUILD_FOR" = "amd64" ]; then
        patch -p0 < $CRAFT_STAGE/lame-3.100-sse-20171014.patch
      fi
      craftctl default
    prime:
      - usr/lib/libmp3lame.so*

  libmp4v2:
    source: https://github.com/enzo1982/mp4v2/releases/download/v2.1.3/mp4v2-2.1.3.tar.bz2
    source-checksum: sha256/033185c17bf3c5fdd94020c95f8325be2e5356558e3913c3d6547a85dd61f7f1
    plugin: autotools
    autotools-configure-parameters: [ --prefix=/usr ]
    build-environment:
      - CXXFLAGS: "${CXXFLAGS-} -Os"
    prime:
      - usr/lib/libmp4v2.so*

  libmpg123:
    source: https://mpg123.de/download/mpg123-1.33.4.tar.bz2
    source-checksum: sha256/3ae8c9ff80a97bfc0e22e89fbcd74687eca4fc1db315b12607f27f01cb5a47d9
    plugin: make
    build-environment:
      - CFLAGS: "${CFLAGS-} -O3 -funroll-loops"
    override-build: |
      if [ "$CRAFT_ARCH_BUILD_FOR" = "i386" ]; then
        ./configure --prefix=/usr --with-cpu=sse
      elif [ "$CRAFT_ARCH_BUILD_FOR" = "amd64" ]; then
        ./configure --prefix=/usr --with-cpu=x86-64
      elif [ "$CRAFT_ARCH_BUILD_FOR" = "armhf" ]; then
        ./configure --prefix=/usr --with-cpu=neon
      elif [ "$CRAFT_ARCH_BUILD_FOR" = "arm64" ]; then
        ./configure --prefix=/usr --with-cpu=neon64
      else
        ./configure --prefix=/usr
      fi
      craftctl default
    prime:
      - usr/lib/libmpg123.so*

  libogg:
    after: [ patches ]
    source: https://ftp.osuosl.org/pub/xiph/releases/ogg/libogg-1.3.6.tar.xz
    source-checksum: sha256/5c8253428e181840cd20d41f3ca16557a9cc04bad4a3d04cce84808677fa1061
    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.6.1.tar.gz
    source-checksum: sha256/6ffcb593207be92584df15b32466ed64bbec99109f007c82205f0194572411a1
    plugin: make
    build-environment:
      - CFLAGS: "${CFLAGS-} -O3 -funroll-loops"
    override-build: |
      if [ "$CRAFT_ARCH_BUILD_FOR" = "armhf" ]; then
        ./configure --prefix=/usr --enable-float-approx --enable-osce --enable-qext --disable-intrinsics --disable-rtcd
      else
        ./configure --prefix=/usr --enable-float-approx --enable-osce --enable-qext
      fi
      craftctl default
    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
      craftctl default
    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 < $CRAFT_STAGE/libvorbis-1.3.7-aotuv-b6.03.patch
      if [ "$CRAFT_ARCH_BUILD_FOR" = "amd64" ]; then
        patch -p1 < $CRAFT_STAGE/libvorbis-1.3.7-aotuv-b6.03-lancer.patch
      fi
      craftctl default
    prime:
      - usr/lib/libvorbis*.so*

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

  icons:
    plugin: nil
    stage-packages:
      - gnome-icon-theme
    override-prime: |
      mkdir -p share/icons/gnome/32x32/status
      cp $CRAFT_STAGE/usr/share/icons/gnome/32x32/status/dialog-error.png share/icons/gnome/32x32/status
      cp $CRAFT_STAGE/usr/share/icons/gnome/32x32/status/dialog-information.png share/icons/gnome/32x32/status
      cp $CRAFT_STAGE/usr/share/icons/gnome/32x32/status/dialog-question.png share/icons/gnome/32x32/status
      cp $CRAFT_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:
      - libcurl3t64-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 < $CRAFT_STAGE/boca-1.1.8-pre-snap.patch
      craftctl default
    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: |
      craftctl default
      craftctl set version=$(echo 1.1.7~`date +"%Y%m%d"`)
      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 < $CRAFT_STAGE/freac-1.1.6-pre-snap.patch
      craftctl default
    build-packages:
      - libudev-dev
