#!/bin/bash
set -e

function clone_repo() {
  GREEN='\033[0;32m'

  REPOSITORY_URL=$1
  REPOSITORY_PATH=$2
  if [[ ! -d "$REPOSITORY_PATH/.git" ]]; then
    echo -e "\n${GREEN}➡️ Cloning $REPOSITORY_URL...\n${NC}"
    git clone "$REPOSITORY_URL" "$REPOSITORY_PATH"
  fi
}

repo_root="$(git rev-parse --show-toplevel)"
root="$(dirname "$repo_root")"

# Copy working package-lock.json to workspace
echo "Copy lock file to workspace..."
cp "$repo_root"/script/workspace/package-lock.json "$root"

# Clone dependent repos
echo "Cloning dependent repos..."
clone_repo https://github.com/actions/languageservices "$root"/languageservices

# Copy workspace files
echo "Copying workspace files..."
cp "$repo_root"/script/workspace/package.json "$root"
cp "$repo_root"/script/workspace/vscode-github-actions.code-workspace "$root"
cp "$repo_root"/script/workspace/update-package-locks.sh "$root"
cp "$repo_root"/script/workspace/watch.sh "$root"
