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

INSTALLER_DIR=/opt/cinc-auditor

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

if is_darwin; then
    PREFIX="/usr/local"
else
    PREFIX="/usr"
fi

cleanup_symlinks() {
  binaries="cinc-auditor"
  for binary in $binaries; do
    rm -f $PREFIX/bin/$binary
  done

  wrapper_links="inspec"
  link_target="cinc-auditor-wrapper"
  for link in $wrapper_links; do
    if [ -L $PREFIX/bin/$link -a "$(readlink $PREFIX/bin/$link)" = "$INSTALLER_DIR/bin/$link_target" ]; then
      echo "Removing inspec compatibility symlink..."
      rm -f $PREFIX/bin/$link
    fi
  done
}

# 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 deinstalling rather than upgrading
  cleanup_symlinks
fi

echo "Cinc Auditor has been uninstalled!"

exit 0
