#!/bin/sh

set -eu

run() {
  echo "${@}"
  "$@"
}

debrev=$(dpkg-parsechangelog -SVersion | sed -e 's/.*-//')

for pkg in $(dh_listpackages); do
  root=$(grep-dctrl -F Package -n -s X-DhRuby-Root -X "${pkg}" debian/control)
  v=$(cd "${root}" && ruby -Ilib -e "puts Gem::Specification.load('${root}.gemspec').version")

  if [ "${pkg}" = 'ruby-nanoc-live' ]; then
    # special case: ruby-nanoc-live has been uploaded with a higher version
    # copied from the main nanoc version
    if dpkg --compare-versions ${v} lt '4.11.14'; then
      v="4.11.14.really${v}"
    fi
  fi

  run dh_gencontrol -p${pkg} -- -v${v}-${debrev}
done
