# Maintainer: DeepChirp <deepchirp@archlinuxcn.org>
# Contributor: zxp19821005 <zxp19821005 at 163 dot com>

pkgname=losslesscut
_pkgname=LosslessCut
_appid="no.mifi.losslesscut"
pkgver=3.69.0
_electronversion=42
pkgrel=2
pkgdesc="The swiss army knife of lossless video/audio editing"
arch=('x86_64')
url="https://losslesscut.app/"
license=('GPL-2.0-only')
depends=("electron${_electronversion}" 'ffmpeg' 'bash' 'hicolor-icon-theme')
makedepends=('git' 'nodejs-lts-jod' 'npm' 'yarn' 'node-gyp' 'librsvg')
source=(
    "${pkgname}::git+https://github.com/mifi/lossless-cut.git?signed#tag=v${pkgver}"
    "fix-wayland-icon.patch"
    "${pkgname}.sh"
)
validpgpkeys=('1DB3130F9B5EEF992F2742C125AB36E3E81CBC26') # Mikael Finstad <finstaden@gmail.com>
sha256sums=('8c0d0443f98eecdaea64f3002acc86cc57f1d5bec2f30a7066994ddc6e38c12a'
            '305ce3d79e36b8a13fe03d8e64caa05270f6f0b81ae5cb7ed0ebd2b56d7c0e95'
            '31ad33b633744f5361abd964be306cea53ae1050e760c787115f7eca60045ae6')

prepare() {
    sed -i -e "
        s/@electronversion@/${_electronversion}/g
        s/@appname@/${pkgname}/g
        s/@runname@/app.asar/g
        s/@cfgdirname@/${_pkgname}/g
        s/@options@//g
    " "${srcdir}/${pkgname}.sh"

    cd "${pkgname}"

    # Fix the window title bar icon
    patch -Np1 -i "${srcdir}/fix-wayland-icon.patch"

    export npm_config_cache="$srcdir/npm_cache"
    export ELECTRON_SKIP_BINARY_DOWNLOAD=1
    export SYSTEM_ELECTRON_VERSION="$(electron${_electronversion} -v | sed 's/v//g')"
    HOME="${srcdir}/.electron-gyp"
    mkdir -p "${srcdir}/.electron-gyp"

    find src -type f -exec sed -i "s/process.resourcesPath/\'\/usr\/lib\/${pkgname}\'/g" {} +
    sed -e "
        s/\/app\/bin\/run.sh/${pkgname}/g;
        s/${_appid}/${pkgname}/g
    " -i "${_appid}.desktop"
    sed -i "s/\"electron\": \"[^\"]*\"/\"electron\": \"${SYSTEM_ELECTRON_VERSION}\"/g" package.json

    yarn config set --home enableTelemetry 0
    NODE_ENV=development    yarn install
}

build() {
    cd "${pkgname}"

    export npm_config_cache="$srcdir/npm_cache"
    export ELECTRON_SKIP_BINARY_DOWNLOAD=1
    export HOME="${srcdir}/.electron-gyp"
    local electronDist="/usr/lib/electron${_electronversion}"

    NODE_ENV=production     yarn run build

    find -name '*.map' -type f -print -delete

    pushd node_modules

    # Compilation toolchains, type definitions, and browser compatibility data
    rm -rf @babel
    rm -rf @types
    rm -rf browserslist
    rm -rf caniuse-lite
    rm -rf node-gyp
    rm -rf typescript

    # Linting, AST parsers, code analysis, and their underlying utilities
    rm -rf @aashutoshrathi
    rm -rf @eslint
    rm -rf @humanwhocodes
    rm -rf acorn
    rm -rf acorn-jsx
    rm -rf doctrine
    rm -rf eslint*
    rm -rf espree
    rm -rf esquery
    rm -rf estraverse
    rm -rf esutils
    rm -rf file-entry-cache
    rm -rf flat-cache
    rm -rf globals
    rm -rf graphemer
    rm -rf hermes-estree
    rm -rf hermes-parser
    rm -rf ignore
    rm -rf jsx-ast-utils
    rm -rf levn
    rm -rf optionator
    rm -rf prelude-ls
    rm -rf regexp-tree
    rm -rf text-table
    rm -rf type-check

    # Testing frameworks and accessibility (A11y) automation testing tools
    rm -rf aria-query
    rm -rf axe-core
    rm -rf axobject-query
    rm -rf jest*
    rm -rf mocha*

    # Polyfills and old syntax shims
    rm -rf es-abstract
    rm -rf es-iterator-helpers
    rm -rf *.prototype.*

    find -name '*.gypi' -type f -print -delete
    find -name '*.ts' -type f -print -delete
    find -name '*.js.flow' -type f -print -delete
    find -name '*.cts' -type f -print -delete
    find -name '*.yml' ! -name '.yarn-state.yml' -type f -print -delete
    find -name '*.md' -type f -print -delete
    find -name '*.markdown' -type f -print -delete
    find -name .editorconfig -type f -print -delete
    find -name .nycrc -type f -print -delete
    find -name test.js -type f -print -delete
    find -name tsconfig.json -type f -print -delete
    find -name Makefile -type f -print -delete

    find -type d -name 'doc' -prune -exec rm -rf {} +
    find -type d -name 'docs' -prune -exec rm -rf {} +
    find -type d -name 'benchmarks' -prune -exec rm -rf {} +
    find -type d -name 'tests' -prune -exec rm -rf {} +
    find -type d -name '__tests__' -prune -exec rm -rf {} +

    find . -type d -empty -print -delete

    popd

    NODE_ENV=production     yarn electron-builder --linux dir -c.electronDist="${electronDist}"

    for res in 16 32 48 64 128 256 512 1024; do
        mkdir -p "icons-build/${res}x${res}"
        rsvg-convert -w "${res}" -h "${res}" "src/renderer/src/icon.svg" -o "icons-build/${res}x${res}/${pkgname}.png"
    done
}

package() {
    cd "${pkgname}"

    install -Dm755 "${srcdir}/${pkgname}.sh" "${pkgdir}/usr/bin/${pkgname}"

    install -Dm644 "dist/linux-unpacked/resources/app.asar" -t "${pkgdir}/usr/lib/${pkgname}"
    cp -r "dist/linux-unpacked/resources/locales" "${pkgdir}/usr/lib/${pkgname}/"

    ln -sf "/usr/bin/ffmpeg" "${pkgdir}/usr/lib/${pkgname}/ffmpeg"
    ln -sf "/usr/bin/ffprobe" "${pkgdir}/usr/lib/${pkgname}/ffprobe"

    install -vDm644 "src/renderer/src/icon.svg" "${pkgdir}/usr/share/icons/hicolor/scalable/apps/${pkgname}.svg"
    for res in 16 32 48 64 128 256 512 1024; do
        install -Dm644 "icons-build/${res}x${res}/${pkgname}.png" \
            "${pkgdir}/usr/share/icons/hicolor/${res}x${res}/apps/${pkgname}.png"
    done

    install -Dm644 "${_appid}.desktop" -t "${pkgdir}/usr/share/applications"
    install -Dm644 "${_appid}.appdata.xml" -t "${pkgdir}/usr/share/metainfo"
}
