#!/usr/bin/perl

#ident "$Id$ Copyright (c) 2004 Bruker BioSpin GmbH"";

use strict;
use English;
use Getopt::Long;
use File::Basename;
use Pod::Usage;
use Sys::Syslog qw(:standard :macros);  # standard functions & macros

my $program	= basename($0);

die "Only root may run $program\n"  unless $EFFECTIVE_USER_ID == 0;

####
# Supported options
my $help = 0;
my $man = 0;
my $syslog = 1;
my $verbose = 0;

GetOptions(
	   'help|?' => \$help,
	   'man'  => \$man,
	   'verbose+'  => \$verbose,
	   'syslog!'  => \$syslog,
	  ) || pod2usage(2);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

my $dmi		= '/usr/sbin/dmidecode';

my $OS_Version = get_os_version();

die "Missing $dmi\n" unless -x $dmi;
sub boolean($) {my $var=shift; return $var ? 'true' : 'false'; }

if ($verbose)
{
    print STDERR '$verbose'," = ",boolean($verbose),"\n";
}
###
#

my $serial_number = GetSmbiosSerialNumber();


openlog('bruker-setup', 'nofatal,pid', 'user') if $syslog;

my $msg = "Default hostname set to $serial_number\n";
print $msg;
system('hostnamectl','--static','set-hostname',$serial_number);
syslog(LOG_INFO, $msg) if $syslog;
closelog if $syslog;
exit;

###################
##               ##
##  Subroutines  ##
##               ##
###################
sub get_os_version
{
    my $querycmd = 'rpm -qf --queryformat %{Name} /etc/redhat-release';
    my $redhat = `$querycmd`;
    chomp $redhat;
    $querycmd = 'rpm -q --queryformat %{version} ' . $redhat;
    my $redhat_release = `$querycmd`;
    my $rh_release  = (int $redhat_release);
    return $rh_release;
}
sub GetSmbiosSerialNumber
{
    open(DMI,"$dmi -t1|") || die "can't pipe to $dmi: $!\n";
    {
	my $ser_num;
	my $product;
	my $model;

	while (<DMI>)
	{
	    print STDERR if $verbose > 1;

	    if ( /Serial Number:\s(\w+)/ )
	    {
		$ser_num = $1;
		my $msg = '$' . "ser_num = $ser_num";

		print STDERR $msg,"\n" if $verbose;
		syslog(LOG_INFO, $msg) if $syslog;
	    }
	    if ( /Serial Number: VMware-(.*)\n/ )
	    {
		$ser_num = 'VM' . substr(md5_hex($1),0,8);
		my $msg = '$'."ser_num = $ser_num";
		print STDERR $msg,"\n" if $verbose;
		syslog(LOG_INFO, $msg) if $syslog;
	    }

	    if ( /Product Name:\s(.*)/ )
	    {
		$product = $1;
		print STDERR '$',"product = $product\n" if $verbose;

		if ($product  =~ (/^hp workstation (xw4100|xw4200|xw5000)/))
		{
		    $model = $1;
		}
		if ($product  =~ (/^HP\s*(xw4300|xw4400|Z400|Z420)\s*Workstation/))
		{
		    $model = $1;
		}
		if ($product  =~ (/^VMware Virtual Platform/))
		{
		    use Digest::MD5 qw(md5_hex);
		    if ( /Serial Number: VMware-(.*)\n/ )
		    {
			$ser_num = 'VM' . substr(md5_hex($1),0,8);
		    }
		}
	    }
	    if ($product  =~ (/^VirtualBox/))
	    {
		if ( /UUID:\s*([\dA-Z]*)/ )
		{
		    $ser_num = 'VB' . $1;
		}
	    }
	}
	close(DMI);
	return $ser_num;
    }
    return;
}


__END__

=head1 NAME

assign-default-hostname - create hostname from BIOS serial number

=head1 SYNOPSIS

assign-default-hostname [options]

Options:

     -help            brief help message
     -man             prints the manual page
     -verbose         create verbose output


=head1 OPTIONS

=over 8

=item B<-help>

    Print a brief help message and exits.

=item B<-man>

    Prints the manual page and exits.


=item B<-verbose>

    Print additional debug information

=back


=head1 DESCRIPTION

assign-default-hostname - create hostname from BIOS serial number

This script is required during system boot by the service bruker-setup
to set a unique hostname in the network. Therefore the serial number of the 
workstation is used as hostname.

=cut

