#!/usr/bin/env bash

# SCRIPT VERSION
USER_SETUP_VER=2
USER_SETUP_VER_FILE="${XDG_DATA_HOME:-$HOME/.local/share}/ublue/vscode-configured"
USER_SETUP_VER_RAN=$(cat "$USER_SETUP_VER_FILE")

mkdir -p "$(dirname "$USER_SETUP_VER_FILE")" || exit 1

USER_SETUP_VER_FILE_V1="$HOME/.aurora-vscode-configured"
# Upgrade from v1 to v2 changes only the location of the USER_SETUP_VER.
# Exit if v1 file is present.
if [[ -f $USER_SETUP_VER_FILE_V1 ]]; then
  echo "User setup v1 has already run. Exiting..."
  rm $USER_SETUP_VER_FILE_V1
  echo "$USER_SETUP_VER" > "$USER_SETUP_VER_FILE"
  exit 0
fi

# Run script if updated
if [[ -f $USER_SETUP_VER_FILE && "$USER_SETUP_VER" = "$USER_SETUP_VER_RAN" ]]; then
  echo "User setup v$VER has already run. Exiting..."
  exit 0
fi

# Setup VSCode
# Pre-install preferred VSCode Extensions
code --install-extension ms-vscode-remote.remote-containers
code --install-extension ms-vscode-remote.remote-ssh
code --install-extension ms-azuretools.vscode-containers

# Prevent future executions
echo "Writing state file"
echo "$USER_SETUP_VER" > "$USER_SETUP_VER_FILE"
