#!/usr/bin/perl
#
# pS-Performance Toolkit script that adds or removes services from the initial
# boot based on whether they are enabled or disabled.
#
# chkconfig: 2345 5 20
# description: pS-Performance Toolkit initial services maintenance script
#

use strict;
use warnings;

our $VERSION = 3.3;

=head1 NAME

services_init_script - Removes any symlinks in the boot order for disabled
services.

=head1 DESCRIPTION

This init script is meant to be called early in the boot order. The
script reads the set of configured services, and removes the symlinks to start
any disabled service from the boot process. This is necessary with the LiveCD
approach since the startup symlinks must be re-deleted each time the CD comes
up.

=cut

use strict;
use warnings;

use FindBin qw($RealBin);
use lib "$RealBin/../lib";

use lib "/opt/perfsonar_ps/toolkit/lib";

use File::Basename;

use perfSONAR_PS::NPToolkit::Config::Services;

if ( $ARGV[0] and $ARGV[0] eq "start" ) {
    start();
}
elsif ( $ARGV[0] and $ARGV[0] eq "stop" ) {
    stop();
}
elsif ( $ARGV[0] and $ARGV[0] eq "restart" ) {
    stop();
    start();
}
else {
    print "Usage: $0 {start|stop|restart}\n";
}

exit 0;

sub start {
    `touch /var/lock/subsys/services_init_script`;

    setup_links();
}

sub stop {
    # Theoretically, we shouldn't have to do this. However, during startup  
    # RedHat doesn't let you add scripts, only remove them. This problem is
    # only applicable to the NetInstalls.
    `rm /var/lock/subsys/services_init_script`;

    setup_links();
}

sub setup_links {
    my $services_conf = perfSONAR_PS::NPToolkit::Config::Services->new();
    $services_conf->init();

    my $services = $services_conf->get_services();

    if ( $services ) {
        foreach my $key ( keys %$services ) {
            my $service = $services->{$key};

            my @scripts = ();
	    if (ref($service->{service_name}) eq "ARRAY") {
		@scripts = @{ $service->{service_name} };
	    }
            else {
		push @scripts, $service->{service_name};
	    }

            foreach my $script (@scripts) {
                `chkconfig --list $script &> /dev/null`;
                next if $? != 0;
                
                if ($service->{enabled}) {
                    system( "chkconfig --add $script" );
                    system( "chkconfig $script on" );
                }
                else {
                    system( "chkconfig $script off" );
                }
            }
        }
    }
}

__END__

=head1 SEE ALSO

L<File::Basename>, L<FindBin>,
L<perfSONAR_PS::NPToolkit::Config::Services>

To join the 'perfSONAR Users' mailing list, please visit:

  https://mail.internet2.edu/wws/info/perfsonar-user

The perfSONAR-PS subversion repository is located at:

  http://anonsvn.internet2.edu/svn/perfSONAR-PS/trunk

Questions and comments can be directed to the author, or the mailing list.
Bugs, feature requests, and improvements can be directed here:

  http://code.google.com/p/perfsonar-ps/issues/list

=head1 VERSION

$Id$

=head1 AUTHOR

Aaron Brown, aaron@internet2.edu

=head1 LICENSE

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=head1 COPYRIGHT

Copyright (c) 2004-2009, Internet2 and the University of Delaware

All rights reserved.

=cut
