# Maintainer (Arch): Nicola Squartini <tensor5@gmail.com>
# Maintainer: André Silva <emulatorman@hyperbola.info>

pkgname=min
pkgver=1.8.1
pkgrel=1
pkgrel+=.parabola1
pkgdesc='A smarter, faster web browser'
pkgdesc+=', without non-privacy search engines'
arch=('any')
url='https://minbrowser.github.io/min'
license=('Apache')
depends=('electron')
makedepends=('grunt-cli' 'npm')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/minbrowser/min/archive/v${pkgver}.tar.gz"
        'min.desktop'
        'min.js'
        'icon.patch'
        'remove_nonprivacy_options.patch')
sha256sums=('56e5f03fc969bd5ebbb900fc89987c28e19e09710590ae9e9db47422b79a7d50'
            'a779a8daff1d684779ebc6d13ea4c2f09e838df018de41fa19fa3f25aa12e32f'
            'c22324184b72b3fad5a0aadb4e18182414e0294c5596c26426adc204fd473258'
            'bb5e55a3c4074e1419f25b0605b77ef622d2870235309e0feed88f790967a936'
            '76c8cc886007be01301c619b3ac1fc86369448e641ca9ae667a2cdd84b9c2276')

prepare() {
    cd ${pkgname}-${pkgver}

    patch -Np1 -i "${srcdir}"/icon.patch
    patch -Np1 -i "${srcdir}"/remove_nonprivacy_options.patch
}

build() {
    cd ${pkgname}-${pkgver}

    npm install
    grunt
    rm -r node_modules

    npm install --production --no-optional
}

package() {
    cd ${pkgname}-${pkgver}

    appdir=/usr/lib/${pkgname}

    install -dm755 "${pkgdir}"${appdir}
    cp -r * "${pkgdir}"${appdir}

    install -dm755 "${pkgdir}"/usr/share/icons/hicolor/256x256/apps
    mv icons/icon256.png \
        "${pkgdir}"/usr/share/icons/hicolor/256x256/apps/${pkgname}.png

    install -Dm755 "${srcdir}"/${pkgname}.js "${pkgdir}"/usr/bin/${pkgname}
    install -Dm644 "${srcdir}"/${pkgname}.desktop \
            "${pkgdir}"/usr/share/applications/${pkgname}.desktop

    # Clean up
    rm "${pkgdir}"${appdir}/dist/build.js
    rm -r "${pkgdir}"${appdir}/icons
    rm -r "${pkgdir}"${appdir}/localization
    rm -r "${pkgdir}"${appdir}/main
    rm -r "${pkgdir}"${appdir}/scripts
    find "${pkgdir}"${appdir} \
        -name "package.json" \
            -exec sed -e "s|${srcdir}/${pkgname}-${pkgver}|${appdir}|" \
                -i {} \; \
        -or -name ".*" -prune -exec rm -r '{}' \; \
        -or -name "Gruntfile.js" -exec rm '{}' \; \
        -or -name "Makefile" -exec rm '{}' \; \
        -or -name "bin" -prune -exec rm -r '{}' \; \
        -or -name "bin.js" -exec rm '{}' \; \
        -or -name "bower.json" -exec rm '{}' \; \
        -or -name "cli.js" -exec rm '{}' \; \
        -or -name "cmd.js" -exec rm '{}' \; \
        -or -name "coffee" -prune -exec rm -r '{}' \; \
        -or -name "example" -prune -exec rm -r '{}' \; \
        -or -name "examples" -prune -exec rm -r '{}' \; \
        -or -name "gulpfile.js" -exec rm '{}' \; \
        -or -name "man" -prune -exec rm -r '{}' \; \
        -or -name "scripts" -prune -exec rm -r '{}' \; \
        -or -name "test" -prune -exec rm -r '{}' \; \
        -or -name "tests" -prune -exec rm -r '{}' \;
}
