#!/bin/sh

set -eu

ruby_standalone_dir=@@prefix@@/lib/ruby-standalone
user=$(id -un)
shell=$(getent passwd "${user}" | cut -d : -f 7)

export RUBY_STANDALONE='@@prefix@@/lib/ruby-standalone'
export RUBY="${ruby_standalone_dir}/bin/ruby"
bindir="$($RUBY -e 'puts Gem.user_dir')/bin"
export PATH="$bindir:${ruby_standalone_dir}/bin:${PATH}"

# symlink rubyX.Y to ruby
vruby=$($RUBY -e 'puts RbConfig::CONFIG["ruby_install_name"]')
if [ ! -e "$bindir/$vruby" ]; then
  mkdir -p "$bindir"
  ln -sfT "$RUBY" "$bindir/$vruby"
fi

if [ $# -eq 0 ]; then
  exec "$shell"
else
  exec "$@"
fi
