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

pkgname=netsurf
pkgver=3.9
pkgrel=2
pkgrel+=.par1
pkgdesc='Lightweight and fast web browser'
pkgdesc+=', without non-privacy search engines'
arch=(x86_64)
arch+=(i686 armv7h)
url='https://www.netsurf-browser.org/'
license=(MIT GPL2)
replaces=(${pkgname}-libre)
conflicts=(${pkgname}-libre)
depends=(curl gtk3 'libhubbub=0.3.6' libjpeg-turbo 'libnsutils=0.0.5-4'
         'libutf8proc>=2.4.0' 'libwapcaplet>=0.4.2' 'libdom>=0.4.0')
# vim is needed only for xxd when building
makedepends=(check inetutils 'libcss>=0.9.0' 'libnsbmp>=0.1.5'
             'libnsgif>=0.2.1' 'nsgenbind>=0.7' perl-html-parser setconf vim)
source=("https://download.netsurf-browser.org/netsurf/releases/source-full/netsurf-all-$pkgver.tar.gz"
        netsurf.sh
        remove_nonprivacy_options.patch)
sha256sums=('11e384eaf47e65c186da5001f1055a02f2d72ff17b50f403b8392546a2cf65ff'
            '34c1f41c02ff3791a1b734197d99ec7f58c403dee05f0f9b7b9b3509d32b51ac'
            '61bec340c47a089b1b5c33d3ae330a340999a1e40be7fc8768fa572c6ca3b617')

prepare() {
  cd "$pkgname-all-$pkgver/$pkgname"

  # 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'

  # Set build configuration in Makefile.defaults
  for opt in \
    NETSURF_UA_FORMAT_STRING='"NetSurf/%d.%d (%s; Arch Linux)"' \
    NETSURF_USE_DUKTAPE=YES
  do
    setconf Makefile.defaults "$opt"
  done

  # remove non-privacy search engines
  sed -i '\|Google|d
          \|Yahoo|d
          \|Bing|d
          \|Business.com|d
          \|Omgili|d
          \|BBC News|d
          \|Ubuntu Packages|d
          \|Ask[.]com|d
          \|Answers.com|d
          \|Youtube|d
          \|AeroMp3|d
          \|AOL|d
          \|Baidu|d
          \|Amazon|d
          \|Ebay|d
          \|IMDB|d
          \|ESPN|d
          s|DuckDuckGo|DuckDuckGo HTML|
          s|duckduckgo[.]com|duckduckgo.com/html|
          s|duckduckgo[.]com/html/favicon.ico|duckduckgo.com/favicon.ico|
          s|seeks-project.info|seeks.fr|g
         ' frontends/riscos/appdir/Resources/SearchEngines \
           resources/SearchEngines
  sed -i '\|readable name such as| s|google|duckduckgo|
          \|readable name such as| s|yahoo|seeks|
          \|host address such as www[.]google[.]com| s|google[.]com|duckduckgo.com/html|
          s|google[.]com?search=%s|duckduckgo.com/html/?q=%s|
          \|return strdup| s|google[.]com|duckduckgo.com/html|
          \|return strdup| s|google|duckduckgo|
          \|return strdup| s|duckduckgo[.]com/html/favicon[.]ico|duckduckgo.com/favicon.ico|
          s|www[.]google[.]com/search?q=%s|duckduckgo.com/html/?q=%s|
         ' desktop/searchweb.c
  sed -i 's|google[.][.a-z]\{0,6\}/search|duckduckgo.com/html/search|
          s|Google-Suche|DuckDuckGo HTML|
          s|Google Search|DuckDuckGo HTML|
          s|Ricerca Google|DuckDuckGo HTML|
          s|Google検索|DuckDuckGo HTML|
          \|BBC News|d
          \|Reuters|d
          \|CNN|d
          \|Slashdot|d
          \|Ars Technica|d
          \|The Register|d
          \|W3C|d
          s|google[.][.a-z]\{0,6\}|duckduckgo.com/html|
          s|Google|DuckDuckGo HTML|
          s|[.a-z]\{0,4\}yahoo[.]co[.a-z]\{0,4\}|www.seeks.fr|
          s|Yahoo!|Seeks|
          \|IMDB|d
          s|The Icon Bar|Creative Commons|
          s|www[.]iconbar[.]com|creativecommons.org|
          s|ROOL|Dictionary.com|
          s|www[.]riscosopen[.]org|dictionary.reference.com|
          \|riscos.info|d
         ' frontends/riscos/appdir/Resources/de/welcome.html\,faf \
           frontends/riscos/appdir/Resources/en/welcome.html\,faf \
           frontends/riscos/appdir/Resources/it/welcome.html\,faf \
           frontends/riscos/appdir/Resources/ja/welcome.html\,faf \
           frontends/riscos/appdir/Resources/nl/welcome.html\,faf
  sed -i 's|Google|DuckDuckGo HTML|
         ' frontends/amiga/dist/NetSurf.guide
  # patch and remove anything that's left
  patch -p1 -i ../../remove_nonprivacy_options.patch
}

build() {
  CFLAGS="$CFLAGS -w -Os -funroll-loops" make \
    -C "$pkgname-all-$pkgver/$pkgname" \
    NETSURF_GTK_MAJOR=3 \
    TARGET=gtk \
    LIBDIR=lib \
    INCLUDEDIR=include \
    PREFIX=/usr
}

package() {
  make -C "$pkgname-all-$pkgver/$pkgname" \
    TARGET=gtk \
    NETSURF_GTK_MAJOR=3 \
    LIBDIR=lib \
    INCLUDEDIR=include \
    DESTDIR="$pkgdir" \
    PREFIX=/usr \
    install

  # Launch script
  install -Dm755 $pkgname.sh "$pkgdir/usr/bin/$pkgname"

  cd "$pkgname-all-$pkgver/$pkgname"

  # Desktop icon and shortcut
  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"

  # License
  install -Dm644 COPYING "$pkgdir/usr/share/licenses/$pkgname/COPYING"
}

# vim: ts=2 sw=2 et: