#! /bin/bash
JSON_FILE=$1

if [ -z "$JSON_FILE" ]; then
  JSON_FILE=/etc/chef/run-chef-client.json
  chmod 700 $JSON_FILE
fi

if [ "$(id -u)" != "0" ]; then
  echo "This script must be run as root" 2>&1
  exit 1
fi

cd /etc/chef

# Backup latest Chef node previously executed, preserving permissions and timestamp
# Chef nodes are created everytime Chef runs successfully
CHEF_NODE_NAME=`ls -lt /etc/chef/nodes | grep -v total | head -1 | awk '{print $9}'`
if [ -n "$CHEF_NODE_NAME" ]; then
  BACKUP_NODE_NAME=$CHEF_NODE_NAME-`date +"%Y%m%d%k%M"`
  mkdir -p /var/log/chef-runs
  mv /etc/chef/nodes/$CHEF_NODE_NAME /var/log/chef-runs/$BACKUP_NODE_NAME
  rm -rf /etc/chef/nodes/$CHEF_NODE_NAME
  chmod -R 700 /etc/chef/nodes
fi

chef-client --local-mode -j $JSON_FILE >> /var/log/chef-client.log

cd -
