# Modified from https://gitlab.archlinux.org/archlinux/packaging/packages/lf/-/blob/6ffd2d8ad375a154f184b11c3d6ac8e46162c3b9/PKGBUILD
# Contributor: Suyun114
# Contributor: Dirk Stolle

_realname=lf
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=38
pkgrel=1
pkgdesc='A terminal file manager inspired by ranger'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/gokcehan/lf'
msys2_changelog_url='https://github.com/gokcehan/lf/blob/master/CHANGELOG.md'
msys2_references=(
  'anitya: 269016'
  'archlinux: lf'
  'purl: pkg:golang/github.com/gokcehan/lf'
)
license=('MIT')
makedepends=('git' "${MINGW_PACKAGE_PREFIX}-go" "${MINGW_PACKAGE_PREFIX}-cc")
source=("$pkgname::git+$url#tag=r$pkgver")
sha512sums=('f945054b40d6a522d4430eae24280acbb8c1185d2f063970fccc6f088757cf4052fc54945bf471979e3a86ef6cf3a9cd3a59a4c103c31f9388bc72a7020935e2')
b2sums=('017317b995551f7db73cde99008453e278e615788f0b6000921a9965aed9e506c040c08b827908ccc44469330bbf4a7630bae9061a192849733ae4b52021e330')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^r//'
}

prepare() {
  cd "$pkgname"

  # create directory for build output
  mkdir build

  # download dependencies
  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "$pkgname"

  # set Go flags
  export CC=cc
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags '${LDFLAGS}' \
    -X main.gVersion=$pkgver" \
    -o build \
    .
}

check() {
  cd "$pkgname"

  go test -v ./...
}

package() {
  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/${MINGW_PREFIX}/bin" build/lf

  # man page
  install -vDm644 -t "$pkgdir/${MINGW_PREFIX}/share/man/man1" lf.1

  # documentation
  install -vDm644 -t "$pkgdir/${MINGW_PREFIX}/share/doc/$pkgname" README.md etc/lfrc.example

  # desktop file
  install -vDm644 -t "$pkgdir/${MINGW_PREFIX}/share/applications" lf.desktop

  # vim integration
  install -vDm644 -t "$pkgdir/${MINGW_PREFIX}/share/vim/vimfiles/syntax" etc/lf.vim
  install -vDm644 -t "$pkgdir/${MINGW_PREFIX}/share/vim/vimfiles/ftdetect" etc/lf.vim

  # shell integrations
  install -vDm644 -t "$pkgdir/${MINGW_PREFIX}/etc/profile.d" etc/*.{sh,csh}
  install -vDm644 -t "$pkgdir/${MINGW_PREFIX}/share/fish/vendor_functions.d" etc/lfcd.fish

  # shell completions
  install -vDm644 etc/lf.bash "$pkgdir/${MINGW_PREFIX}/share/bash-completion/completions/lf"
  install -vDm644 etc/lf.fish "$pkgdir/${MINGW_PREFIX}/share/fish/vendor_completions.d/lf.fish"
  install -vDm644 etc/lf.zsh "$pkgdir/${MINGW_PREFIX}/share/zsh/site-functions/_lf"

  # license
  install -vDm644 -t "$pkgdir/${MINGW_PREFIX}/share/licenses/$pkgname" LICENSE
}
