#! /bin/bash
#
# (C) Copyright IBM Corp. 2001, 2003, 2003
#
# $Id: rvm,v 1.17 2003/07/28 23:25:57 augart-oss Exp $
#
# Invoke the rvm script using environment variables

# The script validates the RVM_ environment variables and
# passes them to the runrvm executable, which then
# computes command line arguments for the Jikes RVM executable.
# @author Jeffrey Palm
# @date   25 Jun 2002
# @modified Steven Augart (ported to bash)
# @date May 31, 2003


# Sanity checks
# What is our name?
# Bash internal shorthand that works like the "basename" command.
ME="${0##*/}"

# Where are we?
# Bash internal shorthand that works like the "dirname" command.
mydir="${0%/*}"			# temporary; directory we were run from

function croak_nonusage () {
    # Display the error message.  If it's a multi-line error message, indent
    # the second and subsequent lines by a few spaces.  
    # Try to auto-wrap the message if we have GNU Fold.

    local gnufold="/usr/bin/fold --width=65 --spaces"
    $gnufold < /dev/null &> /dev/null || gnufold=cat

    echo "${ME}: $*" | $gnufold | sed -e '2,$s/^/     /' >&2
    trap '' EXIT
    exit 1
}


if [[ $mydir && $mydir != $ME ]]; then
    # Where to find auxiliary programs:
    bin_dir="${mydir}"
else
    bin_dir="${RVM_ROOT:?$ME: You must set the RVM_ROOT variable before you run this program.}/rvm/bin"
fi

sanity_env="${bin_dir}/sanity-envars.bash"
[[ -f $sanity_env ]] || croak_nonusage "Internal error: Cannot find the file sanity-envars.bash ($sanity_env); something is badly broken."
[[ -r $sanity_env ]] || croak_nonusage "Internal error: Cannot read the file sanity-envars.bash ($sanity_env); something is badly broken.  You might check the file permissions or user id you used to extract Jikes RVM."
. "${sanity_env}";		# Defines checkenv()

## Sanity checks for environment variables.

checkenv HOME
# Place where source files reside.
checkenv RVM_ROOT

# We reset bin_dir so that we're sure to have an absolute pathname.
bin_dir="${RVM_ROOT}/rvm/bin";
checkenv RVM_BUILD

[[ -f ${bin_dir}/runrvm ]] || croak_nonusage "Can't find the program $bin_dir/runrvm.  Are you sure your RVM_ROOT variable is set correctly?"
# Pass these along as parameters to runrvm.
#. "${mydir}/runrvm" "$HOME" "$RVM_ROOT" "$RVM_BUILD" "$@"
. "${bin_dir}/runrvm" "$HOME" "$RVM_ROOT" "$RVM_BUILD" "$@"
