# Maintainer (arch): Alexander F. Rødseth <xyproto@archlinux.org>
# Contributor: Paulo Matias <matiasΘarchlinux-br·org>
# Contributor: Georgij Kondratjev <smpuj@bk.ru>
# Contributor: Daniel J Griffiths <ghost1227@archlinux.us>
# Contributor: André Silva <emulatorman@hyperbola.info>
# Maintainer: Omar Vega Ramos <ovruni@gnu.org.pe>
# Contributor: bill-auger <bill-auger@programmer.net>


# parabola changes and rationale:
# - remove/replace non-privacy remote services


pkgname=netsurf
pkgver=3.11
pkgrel=1
pkgrel+=.parabola1
pkgdesc='Lightweight and fast web browser'
pkgdesc+=', without non-privacy remote services'
arch=(x86_64)
arch+=(armv7h i686)
url='https://www.netsurf-browser.org/'
license=(GPL2 MIT)
depends=(curl gtk3 'libcss>=0.9.2' 'libdom>=0.4.2' 'libhubbub=0.3.8'
         libjpeg-turbo 'libnsbmp>=0.1.7' 'libnsgif>=1.0.0' 'libnsutils=0.1.1'
         'libutf8proc>=2.9.0' 'libwapcaplet>=0.4.3' libwebp)
# vim is needed only for xxd when building
makedepends=(check inetutils 'nsgenbind>=0.9' perl-html-parser setconf vim)
source=("https://git.netsurf-browser.org/netsurf.git/snapshot/netsurf-release/$pkgver.tar.bz2"
        netsurf.sh)
source+=(remove_nonprivacy_options.patch)
b2sums=('9714735389aa752b5e11a13c3e6e4fc456589517e98fe55025725e05e38a52afea377d53703553ec9d94e492c58f7e1b5ed04c7a0c4fb5158c3053eeedfddcdc'
        'f0220a0c2c03d567434deeed20f821dc110947e16889aa78e12e3f468a431d2f721b9cff23bd6ed3545323f3ed975ea0941f0ee61c4b09938b8bfb99764f8bc8')
b2sums+=('5e0dfb2e3f7fae9a9f346d825480c0cda397444e7ec7e1ae0f41ae0cf31aabff8825ec893cd0e2b940dc21e1599b52454f46e9d53a2b0990f53a7b6ceb382af0')


prepare() {
  cd $pkgname-release/$pkgver
  # Fix compilation issues
  sed -i 's:libutf8proc/::;s:UTF8PROC_CCC_VIRAMA:UTF8PROC_BIDI_CLASS_EN:' utils/idna.c
  # Use "netsurf" as the name of the executable in /usr/bin
  setconf frontends/gtk/res/netsurf-gtk.desktop 'Exec=netsurf %u'


  ## parabola patching ##

  # remove/replace non-privacy remote services
  cd "${srcdir}"/$pkgname-release/$pkgver
  echo "applying remove_nonprivacy_options.patch"
  patch -p1 -i "${srcdir}"/remove_nonprivacy_options.patch

  # replace search form URL and "quick links" on the welcome page
  local welcome_pages=( $(find -name welcome.html) )
  local search_repl='
<form method="get" id="search" action="https://html.duckduckgo.com/html/">
  <div class="websearch">
    <input type="text" name="q" maxlength="255" placeholder="Search" />
    <input type="hidden" name="k9" value="b" />
    <input type="submit" value="Search" name="btnG" />
  </div>
</form>'
  local links_repl='
<h4 style="text-align: center">Quick links:</h4>
<div class="links">
  <ul>
    <li><a href="https://www.parabola.nu/">Parabola</a></li>
    <li><a href="https://www.fsf.org/">FSF</a></li>
    <li><a href="https://www.gnu.org/">GNU</a></li>
  </ul>

  <ul>
    <li><a href="https://html.duckduckgo.com/html/">DuckDuckGo</a></li>
    <li><a href="https://www.wikipedia.org/">Wikipedia</a></li>
  </ul>

  <ul>
    <li><a href="https://creativecommons.org/">Creative Commons</a></li>
    <li><a href="https://www.dictionary.com/">Dictionary.com</a></li>
  </ul>
</div>'
  local search_awk_prog='BEGIN { RS=SUBSEP } { gsub(/<form method="get".*<\/form>/ , repl) ; } 1'
  local links_awk_prog=' BEGIN { RS=SUBSEP } { gsub(/<div class="links">.*<\/div>/ , repl) ; } 1'
  local welcome_page
  for welcome_page in ${welcome_pages[*]}
  do  sed -i '/Download latest NetSurf/d' ${welcome_page}
      awk -i inplace -v repl="${search_repl}" "${search_awk_prog}" ${welcome_page}
      awk -i inplace -v repl="${links_repl}"  "${links_awk_prog}"  ${welcome_page}
      case "${welcome_page}" in
      */en/welcome.html   ) sed -i 's|www.wikipedia.org|en.wikipedia.org|' ${welcome_page} ;;
      */de/welcome.html   ) sed -i 's|www.wikipedia.org|de.wikipedia.org|' ${welcome_page} ;;
      */it/welcome.html   ) sed -i 's|www.wikipedia.org|it.wikipedia.org|' ${welcome_page} ;;
      */ja/welcome.html   ) sed -i 's|www.wikipedia.org|ja.wikipedia.org|' ${welcome_page} ;;
      */nl/welcome.html   ) sed -i 's|www.wikipedia.org|nl.wikipedia.org|' ${welcome_page} ;;
      */zh_CN/welcome.html) sed -i 's|www.wikipedia.org|zh.wikipedia.org|' ${welcome_page} ;;
      esac
  done

  # patching checks
  local err_msg="ERROR: search engines patching needs re-working"
  ! grep -i google ${welcome_pages[*]} || ! echo "$err_msg" || return 1
}

build() {
  export CFLAGS+=' -Os -funroll-loops -w'
  make -C $pkgname-release/$pkgver INCLUDEDIR=include LIBDIR=lib \
    NETSURF_UA_FORMAT_STRING='"Mozilla/5.0 (Arch Linux; %s) NetSurf/%d.%d"' \
    PREFIX=/usr TARGET=gtk3
}

package() {
  make -C $pkgname-release/$pkgver DESTDIR="$pkgdir" INCLUDEDIR=include LIBDIR=lib PREFIX=/usr TARGET=gtk3 install
  install -Dm755 $pkgname.sh "$pkgdir/usr/bin/$pkgname"
  cd $pkgname-release/$pkgver
  install -Dm644 frontends/gtk/res/$pkgname.xpm "$pkgdir/usr/share/pixmaps/$pkgname.xpm"
  install -Dm644 frontends/gtk/res/$pkgname-gtk.desktop "$pkgdir/usr/share/applications/$pkgname.desktop"
  install -Dm644 COPYING "$pkgdir/usr/share/licenses/$pkgname/COPYING"
}
