#!/bin/bash
# 2006-2008 (c) Viva64.com Team
# 2008-2020 (c) OOO "Program Verification Systems"
# 2020-2026 (c) PVS-Studio LLC
#

set -Eeuo pipefail

BASEDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
PREFIX=${1:-}
DOC="https://pvs-studio.com/en/docs/manual/0039/"
if [ -z "$PREFIX" ]; then
  case "$OSTYPE" in
    darwin*)  PREFIX="/usr/local" 
              DOC="https://pvs-studio.com/en/docs/manual/0042/"
              ;;
    *)        PREFIX="/usr" ;;
  esac
fi

function do_install
{
  dst=$1
  for dir in ${@:2}
  do
    cp -av "$dir" "$dst"
  done
}

mkdir -p "$PREFIX"
do_install "$PREFIX" "$BASEDIR/share"
rm -rfd "$PREFIX/share/pvs-studio-dotnet/BuildTools/SdkResolvers/Microsoft.DotNet.MSBuildSdkResolver"

mkdir -p "$PREFIX/bin"
ln -sf "$PREFIX/share/pvs-studio-dotnet/pvs-studio-dotnet" "$PREFIX/bin/pvs-studio-dotnet"

if ! [ -x "$PREFIX/bin/pvs-studio" ] && ! command -v pvs-studio &>/dev/null; then
  echo "The PVS-Studio C++ analyzer core was not found and it is required for using PVS-Studio C# analyzer. Please install it according to the instructions at $DOC."
fi