# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
pkgname=ungoogled-chromium
pkgver=89.0.4389.90
pkgrel=1.1
pkgdesc="Google Chromium, sans integration with Google - PPC64LE FORK"
arch=(x86_64 powerpc64le)
url="https://github.com/leo-lb/ungoogled-chromium/tree/master"
license=('GPL')
conflicts=('chromium')
provides=('chromium')
replaces=('ungoogled-chromium-archlinux')
depends=('gtk3' 'nss' 'alsa-lib' 'xdg-utils' 'libxss' 'libcups' 'libgcrypt'
         'ttf-liberation' 'systemd' 'dbus' 'libpulse' 'pciutils' 'json-glib'
         'desktop-file-utils' 'hicolor-icon-theme' 'pipewire')
makedepends=('python' 'python2' 'gperf' 'mesa' 'ninja' 'nodejs' 'git' 'libva'
             'libpipewire02' 'clang' 'llvm' 'lld' 'gn' 'java-runtime-headless'
             'python2-setuptools' 'meson')
optdepends=('libpipewire02: WebRTC desktop sharing under Wayland'
            'libva: hardware-accelerated video decode [experimental]'
            'kdialog: needed for file dialogs in KDE'
            'org.freedesktop.secrets: password storage backend on GNOME / Xfce'
            'kwallet: for storing passwords in KWallet on KDE desktops')
_gcc_patchset=7
commit='f8ed009f1afe5ee57b8bd49a2c57b270a24d715c'
source=("git+https://github.com/leo-lb/ungoogled-chromium.git#commit=${commit}"
  https://github.com/stha09/chromium-patches/releases/download/chromium-${pkgver%%.*}-patchset-$_gcc_patchset/chromium-${pkgver%%.*}-patchset-$_gcc_patchset.tar.xz
  chromium-glibc-2.33.patch
  xxx-ppc64le-libvpx.patch
  xxx-ppc64le-support.patch)
sha256sums=('SKIP'
            'f8b1558f6c87b33423da854d42f0f69d47885a96d6bf6ce7f26373e93d47442f'
            '2fccecdcd4509d4c36af873988ca9dbcba7fdb95122894a9fdf502c33a1d7a4b'
            '9af5aad8554c966542550f7467bb86d3a02d101aded4407f0a14ad289e47ee62'
	    'eea60ec32cc6a4c249ad2d483b64000d94a060b94d90015943a647c7adc4a4fd')

# Possible replacements are listed in build/linux/unbundle/replace_gn_files.py
# Keys are the names in the above script; values are the dependencies in Arch
declare -gA _system_libs=(
  [ffmpeg]=ffmpeg
  [flac]=flac
  [fontconfig]=fontconfig
  [freetype]=freetype2
  [harfbuzz-ng]=harfbuzz
  [icu]=icu
  [libdrm]=
  [libjpeg]=libjpeg
  [libpng]=libpng
  #[libvpx]=libvpx
  [libwebp]=libwebp
  [libxml]=libxml2
  [libxslt]=libxslt
  [opus]=opus
  [re2]=re2
  [snappy]=snappy
  [zlib]=minizip
)
_unwanted_bundled_libs=(
  $(printf "%s\n" ${!_system_libs[@]} | sed 's/^libjpeg$/&_turbo/')
)
depends+=(${_system_libs[@]})

pkgver() {
  cat "$srcdir/${pkgname}"/chromium_version.txt
}

