#!/bin/sh -e
#
# 2015 Steven Armstrong (steven-cdist at armstrong.cc)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#

name="$(cat "$__object/parameter/name" 2>/dev/null || echo "$__object_id")"
state="$(cat "$__object/parameter/state")"
conf_dir="/etc/consul-template/conf.d"
conf_file="template_${name}.hcl"
template_dir="/etc/consul-template/template"
require=""

# Sanity checks
if [ -f "$__object/parameter/source" ] && [ -f "$__object/parameter/source-file" ]; then
   echo "Use either --source OR --source-file, but not both." >&2
   exit 1
fi
if [ ! -f "$__object/parameter/source" ] && [ ! -f "$__object/parameter/source-file" ]; then
   echo "Either --source OR --source-file must be given." >&2
   exit 1
fi

if [ -f "$__object/parameter/source-file" ]; then
    destination="${template_dir}/${name}"
    require="__file${destination}"
fi

# Generate hcl config file
{
printf 'template {\n'
cd "$__object/parameter/"
for param in *; do
   case "$param" in
      source-file)
         source="$(cat "$__object/parameter/$param")"
         if [ "$source" = "-" ]; then
            source="$__object/stdin"
         fi
         require="__directory${template_dir}" \
            __file "$destination" \
               --owner root --group root --mode 640 \
               --source "$source" \
               --state "$state"
         printf '   source = "%s"\n' "$destination"

      ;;
      source|destination|command|wait)
         printf '   %s = "%s"\n' "$param" "$(cat "$__object/parameter/$param")"
      ;;
      *)
         # ignore unknown parameters
         :
      ;;
   esac
done
printf '}\n'
} | \
require="$require __directory${conf_dir}" \
   __config_file "${conf_dir}/${conf_file}" \
      --owner root --group root --mode 640 \
      --state "$state" \
      --onchange 'service consul-template status >/dev/null && service consul-template reload || true' \
      --source -
