#!/usr/bin/env bash

: <<'DOC'
Channel plugin for installer scripts sourced from `plugins/tools`.

Requirements:
  installer_path    path to the installer script
  uninstaller_path  path to the uninstaller script
DOC

function __script_channel__install() {
  local tool="$1"
  local requirements="$2"
  local script
  script=$(jq_get "$requirements" '.installer_path')

  export SHELLOPS_TOOL_NAME="$tool"
  export SHELLOPS_TOOL_REQUIREMENTS="$requirements"
  # shellcheck disable=SC1090
  source "$script"
  unset SHELLOPS_TOOL_NAME
  unset SHELLOPS_TOOL_REQUIREMENTS
}

function __script_channel__uninstall() {
  # shellcheck disable=SC2034
  local tool="$1"
  local requirements="$2"
  local script
  script=$(jq_get "$requirements" '.uninstaller_path')

  export SHELLOPS_TOOL_NAME="$tool"
  export SHELLOPS_TOOL_REQUIREMENTS="$requirements"
  # shellcheck disable=SC1090
  source "$script"
  unset SHELLOPS_TOOL_NAME
  unset SHELLOPS_TOOL_REQUIREMENTS
}

register_channel \
  --name "script" \
  --name "local" \
  --installer __script_channel__install \
  --uninstaller __script_channel__uninstall
