#!/usr/bin/env bash

# omarchy:summary=Install the OVHcloud CLI, ovhai and the OpenStack clients
# omarchy:requires-sudo=true

# Based: ovhcloud through asdf, from the plugin Based maintains; ovhai from
# OVH's own installer, which wants sha256sum (macOS spells it shasum -a 256),
# appends to ~/.zshrc and drops the binary in ~/bin, so the shell file is put
# back as it was and the binary moves to ~/.local/bin; and the OpenStack
# clients OVH's public cloud speaks.

set -eo pipefail

asdf_data=${ASDF_DATA_DIR:-$HOME/.asdf}
omarchy-pkg-add asdf
export PATH="$asdf_data/shims:$PATH"

echo -e "Installing the OVHcloud CLI...\n"
omarchy-dev-env-tool ovhcloud

echo -e "\nInstalling ovhai...\n"
if ! omarchy-cmd-present ovhai; then
  tmp=$(mktemp -d)
  trap 'rm -rf "$tmp"' EXIT
  if ! omarchy-cmd-present sha256sum; then
    printf '#!/bin/sh\nexec shasum -a 256 "$@"\n' >"$tmp/sha256sum"
    chmod +x "$tmp/sha256sum"
  fi
  [[ -f ~/.zshrc ]] && cp ~/.zshrc "$tmp/zshrc"
  PATH="$tmp:$PATH" bash -c "$(curl -fsSL https://cli.gra.ai.cloud.ovh.net/install.sh)"
  [[ -f $tmp/zshrc ]] && mv "$tmp/zshrc" ~/.zshrc
  mkdir -p ~/.local/bin
  [[ -f ~/bin/ovhai ]] && mv ~/bin/ovhai ~/.local/bin/ovhai
  rmdir ~/bin 2>/dev/null || true
fi

echo -e "\nInstalling the OpenStack clients...\n"
omarchy-pkg-add python3-openstackclient brew=openstackclient python3-barbicanclient linux=only \
  python3-cinderclient linux=only python3-designateclient linux=only python3-glanceclient linux=only \
  python3-heatclient linux=only python3-magnumclient linux=only python3-neutronclient linux=only \
  python3-octaviaclient linux=only python3-swiftclient linux=only
