#!/usr/pkg/bin/ruby32
#
# employ or fire an University employee 
# Usage: employment <--fire|--employ> <Name of University> <Name of Employee>

require 'university'
require 'vapor'

# create an University give the name given on the commandline
if ARGV.size != 3 and (ARGV[0] != "--fire" or ARGV[0] != "--employ") then
  STDERR << "Usage: employment <--fire|--employ> <Name of University> <Name of Employee>"
  exit 1
end

mode = ARGV[0]
uni_name = ARGV[1]
employee_name = ARGV[2]

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

# search for university and for employee

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

employees = pmgr.query( Person, "name = ?", [employee_name ] )
if employees.empty? then
  STDERR << "ERROR: No such employee found.\n"
  exit 1
end
employee = employees[0]


case mode
when "--employ"
  university.employ( employee )
when "--fire"
  if !university.fire( employee ) then
    STDERR << "ERROR: Can't fire because #{university.name} isn't employing #{employee.name}\n"
    exit 1
  end
end

mgr.transaction.commit
