#!/usr/bin/env bash

source "$rvm_scripts_path/functions/pkg"

requirements_rvm_pkg_wait_key()
{
  rvm_is_a_shell_function no_warning ||
  {
    rvm_error "
You are using 'rvm autolibs rvm_pkg', this does not know how to install some of the packages, make sure to install those first.
"
    return 1
  }
  rvm_log "press any key to continue"
  typeset _read_char_flag anykey
  [[ -n "${ZSH_VERSION:-}" ]] && _read_char_flag=k || _read_char_flag=n
  builtin read -${_read_char_flag} 1 -s -r anykey
}

requirements_rvm_pkg_install_custom()
{
  if
    (( $# > 0 ))
  then
    rvm_warn "Before continuing install: $*"
    requirements_rvm_pkg_wait_key || return $?
  fi
}

requirements_rvm_pkg_before()
{
  rvm_warn "
Warning, you are using 'rvm autolibs rvm_pkg', this is rarely used and can produce errors,
make sure to report any problems to https://github.com/wayneeseguin/rvm/issues
"
}

requirements_rvm_pkg_update_system()
{
  rvm_warn "Always update your system first!"
}

requirements_rvm_pkg_define()
{
  case "$1" in
    (rvm)
      requirements_check_custom bash curl patch
      ;;
    (jruby*)
      if
        is_head_or_disable_binary "$1"
      then
        requirements_check_custom git
        if is_jruby_post17 "$1"
        then requirements_check_custom mvn=maven
        else requirements_check_custom ant
        fi
      fi
      requirements_check_custom java
      ;;
    (ir*)
      requirements_check_custom mono
      ;;
    (opal)
      requirements_check_custom node
      ;;
    (yaml|zlib|openssl|readline|xml2|xslt)
      true # just skip the packages
      ;;
    (*-head)
      requirements_check_custom git
      requirements_rvm_pkg_define "${1%-head}"
      ;;
    (*)
      requirements_check_custom gcc g++ libtool bison bzip2
      requirements_check yaml zlib openssl readline
      ;;
  esac
}

requirements_rvm_pkg_after()
{
  requirements_rvm_pkg_configure yaml zlib openssl readline
}
