# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

# Note: hpricot was discontinued aboust 6 years ago and it hasn't had a release in 9 years.
# Likewise, ruby-ronn in it's original version has not had a release in about 9 years.
#
# The most ideal solution to this situation is to update to ruby-ng but that seems
# to depend upon an old version of mustache (0.7.x) - see: https://github.com/apjanke/ronn-ng/blob/master/ronn-ng.gemspec

_realname=ronn
pkgbase="mingw-w64-ruby-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-ruby-${_realname}"
pkgver=0.7.3
pkgrel=5
pkgdesc="ronn is a framework-agnostic way to render logic-free views. (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://rtomayko.github.io/ronn/'
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-ruby" 
         "${MINGW_PACKAGE_PREFIX}-ruby-hpricot"
         "${MINGW_PACKAGE_PREFIX}-ruby-mustache"
         "${MINGW_PACKAGE_PREFIX}-ruby-rdiscount")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
source=(https://rubygems.org/downloads/${_realname}-${pkgver}.gem)
noextract=(${_realname}-$pkgver.gem)
sha256sums=('82df6fd4a3aa91734866710d2811a6387e50a7513fc528ce6c7d95ee7ad7f41e')

check() {
  local _gemdir="$(ruby -e 'puts Gem.default_dir')"
  _gemdir="$(cygpath -u ${_gemdir})"

  ${MINGW_PREFIX}/bin/gem check --verbose \
    "${_realname}-${pkgver}.gem"
}

package() {
  local _gemdir="$(${MINGW_PREFIX}/bin/ruby -e 'puts Gem.default_dir')"
  _gemdir="$(cygpath -u ${_gemdir})"

  ${MINGW_PREFIX}/bin/gem install --ignore-dependencies --no-user-install --verbose \
    -i "${pkgdir}${_gemdir}" -n "${pkgdir}${MINGW_PREFIX}/bin" \
    "${_realname}-${pkgver}.gem"

  local _ruby_exe=$(cygpath -m ${MINGW_PREFIX}/bin/ruby.exe)

  #for this conversion, we want the fully qualified ruby .exe path
  #so that we can be sure that we are calling the correct one.
  sed -e "s|${_ruby_exe}|${MINGW_PREFIX}/bin/ruby|g" \
      -i ${pkgdir}${MINGW_PREFIX}/bin/ronn

  #for Win32 .bat files, we want to drop the path references as the path
  #refs are not needed since it's involved from the same dir.  If we did
  #need fully-qualified pathes, they would have to be fully-qualified and
  # in DOS format.
  local _inst_dir=$(cygpath -m ${pkgdir}/${MINGW_PREFIX}/bin/)
  local _w_ruby_exe=$(cygpath -w ${MINGW_PREFIX}/bin/ruby.exe | sed 's/\\/\\\\/g')
  sed  -e "s|${_w_ruby_exe}|ruby.exe|g" \
       -e "s|${_inst_dir}||g" \
       -i ${pkgdir}${MINGW_PREFIX}/bin/ronn.bat

  install -Dm644 "${pkgdir}/${_gemdir}/gems/${_realname}-${pkgver}/man/ronn.1" \
    "${pkgdir}${MINGW_PREFIX}/share/man/man1/ronn.1"

  rm "${pkgdir}${_gemdir}/cache/${_realname}-${pkgver}.gem"
  rm -rf "${pkgdir}${_gemdir}/gems/${_realname}-${pkgver}/man"
}
