# Modified from https://gitlab.archlinux.org/archlinux/packaging/packages/micro/-/blob/dffc678799d1ade1edd55aec66f9312a3d4c8ea3/PKGBUILD

_realname=micro
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.0.13
pkgrel=1
# manually set date instead of "go run tools/build-date.go", for reproducible builds
_build_date='April 4, 2024'
pkgdesc='Modern and intuitive terminal-based text editor'
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://micro-editor.github.io/'
msys2_repository_url='https://github.com/zyedidia/micro'
license=('spdx:MIT')
makedepends=(git "${MINGW_PACKAGE_PREFIX}-go" "${MINGW_PACKAGE_PREFIX}-cc")
source=("git+https://github.com/zyedidia/micro#commit=68d88b571de6dca9fb8f03e2a3caafa2287c38d4") # tag: v2.0.13
b2sums=('SKIP')

prepare() {
  cd $_realname
  # Let tools/build-date.go always return the same date, to make the package reproducible
  echo -e '//+build ignore\npackage main\nimport "fmt"\nfunc main() { fmt.Println("'$_build_date'") }' > tools/build-date.go
  # Modify the Go build flags
  sed -i 's/-trimpath/-buildmode=pie -trimpath/g;s/GS)/GS) -linkmode=external -extldflags \\"$(LDFLAGS)\\"/g' Makefile
}

build() {
  make -C $_realname CC=cc
}

check() {
  make -C $_realname test
}

package() {
  cd $_realname
  install -Dm755 $_realname "$pkgdir/usr/bin/$_realname"
  install -Dm644 assets/packaging/micro.1 -t "$pkgdir/usr/share/man/man1"
  install -Dm644 assets/packaging/micro.desktop -t "$pkgdir/usr/share/applications"
  install -Dm644 assets/micro-logo-mark.svg "$pkgdir/usr/share/icons/hicolor/scalable/apps/$_realname.svg"
  install -Dm644 -t "$pkgdir/usr/share/licenses/$_realname" LICENSE LICENSE-THIRD-PARTY
}
