#!/usr/bin/env sh
#
# fzf-xcode - xcode version switcher

command -v fzf >/dev/null || exit 1
command -v xcode-select >/dev/null || exit 1

main() {
  current=$(xcode-select --print-path)
  version=$(find /Applications -maxdepth 1 -iname 'Xcode*.app' | fzf \
    +m \
    --cycle \
    --exit-0 \
    --height=25% --no-hscroll --no-mouse --no-multi \
    --nth=1 \
    --prompt="${current}> ") \
  && sudo xcode-select --switch "${version}" \
  && printf 'Now using %s' "${version}"
}

is-mac && main
