#!/usr/bin/env bash
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT

pkgnames=(zshelf)
pkgdesc="Z-Library browser and downloader"
url=https://github.com/khanhas/zshelf
pkgver=0.3.1-4
section=utils
timestamp=2021-02-20T01:45Z
maintainer="khanhas <xuankhanh963@gmail.com>"
license=GPL-3.0
installdepends=(node display)
makedepends=(build:imagemagick build:librsvg2-bin)
flags=(patch_rm2fb)

image=qt:v2.1
source=(
    https://github.com/khanhas/zshelf/archive/v0.3.1.zip
    zshelf.draft
    zshelf.png
)
sha256sums=(
    8069277e24b58b44466b038c5b319daa824f39bdcbdea0731fedb0b5263e3bd7
    SKIP
    SKIP
)

build() {
    qmake zshelf.pro
    make

    curl https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz > /root/node.tar.xz
    tar -C / -xf /root/node.tar.xz
    export PATH="/node-v14.15.4-linux-x64/bin:$PATH"

    cd backend
    npm install
}

package() {
    install -d "$pkgdir"/opt/lib/zshelf

    cp -r "$srcdir"/backend "$pkgdir"/opt/lib/zshelf/backend
    install -D -m 755 -t "$pkgdir"/opt/lib/zshelf "$srcdir"/zshelf
    install -d "$pkgdir"/opt/bin
    ln -s /opt/lib/zshelf/zshelf "$pkgdir"/opt/bin/zshelf

    install -d "$pkgdir"/opt/etc/zshelf
    install -D -m 644 -t "$pkgdir"/opt/etc/zshelf "$srcdir"/config.json
    ln -s /opt/etc/zshelf/config.json "$pkgdir"/opt/lib/zshelf/config.json

    install -D -m 644 -t "$pkgdir"/opt/etc/draft "$srcdir"/zshelf.draft
    install -D -m 644 -t "$pkgdir"/opt/etc/draft/icons "$srcdir"/zshelf.png
}

configure() {
    echo ""
    echo "To download books, you have to configure domain and cookie first."
    echo "Please follow instructions on:"
    echo "https://github.com/khanhas/zshelf#important-configure-domain-and-cookie"
    echo "Config file locates at:" /opt/etc/zshelf/config.json
    echo ""
}
