#!/bin/sh

set -e

if [ $# -gt 1 ]; then
  echo "usage: dh_vagrant_plugin [PACKAGENAME]"
  echo
  echo "If ommitted, PACKAGENAME will be taken from first line of output from"
  echo "dh_listpackages"
  echo
  exit 1
fi

if ! [ -f debian/changelog -a -f debian/control ]; then
  echo "E: must be run from inside a Debian source package"
  exit 1
fi

plugin_package_name="$1"
if [ -z "$plugin_package_name" ]; then
  plugin_package_name=$(dh_listpackages | head -n 1)
fi

plugins_d=debian/${plugin_package_name}/usr/share/vagrant-plugins/plugins.d
plugin_json=$plugins_d/${plugin_package_name}.json

mkdir -pv ${plugins_d}

ruby_version=$(ruby -e 'puts RUBY_VERSION')
vagrant_version=$(cat /usr/share/vagrant/version.txt)

cat >> $plugin_json <<EOF
{
  "${plugin_package_name}": {
    "ruby_version":"$(ruby -e 'puts RUBY_VERSION')",
    "vagrant_version":"$(cat /usr/share/vagrant/version.txt)",
    "gem_version":"",
    "require":"",
    "sources":[]
  }
}
EOF
echo "created $plugin_json"
