#! /usr/bin/ruby
# usage: rip-check
#
# Checks that the current shell session is rip-ready and rip-valid.
#
# Exits 0 if so, 1 if not

ripdir = ENV['RIPDIR']

# We could guess that the $RIPDIR is $HOME, but then they probably
# don't have their RUBYLIB or PATH variables setup properly,
# either. Checking $RIPDIR ensures they at least know what they're
# doing.
if ripdir.to_s.empty?
  abort "$RIPDIR not set. Please eval `rip-shell`"
end

require 'pathname'
ripdir = Pathname.new(ripdir)

# Ensure the rip directory structure is sound.
if !ripdir.exist?
  abort "#{ripdir.expand_path} not found. Please run `rip-setup`"
end

ripdir = ripdir.realpath.to_s

if ripenv = ENV['RIPENV']
  if !File.directory?("#{ripdir}/#{ripenv}")
    abort "ripenv #{ripenv} not found"
  end
else
  if File.symlink?("#{ripdir}/active")
    ripenv = File.readlink("#{ripdir}/active")

    if Pathname.new(ripenv).realpath.to_s.include?(ripdir)
      ripenv = File.basename(ripenv)
    end
  elsif File.exist?("#{ripdir}/inactive")
    abort "rip is currently disabled. `rip-on` to re-enable."
  else
    abort "No ripenvs found. Please run `rip-setup`"
  end
end

puts "RIPDIR=#{ripdir}"
puts "RIPENV=#{ripenv}"
