#!/usr/bin/env bash

__rvm_record_install()
{
  [[ -n "$1" ]] || return

  typeset recorded_ruby_name rvm_install_record_file
  recorded_ruby_name="$( "$rvm_scripts_path/tools" strings "$1" )"
  rvm_install_record_file="$rvm_user_path/installs"

  [[ -f "$rvm_install_record_file" ]] || \touch "$rvm_install_record_file"
  __rvm_sed_i "$rvm_install_record_file" -e "/^$recorded_ruby_name/d"

  #TODO: use `for` so  rvm_configure_flags are quoted properly
  printf "%b" "$recorded_ruby_name -- ${rvm_configure_flags[*]}\n" >> "$rvm_install_record_file"
}

__rvm_remove_install_record()
{
  typeset recorded_ruby_name rvm_install_record_file
  recorded_ruby_name="$( "$rvm_scripts_path/tools" strings "$1" )"
  rvm_install_record_file="$rvm_user_path/installs"

  if [[ -s "$rvm_install_record_file" ]]
  then __rvm_sed_i "$rvm_install_record_file" -e "/^$recorded_ruby_name/d"
  fi
}

__rvm_recorded_install_command()
{
  typeset recorded_ruby_name
  recorded_ruby_name="$( "$rvm_scripts_path/tools" strings "$1" )"
  recorded_ruby_name=${recorded_ruby_name%%${rvm_gemset_seperator:-"@"}*}

  [[ -n "$recorded_ruby_name" ]] || return 1

  if
    [[ -s "$rvm_user_path/installs" ]] &&
    __rvm_grep "^$recorded_ruby_name " "$rvm_user_path/installs" >/dev/null 2>&1
  then
    __rvm_grep "^$recorded_ruby_name " "$rvm_user_path/installs" | head -n 1
  else
    return 1
  fi
}
