# Description: Open source home theater software, wayland native version
# URL: https://kodi.tv/
# Maintainer: Tim Biermann, tbier at posteo dot de
# Depends on: crossguid dbus flatbuffers fstrcmp glu libass libcdio libdvdread libinput libxkbcommon openjdk17-jdk rapidjson rtmpdump spdlog swig taglib tinyxml waylandpp
# Optional: alsa-lib avahi bluez dav1d libmicrohttpd libnfs libva libva-vdpau-driver libxslt mariadb mysql pipewire pulseaudio

name=kodi-wayland
version=20.2
release=1

# version variables {{{
_codename=Nexus
_ffmpeg_version="4.4.1-${_codename}-Alpha1"
_libdvdcss_version="1.4.3-Next-${_codename}-Alpha2-2"
_libdvdnav_version="6.1.1-Next-${_codename}-Alpha2-2"
_libdvdread_version="6.1.3-Next-${_codename}-Alpha2-2"
# }}}
# source {{{
source=(https://github.com/xbmc/xbmc/archive/$version-$_codename/xbmc-$version-$_codename.tar.gz
  https://github.com/xbmc/FFmpeg/archive/$_ffmpeg_version/ffmpeg-$_ffmpeg_version.tar.gz
  https://github.com/xbmc/libdvdcss/archive/$_libdvdcss_version/${name/-wayland/}-libdvdcss-$_libdvdcss_version.tar.gz
  https://github.com/xbmc/libdvdnav/archive/$_libdvdnav_version/${name/-wayland/}-libdvdnav-$_libdvdnav_version.tar.gz
  https://github.com/xbmc/libdvdread/archive/$_libdvdread_version/${name/-wayland/}-libdvdread-$_libdvdread_version.tar.gz)
# }}}
# unpack_source() {{{
unpack_source() {
  for file in ${source[@]}; do
    case ${file##*/} in
      xbmc-$version-$_codename.tar.gz)
        bsdtar -p -o -C $SRC -xf $(get_filename $file) ;;
      *)
        cp $(get_filename $file) $SRC ;;
      esac
    done
}
# }}}

