#!/bin/bash

set -e -x

SCRIPTS_DIR=$(dirname ${0})

BASE_DIR=${1}
CLOUD_DATA=${2}
IFS=',' read -ra FILES <<< "${3}"

if [ -z ${BASE_DIR} ]; then
    echo "Need base directory to place files" 1>&2
    exit 1
fi

mkdir -p ${BASE_DIR}/var/lib/rancher/conf/cloud-config.d
if [ "${CLOUD_DATA}" != "${SCRIPTS_DIR}/conf/empty.yml" ]; then
    cp ${CLOUD_DATA} ${BASE_DIR}/var/lib/rancher/conf/cloud-config.d/
fi

for f in ${FILES[@]}; do
    IFS=":" read s d <<< "${f}"
    mkdir -p $(dirname ${BASE_DIR}/${d})
    cp -a -T ${s} ${BASE_DIR}/${d}
done
