#!/bin/bash

PACKAGE_NAME='newrelic-dotnet-agent'
OBSOLETE_PACKAGE_NAME='newrelic-netcore20-agent'
NEWRELIC_HOME=/usr/local/${PACKAGE_NAME}
OBSOLETE_NEWRELIC_HOME=/usr/local/${OBSOLETE_PACKAGE_NAME}

# create logs dir
mkdir -p $NEWRELIC_HOME/logs 2> /dev/null

# create symlink to logs dir in /var/log/newrelic
mkdir -p /var/log/newrelic 2> /dev/null
ln -sTf $NEWRELIC_HOME/logs /var/log/newrelic/dotnet 2> /dev/null

# remove old profile.d file if it exists
oldHomeDirFile="/etc/profile.d/${OBSOLETE_PACKAGE_NAME}-path.sh"
if [ -e $oldHomeDirFile ]; then
  echo "Cleaning up $oldHomeDirFile"
  rm -f $oldHomeDirFile
fi

# migrate data from obsoleted package, if applicable
if [ -d $OBSOLETE_NEWRELIC_HOME ]; then

  # migrate config file, backing up original first
  if [ -e $OBSOLETE_NEWRELIC_HOME/newrelic.config ]; then
    echo "Migrating newrelic.config from $OBSOLETE_NEWRELIC_HOME"
    # Move the existing config file in the new package directory out of the way
    mv $NEWRELIC_HOME/newrelic.config $NEWRELIC_HOME/newrelic.config.original
    # Copy the config file from the old package directory to the new package directory
    cp -v $OBSOLETE_NEWRELIC_HOME/newrelic.config $NEWRELIC_HOME/newrelic.config
    # Rename the config file in the old package directory so it won't get migrated again
    mv $OBSOLETE_NEWRELIC_HOME/newrelic.config $OBSOLETE_NEWRELIC_HOME/newrelic.config.migrated
  fi

  # migrate any custom instrumentation
  if [ -d $OBSOLETE_NEWRELIC_HOME/extensions ]; then
    # This is safe to run multiple times because of the -n option, which means "don't overwrite an existing file"
    # This also means that only custom instrumentation XML files (not our default auto-instrumentation ones) will be migrated the first time
    cp -nv $OBSOLETE_NEWRELIC_HOME/extensions/*.xml $NEWRELIC_HOME/extensions
  fi
fi

# Deprecated instrumentation files to remove post install
rm -f $NEWRELIC_HOME/extensions/NewRelic.Providers.Wrapper.Logging.Instrumentation.xml 2> /dev/null
rm -f $NEWRELIC_HOME/extensions/NewRelic.Providers.Wrapper.Logging.dll 2> /dev/null

echo "export CORECLR_NEWRELIC_HOME=${NEWRELIC_HOME}" > /etc/profile.d/${PACKAGE_NAME}-path.sh
source /etc/profile.d/${PACKAGE_NAME}-path.sh

chmod o+w $NEWRELIC_HOME/logs
chmod +x $NEWRELIC_HOME/*.sh 2> /dev/null

printf "Initialize the New Relic .NET Agent environment variables by running:\n"
printf "\t\033[1msource /etc/profile.d/${PACKAGE_NAME}-path.sh\033[0m\n"
printf "\t\033[1msource $CORECLR_NEWRELIC_HOME/setenv.sh\033[0m\n"
