# Maintainer (arch): Kyle Keen <keenerd@gmail.com>
# Maintainer: Andreas Grapentin <andreas@grapentin.org>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Contributor: Márcio Silva <coadde@hyperbola.info>

_pkgname=seamonkey
pkgname=iceape
epoch=1
pkgver=2.49.4
pkgrel=5
pkgrel+=.parabola5
_brandingver=2.49
_brandingrel=1
pkgdesc="Iceape, the libre internet suite based on SeaMonkey."
arch=('x86_64')
arch+=('i686' 'armv7h')
license=('MPL')
depends=('alsa-lib'  'dbus-glib'  'desktop-file-utils'  'gtk2' 'gtk3' 'hunspell'  'libevent'  'libnotify'  'libvpx'  'libxt' 'mime-types'  'mozilla-common'  'nss'  'sqlite'  'startup-notification' 'libpulse' 'icu')
makedepends=('unzip' 'zip' 'pkg-config'  'python2' 'yasm' 'wireless_tools' 'mesa' 'autoconf2.13' 'imake' 'python3' 'gst-plugins-base')
makedepends+=('mozilla-searchplugins' 'quilt')
optdepends=('gst-plugins-base: vorbis decoding, ogg demuxing'
            'gst-plugins-bad: aac, vp8 and opus decoding'
            'gst-plugins-good: webm and mp4 demuxing'
            'gst-plugins-ugly: h.264 decoding')
url="https://wiki.parabola.nu/$pkgname"
replaces=("$_pkgname")
conflicts=("$_pkgname")
source=("https://archive.mozilla.org/pub/mozilla.org/seamonkey/releases/$pkgver/source/seamonkey-$pkgver.source.tar.xz"
        mozconfig
        seamonkey-2.0-lang.patch
        patch-bug1435212
        gettid.patch
	no-crmf.diff)
source+=(https://repo.parabola.nu/other/$pkgname/${pkgname}_$_brandingver-$_brandingrel.branding.tar.xz{,.sig}
         libre.patch
         $pkgname.desktop
         xulrunner-27.0-build-arm.patch)
sha256sums=('c8a66774d0a3525698b559fa2c41397186f2de99c5733e35b8eafb0ae5ccc4ee'
            '4e55b0fbbec93e4d7f68118722fa7c61c06b55aa888c6f2923710a0b3953115b'
            'ab19d10fbd6258aec37ab5e5efb12429814b2c24a1193284a48dd654311b4e2e'
            'a24204acc1149f92b96feb33cdb8668f1b938ab907cbcf6fdf8306cb3939976e'
            '923154d052f041af15a2495ceb1f76f7c8619fd8bfc66a0bbedfeab2514adf0b'
            'fb85a538044c15471c12cf561d6aa74570f8de7b054a7063ef88ee1bdfc1ccbb'
            'c6d571ce5fcec98742cc588b56cb543489b73e7e53eeaf0a8f46910f58782ad8'
            'SKIP'
            '5fb5ae632c8c103d8d7be123f32340881372ee1e1b29f19790d058ee8eb16eb0'
            '46ac418fe26937460a36a593d71c8f5cef1288c9e747cac4ec4ca92db56da45d'
            'bc83b8a51c81a401da2fbe1f7cd0843248c32607d3a51119585df074d797922e')
validpgpkeys=('BFA8008A8265677063B11BF47171986E4B745536') # Andreas Grapentin

