#!/bin/sh
#
# Perform necessary removal steps
# after package is uninstalled.
#

is_darwin()
{
  uname -a | grep "^Darwin" 2>&1 >/dev/null
}

cleanup_symlinks() {
  # Keep removed symlinks in this list, so that removal of upgraded packages still cleans up
  # leftovers from older versions. We keep the push jobs values here to cleanup old releases
  workstation_binaries="berks chef chef-cli cinc-apply cinc-shell cinc-solo cinc-run chef-vault cookstyle dco delivery foodcritic cinc-auditor kitchen knife ohai push-apply pushy-client pushy-service-manager cinc-client chef-apply chef-client chef-shell chef-solo inspec mixlib-install cinc-zero fauxhai"
  binaries="chef-run $workstation_binaries chef-analyze hab"

  for binary in $binaries; do
    rm -f "$PREFIX/bin/$binary"
  done
}

if is_darwin; then
    PREFIX="/usr/local"
    mkdir -p "$PREFIX/bin"
else
    PREFIX="/usr"
fi

# Clean up binary symlinks if they exist
# see: http://tickets.opscode.com/browse/CHEF-3022
if [ ! -f /etc/redhat-release -a ! -f /etc/fedora-release -a ! -f /etc/system-release ]; then
  # not a redhat-ish RPM-based system
  cleanup_symlinks
elif [ "x$1" = "x0" ]; then
  # RPM-based system and we're uninstalling rather than upgrading
  cleanup_symlinks
fi

exit 0
