_gemname=em-websocket
pkgname=ruby-$_gemname
pkgver=0.5.3
pkgrel=1
pkgdesc="EventMachine based WebSocket server"
arch=('any')
url="https://github.com/igrigorik/em-websocket"
license=('MIT')
depends=(
  'ruby'
  'ruby-eventmachine'
  'ruby-http_parser.rb'
)
options=('!emptydirs')
source=("$pkgname-$pkgver.tar.gz::$url/archive/v$pkgver.tar.gz")
sha512sums=('28d5cf042899319f50ff0d3b140d75fb281b7e0635118a63df243151c4f442dbabf7dbe15b26b44167d925ed00819b01eeeef110107e4058d8a534f84913b232')
b2sums=('ce38ee1adc9cd49454d74d94130e9d759c631337da9401390fae68210f7b02692a0f23eb5970aa226ab3651e1924a117ac104a01481cc954ca77ab9c27b1e5ac')

prepare() {
  cd "$_gemname-$pkgver"

  # mangle gemspec
  sed -E \
    -e 's/~>/>=/g' \
    -e 's|git ls-files|find -type f\|sed "s,\\\\./,,g"|' \
    -e '/test_files/d' \
    -e '/executables/d' \
    -i "$_gemname.gemspec"
}

build() {
  cd "$_gemname-$pkgver"

  gem build "$_gemname.gemspec"
}

package() {
  local _gemdir="$(ruby -e'puts Gem.default_dir')"

  cd "$_gemname-$pkgver"

  gem install \
    --ignore-dependencies \
    --no-user-install \
    --no-document \
    --install-dir "$pkgdir/$_gemdir" \
    --bindir "$pkgdir/usr/bin" \
    "$_gemname-$pkgver.gem"

  # delete cache
  cd "$pkgdir/$_gemdir"
  rm -vrf cache

  # delete unnecessary files & folders
  cd "gems/$_gemname-$pkgver"
  rm -vrf .gitignore spec Gemfile Rakefile "$_gemname.gemspec"


  # move documentation
  install -vd "$pkgdir/usr/share/doc/$pkgname"
  mv -vt "$pkgdir/usr/share/doc/$pkgname" \
    CHANGELOG.rdoc README.md examples

  # move license
  install -vd "$pkgdir/usr/share/licenses/$pkgname"
  mv -v -t "$pkgdir/usr/share/licenses/$pkgname" LICENCE
}