# -*- Perl -*-
#
# Copyright (C) 1996 Craig Ruefenacht <ruefenac@cs.utah.edu> and the
#                    University of Utah Computer Science Department
#
# $Id: Rust_Exit.pm,v 1.15 1997/04/16 20:18:45 ruefenac Exp $
#
# This is part of RUST-1.0 Beta 6
#
# Abstract: This script contains exit and cleanup functions for
#           Rust scripts and modules
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with the source distribution of RUST, in the license
# directory. If you don't have a copy of it, write to: 
#
# Free Software Foundation, Inc.
# 675 Mass Ave
# Cambridge, MA 02139, USA.

{

package Rust_Exit;

use Rust_Configs;
use Rust_Debug;

require Exporter;
@ISA = qw(Exporter);
@EXPORT =
    (
     # name                       brief description
     # --------------------------------------------------------     
     'init_exit',,		# Initialize this library
     'program_error',		# Program had serios error, report it
     'quit_and_exit',		# Clean up and exit cleanly
     'terminate',		# Terminate prog due to problem
     'exit_prog'		# Do the actual exit call
     );

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub init_exit {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Initialize this library by adding the $global
    #           hash to this object.
 
    $globals = get_globals;
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub program_error {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: A serious error has occured in the rustt system.
    #           This will exit cleanly, and also send mail to
    #           the admin mailing list reporting the error.
    #
    #           If $type = 1, send the email that was sent to
    #           the request system along with the error report
    #
    #           Assumes that locked files have been unlocked    

}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub quit_and_exit {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Quit the program.  Terminate debugging first.
    #           Assumes that locked files have been unlocked
   
    my ($locked_files) = @_;

    if ($locked_files) {
	$locked_files->unlock_all;
    }

    Rust_Debug::end_debugging;
    &exit_prog;
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub terminate {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Call quit_and_exit, but first make mention as to why
    #           this program is terminating.  This function is to be
    #           called when things go wrong and we need to write an
    #           error to STDOUT.  Assumes that locked files have been
    #           unlocked

    my ($reason) = @_;
    print "exit: $reason";
    Rust_Debug::debug("Exiting: $reason");
    &quit_and_exit();
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub exit_prog {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Just call exit0;
    #

    exit 0;
}
}
1;
