#!/bin/sh
#
# Perform necessary inspec setup steps
# after package is installed.
#

PROGNAME=`basename $0`
INSTALLER_DIR=/opt/cinc-auditor
CONFIG_DIR=/etc/cinc-auditor
USAGE="usage: $0"

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

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

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

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

for binary in $binaries; do
  ln -sf $INSTALLER_DIR/bin/$binary $PREFIX/bin || error_exit "Cannot link $binary to $PREFIX/bin"
done

wrapper_links="inspec"
link_target="cinc-auditor-wrapper"
for link in $wrapper_links; do
  if [ ! -e $PREFIX/bin/$link ]; then
    echo "Symlinking inspec command to cinc-auditor for compatibility..."
    ln -sf $INSTALLER_DIR/bin/$link_target $PREFIX/bin/$link || error_exit "Cannot link $link_target to $PREFIX/bin/$link"
  fi
done

# 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 $INSTALLER_DIR

echo "Thank you for installing Cinc Auditor!"

exit 0
