#!/bin/env bash
<%= ZTK::Template.do_not_edit_notice(:message => "OmniTruck Bootstrap") %>
set -x

DEBIAN_FRONTEND="noninteractive"
CHEF_SOLO_ROOT="/tmp/chef-solo"
KNIFE_CONFIG_EXP_FILE="/tmp/knife-config.exp"
export DEBIAN_FRONTEND CHEF_SOLO_ROOT KNIFE_CONFIG_EXP_FILE

mkdir -p ${CHEF_SOLO_ROOT}
cd ${CHEF_SOLO_ROOT}

apt-get -y --force-yes update
apt-get -y --force-yes install build-essential expect wget curl libgecode-dev

mkdir -pv /etc/chef /var/log/chef ${HOME}/.chef

cat <<EOF | tee /etc/chef/solo.rb
file_cache_path "${CHEF_SOLO_ROOT}/"
cookbook_path %w(${CHEF_SOLO_ROOT}/cookbooks/ ${CHEF_SOLO_ROOT}/site-cookbooks/)
role_path "${CHEF_SOLO_ROOT}/roles/"
EOF

cat <<EOF | tee ${CHEF_SOLO_ROOT}/attributes.json
<%= @chef_solo_attributes.to_json %>
EOF

wget -v https://www.opscode.com/chef/install.sh
bash install.sh -v <%= @omnibus_version %>
<% if (@chef_pre_11 == false) -%>

mkdir -pv /var/chef/cache ${CHEF_SOLO_ROOT}/cookbooks/chef-server
wget -qO- https://github.com/opscode-cookbooks/chef-server/archive/master.tar.gz | tar xvzC ${CHEF_SOLO_ROOT}/cookbooks/chef-server --strip-components=1
<% else -%>

export PATH=/opt/chef/embedded/bin:$PATH

cat <<EOF | tee ${CHEF_SOLO_ROOT}/Gemfile
source 'https://rubygems.org'
gem 'chef', '<%= @version %>'
gem 'berkshelf'
gem 'moneta', '< 0.7.0'
EOF

cat <<EOF | tee ${CHEF_SOLO_ROOT}/Berksfile
site :opscode
cookbook 'chef-server'
EOF

gem install bundler --no-ri --no-rdoc
bundle install --standalone --path vendor/bundle --binstubs
bin/berks install --path ${CHEF_SOLO_ROOT}/cookbooks/
<% end -%>

PATH=./bin/:$PATH chef-solo --config /etc/chef/solo.rb --json-attributes ${CHEF_SOLO_ROOT}/attributes.json --logfile /var/log/chef/chef-solo.log --log_level debug
<% if (@chef_pre_11 == false) -%>

cat << EOF | tee /etc/chef-server/chef-server.rb
<%= ZTK::Template.do_not_edit_notice(:message => "OmniTruck Bootstrap") %>
server_name = "<%= @server_name %>"
#
topology                "standalone"
api_fqdn                server_name
nginx['url']          = "https://#{server_name}"
nginx['server_name']  = server_name
lb['fqdn']            = server_name
bookshelf['vip']      = server_name
EOF
sudo chef-server-ctl reconfigure
<% end -%>

echo -n "Waiting on <%= File.basename(@chef_validator) %> and <%= File.basename(@chef_webui) %> to appear..."
until [ -f <%= @chef_validator %> ] && [ -f <%= @chef_webui %> ]; do
  echo -n "."
  sleep 1
done
echo "done."

cp -v <%= @chef_validator %> <%= @chef_webui %> ~/.chef
<% if (@chef_pre_11 == false) -%>

ln -sv <%= @chef_validator %> /etc/chef/validation.pem
ln -sv <%= @chef_admin %> /etc/chef/admin.pem
<% end -%>

cat <<EOF | tee ${KNIFE_CONFIG_EXP_FILE}
#!/usr/bin/expect -f
set timeout 10
<% if (@chef_pre_11 == false) -%>
spawn knife configure -i --server-url https://127.0.0.1 --admin-client-key <%= @chef_admin %> -u ${SUDO_USER} -r '' --defaults --yes -VV
<% else -%>
spawn knife configure -i --server-url https://127.0.0.1 -u ${SUDO_USER} -r '' --defaults --yes -VV
<% end -%>
expect "Please enter a password for the new user:" { send "<%= @default_password %>\n" }
interact
EOF
chmod +x ${KNIFE_CONFIG_EXP_FILE}
${KNIFE_CONFIG_EXP_FILE}

knife client create <%= @user %> -a -f ${HOME}/.chef/<%= @user %>.pem --disable-editing --yes -VV
chown -Rv ${SUDO_USER}:${SUDO_USER} ${HOME}