#!/bin/bash
#
# Perform necessary chef-server setup steps after package is installed.
#

PROGNAME=$(basename $0)

function error_exit
{
	echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
	exit 1
}

ln -sf /opt/opscode/bin/private-chef-ctl /usr/bin || error_exit "Cannot link private-chef-ctl in /usr/bin"
ln -sf /opt/opscode/bin/chef-server-ctl /usr/bin || error_exit "Cannot link chef-server-ctl in /usr/bin"
mkdir -p /var/opt/opscode/plugins
ln -sf /opt/opscode/chef-server-plugin.rb /var/opt/opscode/plugins/chef-ha-drbd.rb || error_exit "Cannot link chef-ha-drbd.rb in /var/opt/opscode/plugins"

mkdir -p /etc/opscode
touch /etc/opscode/chef-server.rb
# chef-server.rb might contain secrets, restrict permissions of other.
chmod o-rwx /etc/opscode/chef-server.rb

# Ensure all files/directories in $INSTALLER_DIR are owned by root. This
# has been fixed on new installs but upgrades from old installs need to
# be manually fixed.
chown -Rh 0:0 /opt/opscode

if [ -e /etc/opscode/chef-server-running.json ]; then
  echo -e "\033[1mYou have upgraded Chef Server!\033[0m"
  echo
  echo "The next step in the upgrade process is to run:"
  echo
  echo -e "\033[1;31mchef-server-ctl upgrade\033[0m"
  echo
  echo "After the upgrade command completes, your Chef services will"
  echo "remain in a down state. To bring them back up run:"
  echo
  echo -e "\033[1;31mchef-server-ctl start\033[0m"
  echo
  echo "Then, to remove configuration files, logs, directories,"
  echo "users, etc. that were used by internal services that"
  echo "have been removed from this version of Chef Server,"
  echo "please run:"
  echo
  echo -e "\033[1;31mchef-server-ctl cleanup\033[0m"
  echo
  echo "(Add the '--no-op' option to see what would be removed by"
  echo "this command)"
  echo
  echo "For detailed upgrade instructions please see:"
  echo "https://docs.chef.io/upgrade_server.html"
fi

exit 0
