#!/bin/bash
# To run this script: curl -sL https://get.diversion.dev | bash || export PATH=$HOME/.diversion/bin:$PATH
CURPATH=$(pwd)
# shellcheck disable=SC2016
DV_DIR='$HOME/.diversion/bin'
DV_DIR_VAL=$(eval echo $DV_DIR)
mkdir -p "$DV_DIR_VAL"
cd "$DV_DIR_VAL" || { echo "$DV_DIR_VAL dir not found"; exit 2; }

OS=$(uname -s| tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m| tr '[:upper:]' '[:lower:]')

# Force x86_64 architecture for Linux systems
if [ "$OS" = "linux" ]; then
    ARCH="x86_64"
fi

DV_BIN="dv"
# just for cleanup purposes
rm -f "$DV_BIN"
sync

echo "Downloading agent binaries..."
curl -#L https://dv-binaries.s3.us-east-2.amazonaws.com/"${OS}_${ARCH}/${DV_BIN}" --output "$DV_BIN"
chmod a+x ./*

if [[ "$OSTYPE" == "darwin"* ]]; then
  echo "Setting up $DV_BIN agent to run at startup"
  launchctl remove "diversion.$DV_BIN.agent" || true
  launchctl submit -l "diversion.$DV_BIN.agent" -- "$DV_DIR_VAL/$DV_BIN" --agent
fi


if echo "$PATH" | grep -v --quiet "$DV_DIR_VAL" --; then
    # Updating PATH

    if [[ -d $ZDOTDIR ]]; then
        ZSHRC_PATH="$ZDOTDIR/.zshrc"
        echo "Updating Zsh configs at $ZSHRC_PATH"
    else
        ZSHRC_PATH="$HOME/.zshrc"
    fi

    # shellcheck disable=SC2016
    PATH_STMT='export PATH="'$DV_DIR':$PATH"'
    FILES_TO_EDIT=("$HOME/.bashrc" "$ZSHRC_PATH" "$HOME/.profile" "$HOME/.zprofile" "$HOME/.bash_profile")

    success=0
    for F in "${FILES_TO_EDIT[@]}"
    do
    if test -f "$F"; then
        success=1
        if grep -Fxq "$PATH_STMT" "$F"
        then
            echo "$F up to date"
        else
            echo "Adding dv to PATH in $F"
            echo "$PATH_STMT" >> "$F"
        fi
    fi
    done
    if ((success == 0)); then
        echo "Could not find any profile file for updating the path"
        if [[ "$SHELL" == *"zsh"* ]]; then
            echo "Shell appears to be zsh, will add path in .zshrc"
            echo "$PATH_STMT" >> "$ZSHRC_PATH"
        else
            echo "Shell appears to be bash, will add path in .bashrc"
            echo "$PATH_STMT" >> "$HOME/.bashrc"
        fi
    fi
    export PATH="'$DV_DIR':$PATH"
fi

DV_DIR_TEMP='$HOME/.diversion/tmp'
DV_DIR_TEMP_VAL=$(eval echo $DV_DIR_TEMP)
# Currently open the install only for mac os
if [ $OS == "darwin" ]; then
    ls "/Applications/" | grep "Diversion Desktop" | grep -v --quiet "Diversion Desktop Test" --
    HAS_DIVERSION_PROD=$?
    ls "/Applications/" | grep --quiet "Diversion Desktop Test" --
    HAS_DIVERSION_TEST=$?
    if [[ ($HAS_DIVERSION_PROD == 0  &&  $DV_BIN == "dv") || ($HAS_DIVERSION_TEST == 0 && $DV_BIN == "dvt") ]]; then
      echo "Desktop application already installed"
    else
      echo "Downloading desktop application binaries..."
      DV_APP_INSTALLER_NAME='diversion.dmg'
      mkdir -p "$DV_DIR_TEMP_VAL"
      cd "$DV_DIR_TEMP_VAL" ||  { echo "$DV_DIR_TEMP_VAL dir not found"; exit 2; }
      if [ $DV_BIN == "dv" ]; then
        curl -#L https://get.diversion.dev/desktop/mac"/${ARCH}" --output "${DV_APP_INSTALLER_NAME}"
      else
        curl -#L https://get.diversion.dev/desktop/test/mac"/${ARCH}" --output "${DV_APP_INSTALLER_NAME}"
      fi
      open "${DV_APP_INSTALLER_NAME}"
    fi
fi


cd "$CURPATH" || { echo "Working directory not found"; exit 3; }

echo "Done"
