# Copyright 2024 Foster Hangdaan
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

pkgname=deno-lts
pkgver=2.1.4
pkgrel=1
_rusty_v8_ver=130.0.1
pkgdesc="A modern runtime for JavaScript and TypeScript. (LTS release)"
arch=('x86_64')
url="https://deno.land"
license=('MIT')
options=('!lto')
conflicts=('deno')
depends=('gcc-libs')
makedepends=('git' 'python' 'rust' 'nodejs' 'gn' 'ninja' 'clang' 'lld' 'cmake' 'protobuf')
source=("deno::git+https://code.fosterhangdaan.com/foster/denoland-deno.git#tag=v${pkgver}"
        "rusty_v8::git+https://code.fosterhangdaan.com/foster/denoland-rusty-v8.git#tag=v${_rusty_v8_ver}"
        "compiler-rt-adjust-paths.patch")
sha512sums=('d890b20d3736c667d9c25d6dc9d30875b6ee1a667dd99f2c0327a8087114e825efbaaa42863a9cee058abdc6c8159b63b7760bc081a86cdc530d2b6135809147'
            '97d293e9aec8b5f36c4d8c09c2cc6bbbe41f8102b5f79d1119758813e16c30371ab664be34334cca6f5dbaa776c0f542a101b706bd19a72db9d0be90a4eb8c13'
            '0c2ece5e4dfe3cb98acf29feb420b728ffc60472a0613bfcfbb4930d9186c54cece0840ba7ec9327b6ea5ed4f052189795086ee7c6b6fb77ec781504c4ebae7a')

prepare() {
  cd rusty_v8
  git config -f .gitmodules submodule.v8.shallow true
  git submodule update --init --recursive

  # https://github.com/denoland/rusty_v8/issues/1587
  patch -Np1 -i ../compiler-rt-adjust-paths.patch

  cd ../deno
  echo -e "\n[patch.crates-io]\nv8 = { path = '../rusty_v8' }" >> Cargo.toml
}

build() {
  cd deno

  # this uses malloc_usable_size, which is incompatible with fortification level 3
  export CFLAGS="${CFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"
  export CXXFLAGS="${CXXFLAGS/_FORTIFY_SOURCE=3/_FORTIFY_SOURCE=2}"

  local _clang_version=$(clang --version | grep -m1 version | sed 's/.* \([0-9]\+\).*/\1/')
  local _extra_gn_args=(
    'custom_toolchain="//build/toolchain/linux/unbundle:default"'
    'host_toolchain="//build/toolchain/linux/unbundle:default"'
    "clang_version=\"$_clang_version\""
  )

  export CC=clang CXX=clang++ AR=ar NM=nm
  export V8_FROM_SOURCE=1
  export CLANG_BASE_PATH=/usr
  export GN=/usr/bin/gn NINJA=/usr/bin/ninja
  export EXTRA_GN_ARGS="${_extra_gn_args[@]}"

  cargo build --release
}

check() {
  cd deno
  ./target/release/deno run tests/testdata/run/002_hello.ts
}

package() {
  cd deno
  install -Dm755 target/release/deno "$pkgdir"/usr/bin/deno

  install -dm755 "$pkgdir"/usr/share/bash-completion/completions
  ./target/release/deno completions bash > "$pkgdir"/usr/share/bash-completion/completions/deno
  install -dm755 "$pkgdir"/usr/share/zsh/site-functions
  ./target/release/deno completions zsh > "$pkgdir"/usr/share/zsh/site-functions/_deno
  install -dm755 "$pkgdir"/usr/share/fish/vendor_completions.d
  ./target/release/deno completions fish > "$pkgdir"/usr/share/fish/vendor_completions.d/deno.fish

  install -Dm644 LICENSE.md -t "$pkgdir"/usr/share/licenses/deno/
}