#!/usr/bin/perl

# monDisk  (c) Copyright 1998 Mark Black 

# NOTE:  If you modify this code, you MUST change the version to
#        something over 100 to prevent it being replaced by future
#        versions of this script. 
$version = 2 ;

# monDisk     - This code is for determining the instances of
#		local disks.  It is needed to populate the MATd
#		GUI.
#          OUTPUT:
#                   1:      - Success
#                   0:info  - Failure
#	   Input args:
#                   -v      - Print script version number
#		    -i      - Instance.  This tells MATd if there is
#		              an instance of the parameter it wishes
#                             to monitor.  Code returns number of 
#                             instances.
#
#         Note:  Exit status of script should always be  0, anything
#	         else indicates an error in the monitoring script.
$input = "" ;
foreach $i (0 .. $#ARGV) {
   $input .= "@ARGV[$i] " ;
}
chop($input) ;

# Location of MAT tmp dir
$mattmp = "tmp" ;


# Discover OS type and set variables for OS type
chop($os = `uname -s`) ;

if($os eq "Linux") {
    $df = "/bin/df" ;
    $arg = "-k -x nfs -x iso9660" ;
    $discard = 1 ;
    $OS="linux" ;
} elsif($os eq "SunOS") {
    chop($ver = `uname -r`) ;
    ($major, $minor) = split(/\./, $ver) ;
    if($major == 5) {
	$df = "/bin/df" ;
        $arg = "-F ufs -k" ;
	$discard = 1 ;
	$OS = "solaris" ;
    } else {
	$df = "/usr/bin/df" ;
        $arg = "-x nfs" ;
	$discard = 1 ;
	$OS = "sunos" ;
    }
} elsif(($os eq "IRIX") | ($os eq "IRIX64"))  {
    $discard = 1 ;
    $OS = "irix" ;
    $df = "/usr/bin/df" ;
    $arg = "-k -l" ;
} elsif($os eq "HP-UX") {
    $discard = 1 ;
    $OS = "hpux" ;
    $df = "/usr/bin/bdf" ;
    $arg = "-l" ;
}



# Was the instance of network devices requested?
if($input eq "-i") {
    # List all local disks.  This command will block, until complete
    @data = `$df $arg 2>/dev/null` ;

    # Discard Header
    for $i (1 .. $discard ) {
        $drop = shift(@data) ;
    }

    # Parse the output from df
    $cnt = 0 ;
    $names = "" ;
    while ($line = shift(@data)) {
	$mntp = $line ;
	$mntp =~ s/.+\s(.+)$/$1/ ;
	chop($mntp) ;
        $names .= "$mntp:" ;
        $cnt++ ;
    }
    chop($names) ;
    print "$cnt:$names\n" ;
    exit(0) ;

} elsif($input eq "-v") {
    print "$version\n" ;
    exit(0) ;

} else {
    # Find the disk use for a specific partition
    @data = `$df $arg $input 2>/dev/null` ;
    if($!) {
	# Command failed!  Input must be in error
	print "ERROR: $!\n" ;
	exit(-1) ;
    }
    # This is done internally in MATd
}









