#!/bin/sh
#
# 2010 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/>.
#
# 
# Let's build a cconfig tree from a configuration
# And save it into the cache tree
#

. cdist-config

[ "${__cdist_target_host}" ] || __cdist_usage "Need \$__cdist_target_host setup"

set -au

__cdist_tree_wrapper="$(which cdist_tree_wrapper)"

# used to build the tree and other stuff
__cdist_bindir="${__cdist_tmp_dir}/bin"

# FIXME: create cache/bin dir only once / do that in its
# own function

# Save output in cache
__cdist_output="${__cdist_cache_hosts}/${__cdist_target_host}"

cdist_type_prefix="__"

################################################################################
# Begin execution

cdist_tree_wrapper_name=${cdist_tree_wrapper##*/}
cdist_tree_wrapper_absdir=$(cd ${cdist_tree_wrapper%/*} && pwd -P)
cdist_tree_wrapper_abs="$cdist_tree_wrapper_absdir/$cdist_tree_wrapper_name"


rm -rf "${__cdist_tmp_dir}"

# contains cdist-tree binaries
mkdir -p "${cdist_bindir}"
#ln -s "${cdist_tree_wrapper_abs}" "${bindir}"

# Create our binaries, will are called from the manifest - temporary
(
   set -e
   cd "${__cdist_type_dir}"
   # FIXME: replace with redirect to file + read
   for bin in *; do
      ln -s "${cdist_tree_wrapper_abs}" "${cdist_type_prefix}${bin}"
   cd "${cdist_bindir}"
   done
)

# Cdist_tree_wrapper does not need any other tool, so remove them from the path.
# but the manifest may need so - FIXME: think about it.
#unset PATH

# prepend our path
PATH="${__cdist_cache_bin}:$PATH"

# create output dir
mkdir -p "${cdist_confdir}"

# force -x, so the user is aware the file is executed
if [ ! -x "${__cdist_manifest_init}" ]; then
   __cdist_exit_err "${__cdist_manifest_init} needs to be executable."
fi

"${__cdist_manifest_init}"

echo "Finished tree generation, have a look at ${cdist_confdir}."