prepare() {
  _cache_dir=${srcdir}/../download-cache
  [ ! -z "${SRCDEST}" ] && _cache_dir=${SRCDEST}/ungoogled-chrome
  msg2 "Using ${_cache_dir} for download cache"

  msg2 "Updating GIT submdules..."
  cd "$srcdir/${pkgname}"
  #sed 's@leo-lb@shawnanastasio@g' -i .gitmodules
  git submodule update --init

  mkdir -p ${_cache_dir} build/src 
  msg2 'Downloading and extracting chrome...'
  ./utils/downloads.py retrieve -c ${_cache_dir} -i downloads.ini
  ./utils/downloads.py unpack -c ${_cache_dir} -i downloads.ini -- build/src

  msg2 'Pruning binaries...'
  ./utils/prune_binaries.py build/src pruning.list

  msg2 'Applying patches...'
  ./utils/patches.py apply build/src patches

  msg2 'Applying domain substitutions...'
  ./utils/domain_substitution.py apply -r domain_regex.list -f domain_substitution.list -c build/domsubcache.tar.gz build/src

  cd build/src

  # Allow building against system libraries in official builds
  sed -i 's/OFFICIAL_BUILD/GOOGLE_CHROME_BUILD/' \
    tools/generate_shim_headers/generate_shim_headers.py

  # https://crbug.com/893950
  sed -i -e 's/\<xmlMalloc\>/malloc/' -e 's/\<xmlFree\>/free/' \
    third_party/blink/renderer/core/xml/*.cc \
    third_party/blink/renderer/core/xml/parser/xml_document_parser.cc \
    third_party/libxml/chromium/*.cc

  # https://crbug.com/1164975
  patch -Np1 -i ${srcdir}/chromium-glibc-2.33.patch

  # Thanks Voidlinux!
  patch -Np0 -i ${srcdir}/xxx-ppc64le-libvpx.patch
  patch -Np0 -i ${srcdir}/xxx-ppc64le-support.patch

  # Force script incompatible with Python 3 to use /usr/bin/python2
  sed -i '1s|python$|&2|' third_party/dom_distiller_js/protoc_plugins/*.py

  mkdir -p third_party/node/linux/node-linux-ppc64le/bin
  ln -s /usr/bin/node third_party/node/linux/node-linux-ppc64le/bin/

  pushd third_party/libvpx
  mkdir -p source/config/linux/ppc64
  # need PATH to find gn
  PATH="${wrksrc}/out/Release:$PATH" ./generate_gni.sh
  popd

  # Remove bundled libraries for which we will use the system copies; this
  # *should* do what the remove_bundled_libraries.py script does, with the
  # added benefit of not having to list all the remaining libraries
  local _lib
  for _lib in ${_unwanted_bundled_libs[@]}; do
    find "third_party/$_lib" -type f \
      \! -path "third_party/$_lib/chromium/*" \
      \! -path "third_party/$_lib/google/*" \
      \! -path "third_party/harfbuzz-ng/utils/hb_scoped.h" \
      \! -regex '.*\.\(gn\|gni\|isolate\)' \
      -delete
  done

  ./build/linux/unbundle/replace_gn_files.py \
    --system-libraries "${!_system_libs[@]}"
}

build() {
  cd "$srcdir/${pkgname}"/build/src

  if check_buildoption ccache y; then
    # Avoid falling back to preprocessor mode when sources contain time macros
    export CCACHE_SLOPPINESS=time_macros
  fi

  export CC=clang
  export CXX=clang++
  export AR=ar
  export NM=nm

  local _flags=(
    'custom_toolchain="//build/toolchain/linux/unbundle:default"'
    'host_toolchain="//build/toolchain/linux/unbundle:default"'
    'clang_use_chrome_plugins=false'
    'is_official_build=true' # implies is_cfi=true on x86_64
    'chrome_pgo_phase=0' # unsupported instrumentation profile format version
    'treat_warnings_as_errors=false'
    'fieldtrial_testing_like_official_build=true'
    'ffmpeg_branding="Chrome"'
    'proprietary_codecs=true'
    'rtc_use_pipewire=true'
    'rtc_pipewire_version="0.3"' # will be the default in Chromium 90
    'link_pulseaudio=true'
    'use_gnome_keyring=false'
    'use_sysroot=false'
    'use_custom_libcxx=false'
    'enable_hangout_services_extension=true'
    'enable_widevine=true'
    'enable_nacl=false'
  )

  if [[ -n ${_system_libs[icu]+set} ]]; then
    _flags+=('icu_use_data_file=false')
  fi

  if check_option strip y; then
    _flags+=('symbol_level=0')
  fi

  # Facilitate deterministic builds (taken from build/config/compiler/BUILD.gn)
  CFLAGS+='   -Wno-builtin-macro-redefined'
  CXXFLAGS+=' -Wno-builtin-macro-redefined'
  CPPFLAGS+=' -D__DATE__=  -D__TIME__=  -D__TIMESTAMP__='

  # Do not warn about unknown warning options
  CFLAGS+='   -Wno-unknown-warning-option'
  CXXFLAGS+=' -Wno-unknown-warning-option'

  gn gen out/Release --args="${_flags[*]}"
  ninja -C out/Release chrome chrome_sandbox chromedriver

}

package() {
  cd "${srcdir}/${pkgname}"/build/src
  mkdir -p ${pkgdir}/usr/{bin,lib}

  install -D out/Default/chrome "$pkgdir/usr/lib/chromium/chromium"
    		ln -s /usr/lib/chromium/chromium ${pkgdir}/usr/bin/chromium
  install -Dm4755 out/Default/chrome_sandbox "$pkgdir/usr/lib/chromium/chrome-sandbox"
  ln -s /usr/lib/$pkgname/chromedriver "$pkgdir/usr/bin/chromedriver"

  install -Dm644 chrome/installer/linux/common/desktop.template \
    "$pkgdir/usr/share/applications/chromium.desktop"
  install -Dm644 chrome/app/resources/manpage.1.in \
    "$pkgdir/usr/share/man/man1/chromium.1"
  sed -i \
    -e "s/@@MENUNAME@@/Chromium/g" \
    -e "s/@@PACKAGE@@/chromium/g" \
    -e "s/@@USR_BIN_SYMLINK_NAME@@/chromium/g" \
    "$pkgdir/usr/share/applications/chromium.desktop" \
    "$pkgdir/usr/share/man/man1/chromium.1"

  cp \
    out/Default/{chrome_{100,200}_percent,resources}.pak \
    out/Default/{*.bin,chromedriver} \
    "$pkgdir/usr/lib/chromium/"
  install -Dm644 -t "$pkgdir/usr/lib/chromium/locales" out/Default/locales/*.pak

  if [[ -z ${_system_libs[icu]+set} ]]; then
    cp out/Default/icudtl.dat "$pkgdir/usr/lib/chromium/"
  fi

  for size in 24 48 64 128 256; do
    install -Dm644 "chrome/app/theme/chromium/product_logo_$size.png" \
    	"$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/chromium.png"
  done

  for size in 16 32; do
    install -Dm644 "chrome/app/theme/default_100_percent/chromium/product_logo_$size.png" \
    	"$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/chromium.png"
  done

  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/chromium/LICENSE"
}
