#!/usr/bin/perl

#  -------------------  Needs more work for the other OS's ------------

# hostinfo  (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 = 1 ;

# hostinfo [-v]
# This script discovers some of the common system information 
# about the current host.

$input = "" ;
foreach $i (0 .. $#ARGV) {
   $input .= "@ARGV[$i] " ;
}
chop($input) ;

# Output the version if requested
if($input eq "-v") {
    print "$version\n" ;
    exit(0) ;
}

$debug = 0 ;      # Set to 1 to turn debug mode ;
# OS global variables
$outputType = 0 ;

# Discover OS type and set variables for OS type
chop($os = `uname -s`) ;
chop($date = `date +%m:%d:%T`) ;
 
if($os eq "Linux") {
    $fstab = "/etc/fstab" ;
    $uname = "uname -m -n -s -r" ;
    $arp = "/sbin/arp" ;
    $uptime = "uptime" ;
    @tmpfree = `free` ;
} else {
    $uname = "uname -m -n -s -r" ;
    $arp = "/sbin/arp" ;
    $uptime = "uptime" ;
}

chop($info = `$uname`) ;
($os, $hostname, $ver, $arch) = split(/ /, $info) ;

# Get IP address from hosts
@line = grep(/$hostname/, `cat /etc/hosts`) ;
foreach $entry (@line) {      # This strips out commented-out hosts
    if($entry =~ s/^(\d+)\.(\d+)\.(\d+)\.(\d+).+$hostname/hello/) {
	$ip = "$1\.$2\.$3\.$4" ;
    }
}

# Get uptime
($a, $b, $c, $utime) = split(/\s+/, `$uptime`) ;

# Get Memory/Swap size
($junk, $memtotal) = split(/\s+/, @tmpfree[1]) ;
($junk, $swaptotal) = split(/\s+/, @tmpfree[3]) ;

# Find number of Drives
open(FSTAB, $fstab) ;
@rfilesys = <FSTAB> ;
close(FSTAB) ;
@filesys = grep(!/^#/, @rfilesys) ;
@ufs = grep(/\/dev/, @filesys) ;


if($outputType == 0) {
    print "HOSTname: $hostname\n" ;
    print "HOSTos: $os\n" ;
    print "HOSTver: $ver\n" ;
    print "HOSTarch: $arch\n" ;
    print "HOSTuptime: $utime\n" ;
    print "HOSTip: $ip\n" ;
    print "HOSTmem: $memtotal\n" ;
    print "HOSTswap: $swaptotal\n" ;
}



