#!/usr/bin/perl
#
# pS-Performance Toolkit script that changes the MOTD to include the toolkit
# version number as well as the URL to go to configure the toolkit.
#
# chkconfig: 2345 99 99
# description: pS-Performance Toolkit MOTD maintenance script
#

use strict;
use warnings;

our $VERSION = 3.3;

=head1 NAME

motd_init_script - Displays a message of the day, and modifies the standard
/etc/motd to contain it.

=head1 DESCRIPTION

This init script is meant to be called as the last possible init script. The
script displays a message to the user and modifies the /etc/motd to contain
that same message. The purpose is to give the user some instructions before
they login so that they have some idea of what to do. The message it gives
contains a url to the web gui. It constructs this url using the "external"
address. The template file for the motd is located in
"/usr/local/etc/motd.tmpl".

=cut

use Template;
use File::Basename qw(dirname basename);

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

# The generate_motd script gets installed into /etc/init.d, so we need to keep
# a reference to the installed location.
use lib "/usr/lib/perfsonar/lib";

use perfSONAR_PS::NPToolkit::Config::Version;

use perfSONAR_PS::Utils::Host qw( discover_primary_address );

my $motd_template = "/etc/perfsonar/toolkit/templates/motd.tmpl";

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

exit 0;

sub stop {

    # Makes no sense to stop
}

sub start {
    my $external_address;
    my $version;

    my $version_config = perfSONAR_PS::NPToolkit::Config::Version->new();
    if ( $version_config->init() == 0 ) {
        $version = $version_config->get_version();
    }

    my $external_addresses = discover_primary_address();
    if ( $external_addresses ) {
        $external_address = $external_addresses->{primary_address};
    }

    my $template_directory = dirname( $motd_template );
    my $template_name      = basename( $motd_template );

    my $tt = Template->new( INCLUDE_PATH => $template_directory ) or die( "Couldn't initialize template toolkit" );

    my %vars = ( external_address => $external_address, version => $version );

    my $output;

    $tt->process( $template_name, \%vars, \$output ) or die $tt->error();

    open( MOTD, ">/etc/motd" );
    print MOTD $output;
    close( MOTD );
}

__END__

=head1 SEE ALSO

L<Template>, L<File::Basename>, L<FindBin>,

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

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

The perfSONAR-PS git repository is located at:

  https://code.google.com/p/perfsonar-ps/

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
