#!/bin/sh
#
# 2010-2011 Nico Schottelius (nico-cdist at schottelius.org)
#
# 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/>.
#
# 
# Wrapper script that generates cconfig from arguments
#
# This script will be called everytime the manifest decides to create
# a new type
#

. cdist-config
set -u

################################################################################
# Prepare object and type
#

__cdist_type="$__cdist_myname"

# Find out whether type is a singleton or regular type
if [ -f "$(__cdist_type_singleton "$__cdist_type")" ]; then
   __cdist_object_id="$__cdist_name_singleton"
else
   [ $# -ge 1 ] || __cdist_usage "<id> <options>"
   __cdist_object_id="$1"; shift
fi

# Verify object id
__cdist_object_id_sane=$(echo "$__cdist_object_id" | grep "^${__cdist_sane_regexp}\$")
if [ -z "$__cdist_object_id_sane" ]; then
   __cdist_usage "Insane object id, ${__cdist_object_id}."
fi

# Prevent double slash if id begins with /
if [ "$(echo $__cdist_object_id | grep "^/")" ]; then
   __cdist_object_self="${__cdist_type}${__cdist_object_id}"
else
   __cdist_object_self="${__cdist_type}/${__cdist_object_id}"
fi
################################################################################
# Internal quirks
#

# Append id for error messages
__cdist_myname="$__cdist_myname ($__cdist_object_id)"

################################################################################
# Create object in tmpdir first
#

# Save original destination
__cdist_out_object_dir_orig="$__cdist_out_object_dir"

# Store to tmp now
__cdist_out_object_dir="$__cdist_tmp_dir"

__cdist_new_object_dir="$(__cdist_object_dir "$__cdist_object_self")"

# Initialise object
mkdir -p "${__cdist_new_object_dir}"

# Record parameter
__cdist_parameter_dir="$(__cdist_object_parameter_dir "$__cdist_object_self")"
mkdir -p "${__cdist_parameter_dir}"

while [ $# -gt 0 ]; do
   opt="$1"; shift

   echo "$opt" | grep -q "^--${__cdist_sane_regexp}\$" || \
      __cdist_usage "Provide sane options"

   opt_file="${opt#--}"

   [ $# -ge 1 ] || __cdist_usage "Missing value for $opt"

   value="$1"; shift

   echo "${value}" > "${__cdist_parameter_dir}/${opt_file}"
done

# Record requirements
# it's fine, if it's not set
set +u
for requirement in $require; do
   echo $requirement >> "$(__cdist_object_require "$__cdist_object_self")"
   __cdist_echo info "Recording requirement $requirement"
done
set -u

################################################################################
# Check newly created object
#

#
# Ensure required parameters are given
#
if [ -f "$(__cdist_type_parameter_required "$__cdist_type")" ]; then
   while read required; do
      if [ ! -f "${__cdist_parameter_dir}/${required}" ]; then
         __cdist_usage "Missing required parameter $required"
      fi
   done < "$(__cdist_type_parameter_required "$__cdist_type")"
fi

#
# Ensure that only optional or required parameters are given
#

if [ -f "$(__cdist_type_parameter_optional "$__cdist_type")" ]; then
   cat "$(__cdist_type_parameter_optional "$__cdist_type")" > \
      "$__cdist_tmp_file"
fi
 
if [ -f "$(__cdist_type_parameter_required "$__cdist_type")" ]; then
   cat "$(__cdist_type_parameter_required "$__cdist_type")" >> \
      "$__cdist_tmp_file"
fi
   
cd "$__cdist_parameter_dir"
for parameter in $(ls -1); do
   is_valid=$(grep "^$parameter\$" "$__cdist_tmp_file")

   [ "$is_valid" ] || __cdist_usage "Unknown parameter $parameter"
done

################################################################################
# Merge object
#
# Restore original destination
__cdist_out_object_dir="$__cdist_out_object_dir_orig"

__cdist_object_dir="$(__cdist_object_dir "$__cdist_object_self")"

#
# If the object already exists and is exactly the same, merge it. Otherwise fail.
#
if [ -e "${__cdist_object_dir}" ]; then
   # Allow diff to fail
   set +e
   diff -ru "${__cdist_new_object_dir}/${__cdist_name_parameter}" \
      "${__cdist_object_dir}/${__cdist_name_parameter}"           \
      > "$__cdist_tmp_file"; ret=$?
   set -e
   
   if [ "$ret" != 0 ]; then
      # Go to standard error
      exec >&2
      echo "${__cdist_object_self} already exists differently."
      echo "Recorded source(s):"
      __cdist_object_source "${__cdist_object_dir}"
      echo "Differences:"
      cat "$__cdist_tmp_file"
      __cdist_exit_err "Aborting due to object conflict."
   fi
else
   #
   # Move object into tree:
   # Create full path minus .cdist and move .cdist
   #
   __cdist_new_object_base_dir="$(__cdist_object_base_dir "$__cdist_object_self")"
   mkdir -p "$__cdist_new_object_base_dir"
   mv "$__cdist_new_object_dir"  "$__cdist_new_object_base_dir"
fi

# Add "I was here message"
__cdist_object_source_add "${__cdist_object_dir}"
