# Maintainer: Gavin Luo <lunt.luo@gmail.com>

pkgname=cherry-studio
_name="Cherry Studio"
pkgver=1.9.13
pkgrel=1
pkgdesc='Cherry Studio is a desktop client that supports for multiple LLM providers'
arch=(x86_64)
url=https://github.com/CherryHQ/cherry-studio
license=(AGPL-3.0-only)
_electron=electron41
depends=("$_electron"
         'bash'
         'glibc'
         'libgcc'
         'libstdc++'
         'libxtst'
         'alsa-lib'
         'libxfixes'
         'libevdev'
         'python'
         'wayland'
         'libx11')
optdepends=('ollama: Use your local LLM')
makedepends=('npm' 'pnpm' 'libcrypt.so=1')
source=("$pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz")
b2sums=('2d9cdcebd3235e576343d07f7602961214186586a76abb4b13f63731eae390ebacf370d27902b38484240a8bca85a8258f3a8ae7c3a88b724cb4cf1207aabd55')

prepare() {
    cat >"${pkgname}.desktop" <<EOD
[Desktop Entry]
Name=${_name}
Exec=/usr/bin/${pkgname} --no-sandbox %U
Terminal=false
StartupNotify=false
Type=Application
Icon=${pkgname}
StartupWMClass=${_name/ /}
Comment=A powerful AI assistant for producer.
MimeType=x-scheme-handler/cherrystudio;
Categories=Utility;
EOD

    cat >"${pkgname}.sh" <<EOD
#!/bin/bash
XDG_CONFIG_HOME=\${XDG_CONFIG_HOME:-~/.config}
FLAT_FILE=\${XDG_CONFIG_HOME}/${pkgname}-flags.conf

# Allow users to override command-line options
if [[ -f \${FLAT_FILE} ]]; then
    mapfile -t _USER_FLAGS < <(sed 's/#.*//' "\${FLAT_FILE}" | tr '\n' ' ')
fi

exec /usr/bin/${_electron} /usr/lib/${pkgname}/app.asar "$@" "\${_USER_FLAGS[@]}"
EOD

    cd "$pkgname-$pkgver"
    local _electron_dep=$(npm pkg get devDependencies.electron | tr -d '"^~')
    local _electron_major=${_electron_dep%%.*}
    if [ "${_electron/electron/}" != "$_electron_major" ] ; then
        echo "Error: Incorrect electron version detected. Please change the value of \"_electron\" from \"$_electron\" to \"electron$_electron_major\"."
        return 1
    fi
}

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

    local electronVer="$(</usr/lib/${_electron}/version)"
    npm pkg set devDependencies.electron=${electronVer}

    export ELECTRON_SKIP_BINARY_DOWNLOAD=1
    export NPM_CACHE_DIR=${srcdir}/.npmcache
    HOME="$srcdir/.electron-gyp" pnpm install --ignore-scripts

    export NODE_ENV=production
    export ELECTRON_DIST=/usr/lib/"$_electron"
    export ELECTRON_VERSION=${electronVer}
    pnpm build:unpack

    find ./dist/linux-unpacked/resources/app.asar.unpacked \
        -type d \( -name darwin -o -name '*musl*' \) -print -exec rm -r {} +

    if [[ $CARCH != "arm64" ]]; then
        find ./dist/linux-unpacked/resources/app.asar.unpacked \
            -type d \( -name arm64-linux \) -print -exec rm -r {} +
    fi
}

package() {
    install -Dm644 "${pkgname}.desktop" -t "${pkgdir}/usr/share/applications"
    install -Dm755 "${pkgname}.sh"         "${pkgdir}/usr/bin/${pkgname}"

    cd "${srcdir}/${pkgname}-${pkgver}"
    install -Dm644 "build/logo.png" "$pkgdir/usr/share/icons/${pkgname}.png"

    cd "${srcdir}/${pkgname}-${pkgver}/dist/linux-unpacked/resources"
    install -Dm644                 app.asar            -t "${pkgdir}/usr/lib/${pkgname}/"
    cp -Pr --no-preserve=ownership app.asar.unpacked      "${pkgdir}/usr/lib/${pkgname}/"
}
