# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=scite
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-defaults")
pkgver=4.4.6
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://www.scintilla.org/SciTE.html'
license=('custom:scite')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gtk3"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("https://www.scintilla.org/${_realname}${pkgver//./}.tgz"
        "0001-Use-POSIX-tools-in-makefiles.patch"
        "0002-Prefix-library-names.patch"
        "0003-Use-FHS.patch")
sha256sums=('1c2e6eee6c8aa2e52983d9713a4b1c97d06c376324a3ec8932adfcb9e55d16d1'
            '845ff26cc58eca51ff569074766c0acded095a0ec06a7bd4f7ccefc400c9536c'
            'e9616d2213ab6c35d3db318b3a8aac835169bee6a9bda04ab96aa33d13f47b79'
            '8add5e0d508ed16a891060cc2253d2cf25745c9c67f4ded70d50997749c9e161')

prepare() {
  cd "${srcdir}"
  patch -p1 -i "${srcdir}"/0001-Use-POSIX-tools-in-makefiles.patch
  patch -p1 -i "${srcdir}"/0002-Prefix-library-names.patch
  patch -p1 -i "${srcdir}"/0003-Use-FHS.patch
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}
  mkdir -p "${srcdir}"/build-${MINGW_CHOST}
  cp -r "${srcdir}"/{scite,scintilla} "${srcdir}"/build-${MINGW_CHOST}
  cd "${srcdir}"/build-${MINGW_CHOST}

  windir=1 GTK3=1 CC="${MINGW_PREFIX}/bin/gcc" CXX="${MINGW_PREFIX}/bin/g++" make -C scintilla/gtk
  windir=1 CC="${MINGW_PREFIX}/bin/gcc" CXX="${MINGW_PREFIX}/bin/g++" make -C scintilla/win32
  # GTK3=1 make -C scite/gtk
  windir=1 CC="${MINGW_PREFIX}/bin/gcc" CXX="${MINGW_PREFIX}/bin/g++" make -C scite/win32
}

package_scite() {
  pkgdesc="Editor with facilities for building and running programs (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2"
           "${MINGW_PACKAGE_PREFIX}-gtk3")
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}-defaults: Default language files")

  cd "${srcdir}"/build-${MINGW_CHOST}

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/{bin,lib}
  cp scintilla/bin/lib{Scintilla,SciLexer,lexilla}.dll "${pkgdir}"${MINGW_PREFIX}/bin
  cp scintilla/bin/lib{Scintilla,SciLexer,lexilla}.dll.a "${pkgdir}"${MINGW_PREFIX}/lib
  cp scite/bin/SciTE.exe "${pkgdir}"${MINGW_PREFIX}/bin

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/lib
  cp scintilla/bin/libScintillaGtk.a "${pkgdir}"${MINGW_PREFIX}/lib

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/include/scintilla
  cp scintilla/include/{*.h,*.iface} "${pkgdir}"${MINGW_PREFIX}/include/scintilla

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/scite
  cp scite/bin/SciTEGlobal.properties "${pkgdir}"${MINGW_PREFIX}/share/scite

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/doc/scite
  cp scite/doc/{*.png,*.jpg,*.html} "${pkgdir}"${MINGW_PREFIX}/share/doc/scite

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/man/man1
  cp scite/doc/scite.1 "${pkgdir}"${MINGW_PREFIX}/share/man/man1/SciTE.1

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite
  cp scintilla/License.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite/LICENSE-scintilla
  cp scite/License.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite/LICENSE-scite
  cp scite/lua/COPYRIGHT "${pkgdir}"${MINGW_PREFIX}/share/licenses/scite/LICENSE-lua
}

package_scite-defaults() {
  pkgdesc="Default language files for the SciTE editor (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}")

  cd "${srcdir}"/build-${MINGW_CHOST}

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/scite
  cp scite/bin/*.properties "${pkgdir}"${MINGW_PREFIX}/share/scite
  rm -f "${pkgdir}"${MINGW_PREFIX}/share/scite/SciTEGlobal.properties
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
