#!/bin/sh

#
# Copyright:: Copyright (c) 2020 Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Perform necessary setup steps
# after package is installed.
#

PROGNAME=`basename $0`
INSTALLER_DIR=/opt/cinc-workstation
SAVED_LINKS=/var/lib/cinc-workstation/sibling-links

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

# A live link from cinc-client or cinc-auditor stays theirs, except inspec,
# which stays Workstation's so it runs the same InSpec as cinc-auditor.
owned_by_sibling()
{
  [ -L "$PREFIX/bin/$1" ] && [ -e "$PREFIX/bin/$1" ] || return 1
  case "$1:`readlink "$PREFIX/bin/$1"`" in
    inspec:/opt/cinc/*) return 1 ;;
    *:/opt/cinc/*|*:/opt/cinc-auditor/*) return 0 ;;
  esac
  return 1
}

link_binary()
{
  if owned_by_sibling "$1"; then
    echo "Leaving $PREFIX/bin/$1 (`readlink "$PREFIX/bin/$1"`); Workstation's is $INSTALLER_DIR/bin/$1"
    return 0
  fi
  ln -sf "$INSTALLER_DIR/bin/$1" "$PREFIX/bin/$1" || error_exit "Cannot link $1 to $PREFIX/bin"
}

# preinst saved the sibling links an older postrm deletes during a dpkg upgrade;
# restore only entries of the form $PREFIX/bin/<name> -> /opt/cinc*/bin/<name>.
if [ -f "$SAVED_LINKS" ]; then
  while read -r link target; do
    name=${link#"$PREFIX"/bin/}
    case "$name" in ""|*/*|*..*) continue ;; esac
    case "$target" in
      /opt/cinc/bin/*/*|/opt/cinc-auditor/bin/*/*|*..*) continue ;;
      /opt/cinc/bin/?*|/opt/cinc-auditor/bin/?*) ;;
      *) continue ;;
    esac
    [ -e "$link" ] || [ -L "$link" ] || [ ! -e "$target" ] || ln -s "$target" "$link"
  done < "$SAVED_LINKS"
  rm -f "$SAVED_LINKS"
  rmdir "`dirname "$SAVED_LINKS"`" 2>/dev/null
fi

# NOTE: If you add to this list, also add it to scripts/run-cli-smoke-tests.ps1
# (bio and chef*/inspec* are intentionally excluded there).
binaries="berks cinc cinc-cli cinc-ng cinc-analyze cinc-apply cinc-shell cinc-solo chef-vault cookstyle cinc-auditor kitchen knife ohai cinc-client bio chef-client chef-shell chef-solo inspec cinc-zero mixlib-install fauxhai taste-tester"
for binary in $binaries; do
  link_binary "$binary"
done

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

if is_darwin; then
  ln -sf $INSTALLER_DIR/bin/uninstall_chef_workstation $PREFIX/bin || error_exit "Cannot link uninstall_chef_workstation to $PREFIX/bin"
fi

echo ""
echo "Thank you for installing Cinc Workstation!"
echo "You can find some tips on getting started at https://cinc.sh/"
echo ""
exit 0
