#!/usr/bin/perl

use lib "."; use lib "uxmon"; use lib "platforms"; 		#inslib
$hostname = $ENV{"HOST"};
chomp( $hostname = `/bin/hostname` ) unless( $hostname );

if( @ARGV ) {
    $file = shift;
    print "updateing $file\n";
    $tmp = "tmp.$$";
    open( IN, "<$file" );
    open( OUT, ">$tmp" );
    while( <IN> ) {
	last if( /^# Autogenerated/ );
	print OUT;
    }
    close IN;
    select OUT;
    print "# Autogenerated - do not edit\n";
}

&config;

if( $file ) {
    close OUT;
    rename( $tmp, $file );
}


# the procedure rules::config is called each time the file is read
sub config {
    my( $chk );
    my( %hosttype ) = &hosttype;

    undef $rules::ping;
		
    $line = "localhost\t";

    $line .= "diskload load syslog ";
    $line .= "dns " if( $hosttype{"dns"} );

    $line .= "\\\n\t\tservice=smtp";
    ($line .= ",printer") if( $hosttype{"LPD"} );
    ($line .= ",pop3") if( $hosttype{"POP"} );
    ($line .= ",smb") if( $hosttype{"PCS"} );
    $line .= " tcp ";

    $line .= "\\\n\t\tprocs=sendmail(1-30)";
    ($line .= ",nfsd(1-16),lockd(1-16),mountd(1-16)") if( $hosttype{"nfsserver"} );
    ($line .= ",hclnfsd(1-1)") if( $hosttype{"PCS"} );
    ($line .= ",rpc.pcnfsd(1-1)") if( $hosttype{"PCS"} );
    ($line .= ",lpd(1-30)") if( $hosttype{"LPD"} );
    ($line .= ",dhcpd(1-2)") if( $hosttype{"dhcp"} );
    ($line .= ",ypbind", undef, 1 ) if( $hosttype{"yp"} );
    ($line .= ",ypserv") if( $hosttype{"ypserver"} );
    ($line .= ",ypxfrd") if( $hosttype{"ypmaster"} );
    $line .= " procs ";

    @fs = ();
    $limit=$hosttype{"have_var"}?1000:10000;
    push( @fs, "/($limit-".($limit*2).")" ) if( $hosttype{"PCS"} || $hosttype{"oracle"} || $hosttype{"LPD"} || $hosttype{"POP"} );
    push( @fs, "/usr(4000-8000)") if( ($hosttype{"PCS"} || $hosttype{"oracle"})  && ! $hosttype{"test"} );
    push( @fs, "/var(10000-20000)") if( ($hosttype{"PCS"} || $hosttype{"oracle"} || $hosttype{"LPD"} || $hosttype{"POP"}) && $hosttype{"have_var"} );
    push( @fs, "/oracle(60000-120000)") if( $hosttype{"oracle"} );
    push( @fs, "/oracle/db/dat(60000-120000)" ) if( $hosttype{"oracle"} && -d "/oracle/db/dat" );
    $line .= "\\\n\t\tfs=".join( ",", @fs )." diskfree " if( @fs );

    @rpc = ();
    push( @rpc, "mount" ) if( $hosttype{"nfsserver"} );
    push( @rpc, "nfs" ) if( $hosttype{"nfsserver"} );
    push( @rpc, "nlm" ) if( $hosttype{"nfsserver"} );
    push( @rpc, "yp" ) if( $hosttype{"ypserver"} );
    push( @rpc, "yppasswd" ) if( $hosttype{"ypmaster"} );
    $line .= "\\\n\t\trpc=".join( ",", @rpc )." rpc " if( @rpc );

    # if dump is used for backup warn if last level 7 backup
    # is more than 3 days or last level 0 backup is more than 30 days old
    if( $hosttype{"dumpbak"} ) {
	$line .= "fs=.7(4.5-8),.0(30-40) dumpdates ";
    }

    if( $hosttype{"PCS"} ) {
	$line .= "\n${loc}notes\tping notes" if( gethostbyname( $loc."notes" ) );
	if( gethostbyname( $loc."cd" ) ) {
	    $line .= "\n${loc}cd\tping rpc=mount,nfs rpc cds";
	}
    }
    
    $disp = "obelix" if( gethostbyname("obelix" ) );
    $disp = "sadm" if( gethostbyname("sadm" ) );
    $disp = "solarix" if( gethostbyname("solarix.graeff.com" ) );
    $disp = "bsdisplay" if( gethostbyname("bsdisplay" ) );
    $line .= "\n$disp\t\tbsdisplay";
    print $line."\n";
}


sub hosttype {
    my( %hosttype, $domain );
    my( $mtime );

    chomp( $domain = `domainname 2>/dev/null` );
    $hosttype{"yp"} = $domain;
    $hosttype{"ypserver"} = $domain && -d "/var/yp/$domain";
    $hosttype{"ypmaster"} = `ypwhich -m passwd` =~ /^$hostname[\r\n]*$/; # ah, well ....
    $hosttype{"PCS"} = -d "/pcs/infra";
    $hosttype{"POP"} = (($hostname =~ /^pop/) || $hosttype{"PCS"}) ;
    $hosttype{"LPD"} = ((-f "/etc/printcap") || $hosttype{"PCS"}) ;
    system( "grep -s nfs /etc/dfs/sharetab > /dev/null 2>&1" );
    $hosttype{"nfsserver"} = (! $?) || (-f "/etc/exports" );
    $hosttype{"dhcp"} = (-f "/etc/dhcpd.conf");
    $hosttype{"oracle"} = (-f "/l/adm/orainstance");
    $hosttype{"test"} = ( $hostname eq "beatnix" );
    $hosttype{"dns"} = ( -f "/etc/named.boot" );
    my $df = `df`;
    $hosttype{"have_var"} = ($df =~ /\/var[\s\t]/);
    if( -f "/etc/dumpdates" ) {
	$mtime = (stat(_))[9];
	if( time-$mtime<3600*24*60 ) {
	    $hosttype{"dumpbak"} = 1;
	}
    }
    if( $BigSister::common::dl > 4 ) {
	print STDERR "host type:\n";
	my( $name, $val );
	while( ($name,$val) = each %hosttype ) {
	    print STDERR "  $name: $val\n";
	}
    }

    return %hosttype;
}



1;
