#!/usr/pkg/bin/ruby33
#
# enroll a new Student at an University 
# Usage: enroll <Name of University> <Student ID> <Gender> <Name>

require 'university'
require 'vapor'

# create an University give the name given on the commandline
if ARGV.size != 4 and (ARGV[0] != "M" or ARGV[0] != "F") then
  STDERR << "Usage: enroll <Name of University> <Student ID> <Gender> <Name>" 
  exit 1
end

uni_name = ARGV[0]
stud_id = ARGV[1].to_i
stud_name = ARGV[3]
stud_gender = 
  case ARGV[2]
  when "M" then "Male"
  when "F" then "Female"
  else
    STDERR << "ERROR: Gender must be specified as 'M' or 'F'\n"
    exit 1
  end

# connect to Repository
pmgr = Vapor::PersistenceManager.new( Properties )
pmgr.transaction.begin

# search for university
universities = pmgr.query( University, "name = ?", [ uni_name ] )
if universities.empty? then
  STDERR << "ERROR: No such university found.\n"
  exit1
end
university = universities[0]

# create Student
student = Student.new( stud_name, stud_gender, stud_id, university )

# save Student in Repository
student.make_persistent

pmgr.transaction.commit