prepare() {
  cd "$_pkgname-$pkgver"
  cp ../mozconfig .mozconfig
  #patch -Np1 -i ../seamonkey-2.0-lang.patch

  # perform branding
  install -m755 -d $pkgname
  mv -v "$srcdir"/$pkgname-$_brandingver/{branding,extras,patches} $pkgname

  export QUILT_PATCHES=$pkgname/patches
  export QUILT_REFRESH_ARGS='-p ab --no-timestamps --no-index'
  export QUILT_DIFF_ARGS='--no-timestamps'

  quilt push -av

  # set up a simple non-animated throbber from the icon
  for throbber_target_gif in suite/themes/{classic,modern}/communicator/brand/throbber{,16}-anim.png; do
    install -m644 $pkgname/extras/Throbber-small.gif $throbber_target_gif
  done
  for throbber_target_png in suite/themes/{classic,modern}/communicator/brand/throbber{,16}-single.png; do
    install -m644 $pkgname/extras/Throbber-small.png $throbber_target_png
  done

  install -m644 $pkgname/extras/preview.png suite/themes/classic

  # Convert svg file to png and replace seamonkey to iceape icons
  _bdir=$pkgname/branding
  brandingdir=suite/branding/nightly
  for i in 16 48; do
    rsvg-convert -w $i -h $i $_bdir/${pkgname}_icon_plain.svg \
      -o $brandingdir/icons/gtk/default$i.png
    rsvg-convert -w $i -h $i $_bdir/${pkgname}_icon_plain.svg \
      -o $brandingdir/icons/gtk/main-window$i.png
  done

  for i in 32 48 64; do
    rsvg-convert -w $i -h $i $_bdir/${pkgname}_icon_plain.svg \
      -o $brandingdir/content/icon$i.png
  done

  rsvg-convert -w 32 -h 32 $_bdir/${pkgname}_icon_plain.svg \
    -o $brandingdir/icons/gtk/default.png
  rsvg-convert -w 32 -h 32 $_bdir/${pkgname}_icon_plain.svg \
    -o $brandingdir/icons/gtk/main-window.png
  rsvg-convert -w 128 -h 128 $_bdir/${pkgname}_icon_plain.svg \
    -o $brandingdir/icons/gtk/${pkgname}.png
  rsvg-convert -w 200 -h 200 $_bdir/${pkgname}_icon_plain.svg \
    -o $brandingdir/content/logo.gif
  rsvg-convert -w 300 -h 280 $_bdir/${pkgname}_logo_plain.svg \
    -o $brandingdir/content/about.png
  rsvg-convert -w 1138 -h 1152 $_bdir/${pkgname}_background_plain.svg \
    -o $brandingdir/content/messenger-start-bg.png

  # Create app-icons folder for iceape*.png icons
  install -m755 -d $_bdir/app-icons

  # Icons for /usr/share/icons/hicolor
  for i in 16 32 48 64 128; do
    rsvg-convert -w $i -h $i $_bdir/${pkgname}_icon_plain.svg \
      -o $_bdir/app-icons/$pkgname${i}.png
  done

  patch -Np1 -i "$srcdir/libre.patch"

  # FS#48404
  #echo "ac_add_options --enable-gstreamer=1.0" >> .mozconfig

  # ffmpeg 4.0 patch from freebsd
  # https://github.com/freebsd/freebsd-ports/blob/master/www/seamonkey/files/patch-bug1435212
  patch -Np1 -d mozilla -i ../../patch-bug1435212

  # https://bugzilla.mozilla.org/show_bug.cgi?id=1533969
  patch -Np1 -i ../gettid.patch

  # Don't exit with error when some libs are missing which we have in
  # system.
  sed -i '/^MOZ_PKG_FATAL_WARNINGS/s@= 1@= 0@' suite/installer/Makefile.in

  # configure script misdetects the preprocessor without an optimization level
  sed -i '/ac_cpp=/s/$CPPFLAGS/& -O2/' configure

  # 2.26 build fix from wgnie (blfs ch 37)
  sed -i 's/$(MOZ_ZLIB_CFLAGS)/& $(MOZ_PIXMAN_CFLAGS)/' config/config.mk

  # work around from FS#54395
  patch -Np1 -d mozilla -i ../../no-crmf.diff
  sed -i "s/'crmf',//" mozilla/security/manager/ssl/moz.build
  sed -i 's/^.*-lcrmf/#&/' mozilla/config/external/nss/crmf/moz.build
  sed -i 's/ -lcrmf"/"/' mozilla/old-configure.in.orig
  sed -i 's/ -lcrmf"/"/' mozilla/old-configure
  sed -i 's/SDK_LIBS = crmf/SDK_LIBS = /' mozilla/config/external/nss/Makefile.in

  if [[ $CARCH = i686 ]]; then
    LDFLAGS+=" -Wl,--no-keep-memory -Wl,--reduce-memory-overheads"
  fi

  if [[ $CARCH = arm* ]]; then
    echo "ac_add_options --disable-elf-hack" >> .mozconfig
    LDFLAGS+=" -Wl,--no-keep-memory -Wl,--reduce-memory-overheads"
    patch -p2 -d mozilla -i "${srcdir}"/xulrunner-27.0-build-arm.patch
  fi
}

build() {
  cd "$_pkgname-$pkgver"

  export LDFLAGS="$LDFLAGS -Wl,-rpath,/usr/lib/$pkgname-$pkgver"
  export MOZ_MAKE_FLAGS="$MAKEFLAGS"
  unset MAKEFLAGS
  unset CPPFLAGS

  # GCC 6
  CFLAGS+=" -fno-delete-null-pointer-checks -fno-lifetime-dse -fno-schedule-insns2"
  CXXFLAGS+=" -fno-delete-null-pointer-checks -fno-lifetime-dse -fno-schedule-insns2"

  # GCC 9
  CXXFLAGS+=" -Wno-format-overflow"

  make -j1 -f client.mk build
}

package() {
  local _icu_ver
  _icu_ver=$(pacman -S --print-format='%v' icu)
  depends+=("icu>=${_icu_ver}" "icu<$((${_icu_ver%%.*} + 1))")

  cd "$_pkgname-$pkgver"

  make -j1 -f client.mk DESTDIR="$pkgdir" INSTALL_SDK= install

  rm -rf "$pkgdir"/usr/lib/$pkgname-$pkgver/{dictionaries,hyphenation,searchplugins}
  ln -s /usr/share/hunspell "$pkgdir/usr/lib/$pkgname-$pkgver/dictionaries"
  ln -s /usr/share/hyphen "$pkgdir/usr/lib/$pkgname-$pkgver/hyphenation"
  ln -s /usr/lib/mozilla/searchplugins "$pkgdir/usr/lib/$pkgname-$pkgver/searchplugins"

  install -Dm644 suite/branding/nightly/icons/gtk/iceape.png \
                "$pkgdir/usr/share/pixmaps/seamonkey.png"

  install -Dm644 "$srcdir/$pkgname.desktop" \
                "$pkgdir/usr/share/applications/$pkgname.desktop"

  for i in 16 32 48 64 128; do
      install -Dm644 $pkgname/branding/app-icons/$pkgname$i.png \
                    "$pkgdir/usr/share/icons/hicolor/${i}x${i}/apps/$pkgname.png"
  done
  install -Dm644 $pkgname/branding/${pkgname}_icon_plain.svg \
                "$pkgdir/usr/share/icons/hicolor/scalable/apps/$pkgname.svg"

  rm -f "$pkgdir/usr/lib/pkgconfig/"$pkgname-ns{s,pr}.pc
  #rm -r "$pkgdir"/usr/{include,lib/$pkgname-devel-$pkgver,share/idl}
}
