#!/usr/bin/env bash

install_setup()
{
  set -o errtrace

  export HOME="${HOME%%+(\/)}" # Remove trailing slashes if they exist on HOME

  case "$(uname -s)" in
    AIX) name_opt=-name  ;;
    *)   name_opt=-iname ;;
  esac

  if (( ${rvm_ignore_rvmrc:=0} == 0 ))
  then
    for file in /etc/rvmrc "$HOME/.rvmrc"
    do
      if [[ -s "$file" ]]
      then
        . $file
      fi
    done
  fi

  export PS4="+ \${BASH_SOURCE##\${rvm_path:-}} : \${FUNCNAME[0]:+\${FUNCNAME[0]}()}  \${LINENO} > "

  # TODO: Figure out a much better name for 'rvm_user_install'
  if (( UID == 0 )) || [[ "${rvm_prefix:-}" != "${HOME}" ]]
  then
    export rvm_user_install_flag=0
  else
    export rvm_user_install_flag=1
  fi

  unset rvm_auto_flag
}


install_usage()
{
  printf "
  Usage:

    ${0} [options]

  options:

    --auto    : Automatically update shell profile files.

    --prefix  : Installation prefix directory.

    --help    : Display help/usage (this) message

    --version : display rvm package version

  \n"
}

check_rubyopt_conditions()
{
  if [[ -n "${RUBYOPT:-""}" ]]; then
    printf "

WARNING: You have RUBYOPT set in your current environment.
This may cause rubies to not work as you expect them to as it is not supported
by all of them If errors show up, please try unsetting RUBYOPT first.

"
  fi
}

