ksh -c '

function exists {
  if type $1 >/dev/null 2>&1
  then
    return 0
  else
    return 1
  fi
}

if ! exists /usr/bin/chef-client; then
  <% if @chef_config[:aix_package] -%>
    # Read the download URL/location from knife.rb with option aix_package
    rm -rf /tmp/chef_installer # ensure there no older pkg
    echo "<%= @chef_config[:aix_package] %>"
    perl -e '\''use LWP::Simple; getprint($ARGV[0]);'\'' <%= @chef_config[:aix_package] %> > /tmp/chef_installer
    installp -aYF -d  /tmp/chef_installer chef
  <% else -%>
     echo ":aix_package location is not set in knife.rb"
     exit
  <% end -%>
fi

mkdir -p /etc/chef

<% if client_pem -%>
cat > /etc/chef/client.pem <<'EOP'
<%= ::File.read(::File.expand_path(client_pem)) %>
EOP
chmod 0600 /etc/chef/client.pem
<% end -%>

<% if validation_key -%>
cat > /etc/chef/validation.pem <<'EOP'
<%= validation_key %>
EOP
chmod 0600 /etc/chef/validation.pem
<% end -%>

<% if encrypted_data_bag_secret -%>
cat > /etc/chef/encrypted_data_bag_secret <<'EOP'
<%= encrypted_data_bag_secret %>
EOP
chmod 0600 /etc/chef/encrypted_data_bag_secret
<% end -%>

<% unless trusted_certs.empty? -%>
mkdir -p /etc/chef/trusted_certs
<%= trusted_certs %>
<% end -%>

<%# Generate Ohai Hints -%>
<% unless @chef_config[:knife][:hints].nil? || @chef_config[:knife][:hints].empty? -%>
mkdir -p /etc/chef/ohai/hints

<% @chef_config[:knife][:hints].each do |name, hash| -%>
cat > /etc/chef/ohai/hints/<%= name %>.json <<'EOP'
<%= Chef::JSONCompat.to_json(hash) %>
EOP
<% end -%>
<% end -%>

cat > /etc/chef/client.rb <<'EOP'
<%= config_content %>
EOP

cat > /etc/chef/first-boot.json <<'EOP'
<%= Chef::JSONCompat.to_json(first_boot) %>
EOP

<%= start_chef %>'
