#!/usr/bin/perl -w
# ------------- HEADER --------------#
#------------------------------------#
#     Agent for Sun Cluster 3.2      #
#                                    #
# Resource Type: NONsharedDevice     #
# Method: STOP                       #
# Author: byLeal                     #
# Version: 2.0                       #
# Date: 29/fev/2008                  #
# Version: 1.0                       #
# Date: 16/ago/2007                  #
#------------------------------------#

# perl's config
use POSIX;
use Fcntl qw(:DEFAULT :flock);
use Getopt::Std;

# procedures..

sub log_print {
    my($now) = strftime "%Y%m%d-%H%M%S", localtime;
    print STDERR "${LOG_PROGNAME}[$$] $now: @_\n";
}

sub log_die {
    &log_print;
    cleanup();
    die("\n");
    # was: exit 1;
    # die is catchable in an "eval".
}

sub open_log {
    my($logfile) = @_;
    $LOGPIPE = 0;
    if (-t STDOUT) {       # guess interactive shell..
       if (open(LOGPIPE, "| tee -a $logfile") && open(STDOUT, ">&LOGPIPE")) {
           $LOGPIPE = 1;
       }
    }
    unless ($LOGPIPE) {
        open(STDOUT, ">> $logfile")
            or log_die "$MESSAGE_ABRIR $logfile: $!";
    }
    open(STDERR, ">&STDOUT")
        or log_die "Could not DUP stdout -> stderr:: $!";
}

sub lock {
    my($pidfile) = @_;
    sysopen PIDFILE, $pidfile, O_WRONLY | O_CREAT
        or log_die "$MESSAGE_ABRIR $pidfile: $!";
    flock PIDFILE, LOCK_EX | LOCK_NB
        or log_die "$MESSAGE_LOCK pidfile: $pidfile: $!";
    truncate PIDFILE, 0;
    syswrite PIDFILE, "$$\n";
}

sub usage() {
    printf "We must receive informations from Sun Cluster.\n";
    printf "Something like: -R <resource> -T <resourcetype> -G <resourcegroup> ..
.\n";
    die("\n");
}

sub run_command {
    my($command, $args) = @_;
    $ERROR = 0;
    log_print "running: $command $args";
    $ERROR = system "$command $args >/dev/null 2>&1";
    if ($ERROR) {
     log_die "->command execution error: $command $args";
    } else {
      log_print "command executed OK";
    }
}

sub cleanup {
   unlink("$DIR_CONTROL/$LOG_PROGNAME.pid");
   if ($LOGPIPE) {
       close STDOUT;
       close STDERR;
       close LOGPIPE;
   }
}

# some variables..
$REQUIRED_UID  = "0";
$MESSAGE_LOCK  = "Could not aquire exclusive lock";
$MESSAGE_DIR_A = "Could not access diretory";
$DIR_LOGS      = "/var/log/scnonshared";
$DIR_CONTROL   = "/var/scnonshared/run";

# THE GAME...
# better than nothing...
if ($REQUIRED_UID != $>) { printf "You must be root to run this program.."; die("\n");}

# Date and time for logging...
$DATE = strftime("%Y%m%d", localtime);

# Sun Cluster variables
%options=();
$options = getopts('R:T:G:', \%options);
usage() unless ($options{R} && $options{T} && $options{G});

# Who we are?
$LOG_PROGNAME = "nsd_svc_stop" . "-" . "$options{R}";

# initializing logging...
unless (-d $DIR_LOGS) { printf "$MESSAGE_DIR_A: \"$DIR_LOGS/\""; die("\n");}
open_log("$DIR_LOGS/$LOG_PROGNAME.$DATE.log");
unless (-d $DIR_CONTROL) { printf "$MESSAGE_DIR_A: \"$DIR_CONTROL/\""; die("\n");}
lock("$DIR_CONTROL/$LOG_PROGNAME.pid");

log_print "START - nsd_svc_stop - v2.0";
log_print "Now the postnet_stop of the various resources are run before the stop methods are executed in the dependency order. So, all the stop procedures are moved to postnet_stop script.";
log_print "END - nsd_svc_stop - v2.0";

cleanup();
exit 0;