build()  {
  # clang? {{{
  if [[ $PKGMK_KODI_USE_CLANG == on ]]; then
    [[ -e /usr/bin/clang && -e /usr/bin/clang++ && -e /usr/bin/lld ]] && export CC=clang CXX=clang++ AR=llvm-ar NM=llvm-nm RANLIB=llvm-ranlib LDFLAGS+=' -fuse-ld=lld' && KODI_EXTRA+=' -D CMAKE_CXX_COMPILER_AR=/usr/bin/llvm-ar -D CMAKE_CXX_COMPILER_RANLIB=/usr/bin/llvm-ranlib -D CMAKE_NM=/usr/bin/llvm-nm -D ENABLE_LLD=ON -D USE_LTO=8' || echo "Please install clang first"
  fi
  # }}}
  unset PATH
  export PATH="/usr/lib/java/openjdk17-jdk/bin:/usr/bin:/bin:/usr/sbin:/sbin"

  # optionals {{{
  prt-get isinst alsa-lib && KODI_EXTRA+=' -D ENABLE_ALSA=ON' || KODI_EXTRA+=' -D ENABLE_ALSA=OFF'
  prt-get isinst avahi && KODI_EXTRA+=' -D ENABLE_AVAHI=ON' || KODI_EXTRA+=' -D ENABLE_AVAHI=OFF'
  prt-get isinst bluez sbc && KODI_EXTRA+=' -D ENABLE_BLUETOOTH=ON' || KODI_EXTRA+=' -D ENABLE_BLUETOOTH=OFF'
  prt-get isinst ccache && PATH="$(echo ${PATH} | awk -v RS=: -v ORS=: '/ccache/ {next} {print}' | sed 's/:*$//')"
  prt-get isinst dav1d && KODI_EXTRA+=' -D ENABLE_INTERNAL_DAV1D=OFF' || KODI_EXTRA+=' -D ENABLE_INTERNAL_DAV1D=ON'
  prt-get isinst libmicrohttpd && KODI_EXTRA+=' -D ENABLE_MICROHTTPD=ON' || KODI_EXTRA+=' -D ENABLE_MICROHTTPD=OFF'
  prt-get isinst libnfs && KODI_EXTRA+=' -D ENABLE_NFS=ON' || KODI_EXTRA+=' -D ENABLE_NFS=OFF'
  prt-get isinst libva && KODI_EXTRA+=' -D ENABLE_VAAPI=ON' || KODI_EXTRA+=' -D ENABLE_VAAPI=OFF'
  prt-get isinst libva-vdpau-driver && KODI_EXTRA+=' -D ENABLE_VDPAU=ON' || KODI_EXTRA+=' -D ENABLE_VDPAU=OFF'
  prt-get isinst libxslt && KODI_EXTRA+=' -D ENABLE_XSLT=ON' || KODI_EXTRA+=' -D ENABLE_XSLT=OFF'
  prt-get isinst mysql && KODI_EXTRA+=' -D ENABLE_MYSQLCLIENT=ON'
  prt-get isinst mariadb && KODI_EXTRA+=' -D ENABLE_MARIADBCLIENT=ON'
  prt-get isinst pulseaudio && KODI_EXTRA+=' -D ENABLE_PULSEAUDIO=ON' || KODI_EXTRA+=' -D ENABLE_PULSEAUDIO=OFF'
  prt-get isinst pipewire && KODI_EXTRA+=' -D ENABLE_PIPEWIRE=ON' || KODI_EXTRA+=' -D ENABLE_PIPEWIRE=OFF'
  # }}}

  # grabbed off from gentoo
  for i in xbmc-$version-$_codename/tools/depends/native/{TexturePacker,JsonSchemaBuilder}/src; do
    pushd $i;
    autoreconf -vfi;
    popd
  done
  sed -e 's/autoreconf -vif/echo "autoreconf already done"/' -i \
    xbmc-$version-$_codename/tools/depends/native/TexturePacker/src/autogen.sh \
    xbmc-$version-$_codename/tools/depends/native/JsonSchemaBuilder/src/autogen.sh

  cmake -S xbmc-$version-$_codename -B build -G Ninja $KODI_EXTRA \
    -D CMAKE_INSTALL_PREFIX=/usr \
    -D CMAKE_INSTALL_LIBDIR=lib \
    -D CMAKE_BUILD_TYPE=Release \
    -D CMAKE_CXX_FLAGS_RELEASE="$CXXFLAGS -Wno-error=restrict" \
    -D CMAKE_C_FLAGS_RELEASE="$CFLAGS" \
    -D CORE_PLATFORM_NAME=wayland \
    -D APP_RENDER_SYSTEM=gl \
    -D ENABLE_EGL=ON \
    -D ENABLE_DBUS=ON \
    -D ENABLE_DVDCSS=ON \
    -D ENABLE_EVENTCLIENTS=ON \
    -D ENABLE_UDEV=ON \
    -D ENABLE_PYTHON=ON \
    -D fmt_DIR=/usr/lib/cmake/fmt \
    -D spdlog_DIR=/usr/lib/cmake/spdlog \
    -D ENABLE_ISO9660PP=OFF \
    -D ENABLE_GOLD=OFF \
    -D ENABLE_OPTICAL=OFF \
    -D ENABLE_TESTING=OFF \
    -D VERBOSE=OFF \
    -D ENABLE_INTERNAL_FFMPEG=ON \
    -D FFMPEG_URL="$SRC/ffmpeg-$_ffmpeg_version.tar.gz" \
    -D ENABLE_INTERNAL_CROSSGUID=OFF \
    -D LIBDVDCSS_URL="$SRC/${name/-wayland/}-libdvdcss-$_libdvdcss_version.tar.gz" \
    -D LIBDVDNAV_URL="$SRC/${name/-wayland/}-libdvdnav-$_libdvdnav_version.tar.gz" \
    -D LIBDVDREAD_URL="$SRC/${name/-wayland/}-libdvdread-$_libdvdread_version.tar.gz" \
    -D ENABLE_INTERNAL_FMT=OFF \
    -D ENABLE_INTERNAL_FSTRCMP=OFF \
    -D ENABLE_INTERNAL_FLATBUFFERS=OFF \
    -D ENABLE_INTERNAL_GTEST=OFF \
    -D ENABLE_INTERNAL_RapidJSON=OFF \
    -Wno-dev
  cmake --build build
  DESTDIR=$PKG cmake --build build --target install

  export PYTHONHASHSEED=0
  /usr/bin/python3 -m compileall -d / $PKG/usr/lib
  /usr/bin/python3 -O -m compileall -d / $PKG/usr/lib

  rm -r $PKG/usr/{lib/firewalld,share/doc}
}

# vim: foldmethod=marker foldlevelstart=1