andand_return_instructions()
{
  printf "
  This means that if you see something like:

    '[ -z \"\$PS1\" ] && return'

  then you change this line to:

  if [[ -n \"\$PS1\" ]] ; then

    # ... original content that was below the '&& return' line ...

  fi # <= be sure to close the if at the end of the .bashrc.

  # This is a good place to source rvm v v v
  [[ -s \"$rvm_path/scripts/rvm\" ]] && source \"$rvm_path/scripts/rvm\"  # This loads RVM into a shell session.

EOF - This marks the end of the .bashrc file
"
}

installation_complete()
{
  printf "
  You must now complete the install by loading RVM in new shells.

  1) Place the folowing line at the end of your shell's loading files
     (.bashrc or .bash_profile for bash and .zshrc for zsh),
     after all PATH/variable settings:

     [[ -s \"$rvm_path/scripts/rvm\" ]] && source \"$rvm_path/scripts/rvm\"  # This loads RVM into a shell session.

     You only need to add this line the first time you install rvm.

  2) Ensure that there is no 'return' from inside the ~/.bashrc file,
     otherwise rvm may be prevented from working properly.

     $(andand_return_instructions)

     Be absolutely *sure* to REMOVE the '&& return'.

     If you wish to DRY up your config you can 'source ~/.bashrc' at the bottom of your .bash_profile.

     Placing all non-interactive (non login) items in the .bashrc,
     including the 'source' line above and any environment settings.

  3) CLOSE THIS SHELL and open a new one in order to use rvm.
  \n"
}

andand_return_warning()
{
  printf "
  WARNING:  you have a 'return' statement in your ~/.bashrc
              This could cause some features of RVM to not work.

  $(andand_return_instructions)

  Even if you are using zsh you should still adjust the ~/.bashrc
  If you have any questions about this please visit
    #rvm on irc.freenode.net.
  \n"
}

display_thank_you()
{
  printf "
${name:-"${USER:-$(id | sed -e 's/^[^(]*(//' -e 's/).*$//')}"},

Thank you very much for using RVM! I sincerely hope that RVM helps to
make your work both easier and more enjoyable.

If you have any questions, issues and/or ideas for improvement please
join#rvm on irc.freenode.net and let me know, note you must register
(http://bit.ly/5mGjlm) and identify (/msg nickserv <nick> <pass>) to
talk, this prevents spambots from ruining our day.

My irc nickname is 'wayneeseguin' and I hang out in #rvm typically

  ~09:00-17:00EDT and again from ~21:00EDT-~23:00EDT

If I do not respond right away, please hang around after asking your
question, I will respond as soon as I am back.  It is best to talk in
#rvm itself as then other users can help out should I be offline.

Be sure to get head often as rvm development happens fast,
you can do this by running 'rvm get head' followed by 'rvm reload'
or opening a new shell

  w⦿‿⦿t

    ~ Wayne

"
}

upgrade_notes()
{
  printf "

Upgrade Notes

  * rvm_trust_rvmrcs has been changed to rvm_trust_rvmrcs_flag for consistency

  * Ruby package dependency list for your OS is given by:
    rvm notes

  * If you encounter any issues with a ruby 'X' your best bet is to:
    rvm remove X ; rvm install X

  * If you wish to have the 'pretty colors' again, set in ~/.rvmrc:
    export rvm_pretty_print_flag=1

  * If you see the following error message: Unknown alias name: 'default'
    re-set your default ruby, this is due to a change in how default works.
"
}

determine_install_path()
{
  export HOME="${HOME%%+(\/)}" # Remove trailing slashes if they exist on HOME

  if (( ${rvm_ignore_rvmrc:=0} == 0 ))
  then
    for rvmrc in /etc/rvmrc "$HOME/.rvmrc"
    do
      if [[ -f "$rvmrc" ]]
      then
        if \grep '^\s*rvm .*$' "$rvmrc" >/dev/null 2>&1
        then
          printf "\nError: $rvmrc is for rvm settings only.\nrvm CLI may NOT be called from within $rvmrc. \nSkipping the loading of $rvmrc"
          return 1
        else
          source "$rvmrc"
        fi
      fi
    done
  fi

  if [[ -z "${rvm_path:-}" ]]
  then
    # Set the default sandboxed value.
    # TODO: Alter the variable names to make sense
    if [[ -z "${rvm_user_install_flag:-}" ]]
    then
      if (( UID == 0 )) ||
        [[ -n "${rvm_prefix:-}" && "${rvm_prefix:-}" != "${HOME}" ]]
      then
        rvm_user_install_flag=0
      else
        rvm_user_install_flag=1
      fi
    fi

    if [[ -z "${rvm_prefix:-}" ]]
    then
      if (( ${rvm_user_install_flag:=0} == 0 ))
      then
        rvm_prefix="/usr/local"
      elif [[ -n "$HOME" ]]
      then
        rvm_prefix="$HOME"
      else
        echo "No \$rvm_prefix was provided and "
        echo "$(id | \sed -e's/^[^(]*(//' -e 's/).*//') has no \$HOME defined."
        echo "Halting loading of RVM."
        rvm_load_rvm=0
      fi
    fi

    true "${rvm_prefix/rvm/scripts}" # Fix rvm_prefix changes, older installs.

    if [[ -z "${rvm_path:-}" ]]
    then
      if [[ "$rvm_prefix" = "$HOME" ]]
      then
        rvm_path="${rvm_prefix}/.rvm"
      else
        rvm_path="${rvm_prefix}/rvm"
      fi
    fi

    export rvm_path="${rvm_path%%+(\/)}"
  fi
}

determine_install_or_upgrade()
{ # TODO: Improve this...
  upgrade_flag=0
  if grep 'scripts/rvm' "${HOME}"/.bash* >/dev/null 2>&1 ||
    grep 'scripts/rvm' "${HOME}"/.zsh* >/dev/null 2>&1
then
  if [[ -d "$rvm_path" && -s "${rvm_path}/scripts/rvm" ]]
  then
    upgrade_flag=1
  fi
fi
export upgrade_flag
}

print_install_header()
{
  printf "
  RVM:  Shell scripts enabling management of multiple ruby environments.
  RTFM: https://rvm.beginrescueend.com/
  HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)
  "

  if [[ ${upgrade_flag:-0} -eq 1 ]] ;then

    printf "\nUpgrading the RVM installation in $rvm_path/"

  else

    printf "\nInstalling RVM to $rvm_path/"

  fi
}

configure_installation()
{
  install_source_path="$(dirname "$0" | sed 's#\/scripts$##')"

  if [[ -d "$install_source_path/scripts" \
    && -s "$install_source_path/scripts/functions/utility" ]] ; then
  builtin cd "$install_source_path"
fi

# Save scripts path
scripts_path=${rvm_scripts_path:-""}
rvm_scripts_path="${PWD%%+(\/)}/scripts"
# Load scripts.

source scripts/initialize
source scripts/functions/init
source scripts/version

# What does this do that scripts/initialize not do?:
__rvm_initialize

# Restore Scripts Path
rvm_scripts_path=${scripts_path:-"$rvm_path/scripts"}
#
item="* "
question="\n<?>"
cwd="${PWD%%+(\/)}"

true "${source_path:=$cwd}"

return 0
}

create_install_paths()
{
  install_paths=( archives src log bin gems man rubies config user tmp gems environments wrappers)
  for install_path in "${install_paths[@]}" ; do
    [[ -d "$rvm_path/$install_path" ]] || mkdir -p "$rvm_path/$install_path"
  done
  if [[ "$rvm_bin_path" != "" ]]; then
    [[ -d "$rvm_bin_path" ]] || mkdir -p "$rvm_bin_path"
  fi
  return 0
}

cleanse_old_entities()
{
  #
  # Remove old files that no longer exist.
  #
  for script in utility array ; do
    [[ -f "$rvm_path/scripts/${script}" ]] &&
      rm -f "$rvm_path/scripts/${script}"
  done
  return 0
}

install_rvm_files()
{
  files=(README LICENCE VERSION)
  for file in "${files[@]}"; do
    cp -f "$source_path/${file}" "$rvm_path/${file}"
  done

  directories=(config contrib scripts examples lib hooks help patches)

  for directory in ${directories[@]} ; do

    for entry in $(find $directory 2>/dev/null); do

      if [[ -f "$source_path/$entry" ]] ; then

        # Target is supposed to be a file, remove if it is a directory.
        if [[ -d "$rvm_path/$entry" ]]; then __rvm_rm_rf "$rvm_path/$entry" ; fi

        cp -f "$source_path/$entry" "$rvm_path/$entry"

      elif [[ -d "$source_path/$entry" ]] ; then

        # Target is supposed to be a directory, remove if it is a file.
        if [[ -f "$rvm_path/$entry" ]]; then rm -f "$rvm_path/$entry" ; fi

        if [[ ! -d "$rvm_path/$entry" ]] ; then mkdir -p "$rvm_path/$entry" ; fi

      fi

    done

  done

  return 0
}

setup_configuration_files()
{
  (
  cd "$rvm_path"

  [[ -f config/user ]] && mv config/user user/db

  if [[ ! -s user/db ]] ; then
    echo '# User settings, overrides db settings and persists across installs.' \
      >> user/db
  fi

  if [[ -s config/rvmrcs ]] ; then
    mv config/rvmrcs user/rvmrcs
  else
    if [[ ! -f user/rvmrcs ]] ; then
      touch user/rvmrcs
    fi
  fi
  )
}

ensure_scripts_are_executable()
{
  scripts=(monitor match log install color db fetch log set package)

  for script_name in "${scripts[@]}" ; do

    chmod +x "$rvm_scripts_path/$script_name" 2>/dev/null # Hide errors.

  done
  return 0
}

install_binscripts()
{
  files=(rvm-prompt rvm rvmsudo rvm-shell rvm-exec rvm-auto-ruby)
  for file in "${files[@]}" ; do
    # Ensure binscripts are always available in rvm_path/bin first.
    rm -f "${rvm_path}/bin/${file}"

    cp -f "${source_path}/binscripts/${file}" "${rvm_path}/bin/${file}"
    chmod +x "${rvm_path}/bin/${file}"

    if [[ "${rvm_bin_path}" != "${rvm_path}/bin" ]] ; then
      cp -f "${source_path}/binscripts/${file}" "${rvm_bin_path}/${file}"
    fi

  done
  return 0
}

automatic_profile_setup()
{
  if [[ ${rvm_auto_flag:-0} -eq 1 ]] ; then

    printf "Checking rc files... ($rvm_rc_files)"

    if [[ ${rvm_loaded_flag:-0} -ne 1 ]] ; then

      for rcfile in $(printf $rvm_rc_files) ; do

        [[ -f $rcfile ]] || touch "$rcfile"

        if [[ -s "${HOME}/.profile" ]] ; then

          if ! grep '.profile' "$rcfile" >/dev/null 2>&1 ; then

            echo "    Adding profile sourcing line to $rcfile."

            printf "
  # rvm install added line:
  [[ -s \"$rvm_path/.profile\" ]] && source \"$rvm_path/.profile\"
  " >> "$rcfile"

          fi

        fi

        if ! grep "scripts\/rvm" "$rcfile" >/dev/null 2>&1 ; then

          echo "    Adding rvm loading line to $rcfile."

          printf "
  # rvm install added:
  [[ -s \"$rvm_scripts_path/rvm\" ]] && . \"$rvm_scripts_path/rvm\"
  " >> "$rcfile"

        fi

      done
    fi
  fi
}

install_gemsets()
{
  if [[ -d gemsets/ ]] ; then

    if [[ ! -d "$rvm_path/gemsets" ]] ; then

      mkdir -p "$rvm_path/gemsets"

    fi

    for gemset_file in $(find "${PWD%%+(\/)}/gemsets" "${name_opt}" '*.gems' | sed 's/^\.\///') ; do

      cwd="${PWD//\//\\/}\/gemsets\/"

      destination="$rvm_path/gemsets/${gemset_file/$cwd}"

      destination_path="$(dirname "$destination")"

      if [[ ! -s "$destination" ]] ; then

        [[ ! -d "$destination_path" ]] && mkdir -p "$destination_path"

        cp "$gemset_file" "$destination"

      fi

    done

  fi
}

install_patchsets()
{
  if [[ -d patchsets/ ]] ; then

    [[ ! -d "$rvm_path/patchsets" ]] && mkdir -p "$rvm_path/patchsets"

    patchsets=($(builtin cd patchsets ; find \. "${name_opt}" '*' | sed 's/^\.\///'))

    for patchset_file in "${patchsets[@]}" ; do

      destination="$rvm_path/patchsets/$patchset_file"

      if [[ ! -s "$destination" ]] ; then

        destination_path="$(dirname "$destination")"

        [[ ! -d "$destination_path" ]] && mkdir -p "$destination_path"

        cp "patchsets/$patchset_file" "$destination"

      fi
    done

  fi
}

cleanse_old_environments()
{
  if [[ -d "$rvm_path/environments" ]] ; then
    # Remove BUNDLE_PATH from environment files
    environments=(
      $(find "$rvm_path/environments/" -maxdepth 1 -mindepth 1 -type f)
    )
    if [[ ${#environments[@]} -gt 0 ]] ; then
      for file in "${environments[@]}" ; do
        if grep 'BUNDLE_PATH' "$file" > /dev/null 2>&1; then
          grep -v 'BUNDLE_PATH' "$file" > "$file.new"
          mv "$file.new" "$file"
        fi
      done
    fi
  fi
}

migrate_old_gemsets()
{
  for gemset in "$rvm_path"/gems/*\%* ; do
    new_path=${gemset/\%/${rvm_gemset_separator:-"@"}}

    if [[ -d "$gemset" ]] && [[ ! -d "$new_path" ]] ; then
      printf "\n    Renaming $(basename "$gemset") to $(basename "$new_path") for new gemset separator."
      mv "$gemset" "$new_path"
    fi
  done

  for gemset in "$rvm_path"/gems/*\+* ; do
    new_path=${gemset/\+/${rvm_gemset_separator:-"@"}}

    if [[ -d "$gemset" && ! -d "$new_path" ]] ; then
      printf "\n    Renaming $(basename "$gemset") to $(basename "$new_path") for new gemset separator."
      mv $gemset $new_path
    fi
  done

  for gemset in "$rvm_path"/gems/*\@ ; do
    new_path=$(echo $gemset | sed -e 's#\@$##')

    if [[ -d "$gemset" && ! -d "$new_path" ]] ; then
      printf "\n    Fixing: $(basename "$gemset") to $(basename "$new_path") for new gemset separator."
      mv "$gemset" "$new_path"
    fi
  done
}

migrate_defaults()
{
  # Move from legacy defaults to the new, alias based system.
  if [[ -s "$rvm_config_path/default" ]]; then

    original_version="$(basename "$(grep GEM_HOME "$rvm_config_path/default" \
      | awk -F"'" '{print $2}' | sed "s#\%#${rvm_gemset_separator:-"@"}#")")"

    if [[ -n "$original_version" ]]; then

      "$rvm_scripts_path/alias" create default "$original_version" &> /dev/null

    fi ; unset original_version

    __rvm_rm_rf "$rvm_config_path/default"

  fi
}

correct_binary_permissions()
{
  declare -a files

  printf "\n    Correct permissions for base binaries in $rvm_bin_path..."

  mkdir -p "${rvm_bin_path}"

  files=(rvm rvmsudo rvm-shell rvm-auto-ruby)
  for file in "${files[@]}" ; do
    [[ -s "${rvm_bin_path}/$file" ]] &&
      chmod +x "${rvm_bin_path}/$file"
  done
}

install_man_pages()
{
  printf "\n    Copying manpages into place.\n"

  files=($(builtin cd "$install_source_path/man" ; \
         find . -maxdepth 2 -mindepth 1 -type f -print))

  for file in "${files[@]//.\/}" ; do

    [[ ! -d $rvm_man_path/${file%\/*} ]] &&
      mkdir -p $rvm_man_path/${file%\/*}

    cp -Rf "$install_source_path/man/$file" "$rvm_man_path/$file"

  done
}

cleanup_tmp_files()
{
  files=($( find "$rvm_path/" -mindepth 1 -maxdepth 2 "${name_opt}" '*.swp' -type f ))

  if [[ ${#files[@]} -gt 0 ]] ; then
    printf "\n    Cleanup any .swp files."
    for file in "${files[@]}" ; do
      [[ -f "$file" ]] && rm -f "$file"
    done
  fi
}

display_notes()
{
  if [[ $upgrade_flag -eq 0 ]] ; then
    chmod +x ./scripts/notes # Sometimes things don't clone properly :/
    ./scripts/notes
  fi

  if command -v git > /dev/null 2>&1 ; then
    name="$(git config user.name 2>/dev/null)"
  fi

  if [[ ${upgrade_flag:-0} -eq 1 ]] ; then

    upgrade_notes

    check_rubyopt_conditions

    printf "\nUpgrade of RVM in $rvm_path/ is complete.\n\n"

  else

    if (( ${rvm_user_install_flag:=0} == 1 ))
    then
      installation_complete
    fi

    if [[ -s "${HOME}/.bashrc" ]] && grep '&& return' "${HOME}/.bashrc" >/dev/null 2>&1 ; then

      andand_return_warning

    fi

    check_rubyopt_conditions

    printf "\nInstallation of RVM to $rvm_path/ is complete.\n\n"
  fi
}

#
# root install functions.
#
setup_rvm_path_permissions()
{
  chown -R root:"$rvm_group_name" "$rvm_path"

  chmod -R g+w "$rvm_path"

  if [[ -d "$rvm_path" ]] ; then
    find "$rvm_path" -type d -print0 | xargs -n1 -0 chmod g+s
  fi

  return 0
}

setup_rvm_group()
{
  if \grep "$rvm_group_name" /etc/group >/dev/null 2>&1 ; then
    echo "RVM system user group '$rvm_group_name' exists, proceeding with installation."
  else
    echo "Creating RVM system user group '$rvm_group_name'"

    case "$(uname)" in
      "OpenBSD")
        groupadd "$rvm_group_name"
        ;;
      "FreeBSD")
        pw groupadd -q "$rvm_group_name"
        ;;
      "Linux")
        if [[ -f "/etc/SuSE-release" ]] ; then
          groupadd "$rvm_group_name"
        else
          groupadd -f "$rvm_group_name"
        fi
      ;;

      "Darwin")
        gid="501" #only gids > 500 show up in user preferences

        #Find an open gid
        while true; do

          name=$(dscl . search /groups PrimaryGroupID $gid | cut -f1 -s)

          [[ -z "$name" ]] && break

          gid=$[$gid +1]
        done

        # Create the group, isn't OSX "fun"?! :)
        # Yes folks, screw convention!!!
        dscl . -create "/Groups/$rvm_group_name"
        dscl . -create "/Groups/$rvm_group_name" gid "$gid"
      ;;
      "SunOS") groupadd "$rvm_group_name";;
    esac
  fi

  return 0
}

system_check()
{
  local os=$(uname)
  case "$os" in
    OpenBSD|Linux|FreeBSD|Darwin|SunOS)
      return 0 # Accounted for, continue.
    ;;

    *)
      printf "Installing RVM as root is currently only supported on the following known OS's (uname):\n  Linux, FreeBSD, OpenBSD, Darwin and SunOS\nWhereas your OS is reported as '$os'" >&2
      return 1
    ;;
  esac
}

setup_etc_profile()
{
  local executable

  if [[ -d /etc/profile.d ]] ; then
    etc_profile_file="/etc/profile.d/rvm.sh"
    executable=1
  else
    etc_profile_file="/etc/profile"
    executable=0
  fi

  if ! grep "RVM profile" "${etc_profile_file}" >/dev/null 2>&1; then

    printf "#
# RVM profile
#
# /etc/profile.d/rvm.sh # sh extension required for loading.
#
if [ -n \"\${BASH_VERSION:-}\" -o -n \"\${ZSH_VERSION:-}\" ] ; then

  # Load user rvmrc configurations, if exist
  for file in /etc/rvmrc \"\$HOME/.rvmrc\" ; do
    [[ -s \"\$file\" ]] && source \$file
  done

  # Load RVM if it is installed, try user then root install.
  if [[ -s \"\$rvm_path/scripts/rvm\" ]] ; then
    source \"\$rvm_path/scripts/rvm\"

  elif [[ -s \"\$HOME/.rvm/scripts/rvm\" ]] ; then
    true \${rvm_path:=\"\$HOME/.rvm\"}
    source \"\$HOME/.rvm/scripts/rvm\"

  elif [[ -s \"/usr/local/rvm/scripts/rvm\" ]] ; then
    true \${rvm_path:=\"/usr/local/rvm/scripts/rvm\"}
    source \"/usr/local/rvm/scripts/rvm\"
  fi

  #
  # Opt-in for custom prompt through by setting:
  #
  #   rvm_ps1=1
  #
  # in either /etc/rvmrc or \$HOME/.rvmrc
  #
  if [[ \${rvm_ps1:-0} -eq 1 ]] ; then
    # Source RVM ps1 functions for a great prompt.
    if [[ -s \"\$rvm_path/contrib/ps1_functions\" ]] ; then
      source \"\$rvm_path/contrib/ps1_functions\"
    elif [[ -s \"/usr/local/rvm/contrib/ps1_functions\" ]] ; then
      source \"/usr/local/rvm/contrib/ps1_functions\"
    fi

    if command -v ps1_set >/dev/null 2>&1 ; then
      ps1_set
    fi
  fi

  # Add \$rvm_bin_path to \$PATH if necessary
  if [[ \"\${rvm_bin_path}\" != \"\${rvm_path}/bin\" ]] ; then
    regex=\"^([^:]*:)*\${rvm_bin_path}(:[^:]*)*\$\"
    if [[ ! \"\${PATH}\" =~ \$regex ]] ; then
      export PATH=\"\${rvm_bin_path}:\${PATH}\"
    fi
  fi
fi
" >> "${etc_profile_file}"

    [[ $executable -eq 1 ]] && chmod +x "${etc_profile_file}"

  fi

  return 0
}

add_user_to_rvm_group()
{
  echo "Adding user '$user' to the RVM group '${rvm_group_name}'"

  case "$(uname)" in
    "OpenBSD")
      usermod -G "$rvm_group_name" "$user"
      ;;
    "FreeBSD")
      pw usermod "$user" -G "$rvm_group_name"
      ;;
    "Linux")
      if [[ -f "/etc/SuSE-release" ]] ; then
        groupmod -A "$user" "$rvm_group_name"
      else
        /usr/sbin/usermod -a -G "$rvm_group_name" "$user"
      fi
    ;;
    "Darwin")
      dscl . -append "/Groups/$rvm_group_name" GroupMembership "$user"
      ;;
    "SunOS")
      groups="$(id -G "$user") \"$rvm_group_name\""
      usermod -G "${groups// /, }" "$user"
  esac

  return 0
}

setup_rvm_group_users()
{
  # TODO: implement this.  For now we'll use a naieve approach based on homes.
  user_homes=$(find /home/ -mindepth 1 -maxdepth 1 -type d)
  for user_home in ${user_homes[@]} ; do
    # TODO: Check if user is a login user.
    # TODO: Skip if user is already in the RVM group.
    user="${user_home//*\/}"
    printf "Ensuring '$user' is in group '$rvm_group_name'\n"
    add_user_to_rvm_group $user
  done
}

setup_rvmrc()
{
  if [[ $UID -eq 0 ]] ; then
    rvmrc_file="/etc/rvmrc"
    if ! grep 'umask g+w' $rvmrc_file >/dev/null 2>&1; then
      echo 'umask g+w' >> $rvmrc_file
    fi

    if [[ -s $rvmrc_file ]] ; then
      chown $USER:${rvm_group_name:-$USER} $rvmrc_file
    fi
  else
    rvmrc_file="$HOME/.rvmrc"
  fi

  if ! grep 'export rvm_path' $rvmrc_file >/dev/null 2>&1; then
    echo "export rvm_path=\"$rvm_path\"" >> $rvmrc_file
  fi

  return 0
}

setup_user_profile()
{
  if [[ $UID -ne 0 ]] ; then
    if [[ -n "${ZSH_VERSION:-}" ]] ; then
      profile_file="$HOME/.zshenv"
    else
      profile_file="$HOME/.bash_profile"
    fi

    if [[ -d /etc/profile.d ]] ; then
      etc_profile_file="/etc/profile.d/rvm.sh"
    else
      etc_profile_file="/etc/profile"
    fi

    if ! grep '$HOME/.rvm/scripts/rvm' "$profile_file" >/dev/null 2>&1; then
      if [[ ! -s "${etc_profile_file}" ]] ; then
        echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*' >> "$profile_file"
      fi
    fi
  fi

  return 0
}

root_canal()
{
  true ${rvm_group_name:=rvm}

  if [[ $UID -eq 0 ]] && system_check ; then
    setup_rvm_group
    setup_etc_profile
    setup_rvm_path_permissions
    setup_rvm_group_users
  fi

  return 0
}

