#!/usr/bin/env perl 
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## 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.
##
##**************************************************************

use strict;
use warnings;
use Config;

# Update the module include path
BEGIN
{
    my $Dir = $0;
    if ( $Dir =~ /(.*)\/.*/ )
    {
	push @INC, "$1";
    }
}
use HawkeyePublish;
use HawkeyeLib;

# Global Hawkeye data
my $Hawkeye;

my %uname_cmd = 
(
    "arch", "uname -m",
    "os", "uname -s",
    "os_ver", "uname -r",
);

# Override the flags based on a system
if ( ($Config{'archname'} =~ m!aix!) or
     ($Config{'archname'} =~ m!aix!) or
     ($Config{'archname'} =~ m!darwin!) ) {
    $uname_cmd{"arch"} = "uname -p";
}

Init();
RunIt();

sub Init (){
    HawkeyeLib::DoConfig( );

    # Setup the hawkeye stuff
    $Hawkeye = HawkeyePublish->new;
    $Hawkeye->Quiet( 1 );
    $Hawkeye->AutoIndexSet( 0 );
}

sub RunIt (){
    my $Hash = HawkeyeHash->new( \$Hawkeye, "" );
    my $arch = `$uname_cmd{"arch"}`;
    my $os = `$uname_cmd{"os"}`;
    my $os_version = `$uname_cmd{"os_ver"}`;

    chomp($arch);
    chomp($os);
    chomp($os_version);

    if ($Config{'archname'} =~ m!aix!) {
        my $major = `uname -v`;
        my $minor = `uname -r`;
        chomp $major;
        chomp $minor;
        $os_version = "$major.$minor"
    }

    $Hash->Add( "arch", HawkeyePublish::TypeString, trim($arch) );
    $Hash->Add( "os", HawkeyePublish::TypeString, trim($os) );
    $Hash->Add( "os_version", HawkeyePublish::TypeString, trim($os_version) );


    if ( lc($os) eq "linux") {
        my %distro = &get_linux_distro();
        $Hash->Add( "os_distro", HawkeyePublish::TypeString, "$distro{'long'} $distro{'version'}");
    }

    # Ok, summary is done...
    $Hash->Store( );

    # Publish
    $Hawkeye->Publish( );
}

sub get_linux_distro () {
    open(DISTRO, "/etc/issue") || die "Unable to open /etc/issue";
    my %distro =
    ( "long", "unknown",
      "short", "unknown",
      "version", "unknown",
    );

    while(<DISTRO>) {
        next if /^(\s)*$/;      # skip blank lines
        chomp;
        my $line = $_;
        my @distro_strs;

        if($line =~ m!Red Hat!) {
            @distro_strs = split(" ", $line);
            if($line =~ m!Red Hat Enterprise Linux AS release!) {
                $distro{"long"} = "$distro_strs[0]$distro_strs[1]$distro_strs[4]";
                $distro{"version"} = "$distro_strs[6]";
                $distro{"short"} = "rhas";
            }
            elsif($line =~ m!Red Hat Enterprise Linux ES release!) {
                $distro{"long"} = "$distro_strs[0]$distro_strs[1]$distro_strs[4]";
                $distro{"version"} = "$distro_strs[6]";
                $distro{"short"} = "rhes";
            }
            elsif($line =~ m!Red Hat Enterprise Linux WS release!) {
                $distro{"long"} = "$distro_strs[0]$distro_strs[1]$distro_strs[4]";
                $distro{"version"} = "$distro_strs[6]";
                $distro{"short"} = "rhws";
            }
            else {
                $distro{"long"} = "$distro_strs[0]$distro_strs[1]";
                $distro{"version"} = "$distro_strs[4]";
                $distro{"short"} = "rh";
            }
        }
        elsif($line =~ m!SuSE!) {
            @distro_strs = split(" ", $line);
            $distro{"long"} = "$distro_strs[3]";
            $distro{"version"} = "$distro_strs[4]";

            if($line =~ m!SuSE SLES!) {
                $distro{"short"} = "sles";
            }
            else {
                $distro{"short"} = "suse";
            }
        }
        elsif($line =~ m!Fedora!) {
            @distro_strs = split(" ", $line);
            $distro{"long"} = "$distro_strs[0]$distro_strs[1]";
            $distro{"version"} = " $distro_strs[3]";
            $distro{"short"} = "fc";
        }

        return %distro;
    }
}

sub trim () {
    my @str = @_;
    return &ltrim(&rtrim(@str));
}

sub rtrim () { 
    my @str = @_;
    for (@str) {
        s/\s+$//;
    }   
    return @str == 1 ? $str[0] : @str;
}
    
sub ltrim () {
    my @str = @_;
    for (@str) {
        s/^\s+//;
    }
    return @str == 1 ? $str[0] : @str;
}
