#!/usr/bin/perl
# -*- Mode: cperl; coding: utf-8; cperl-indent-level: 4 -*-
# vim: ts=4 sts=4 sw=4:

#ident "$Id: create-missing-eth0-msg,v 1.4 2015/05/22 13:25:22 carsten.kuhl Exp $
# Copyright (c) 2014 Bruker BioSpin GmbH"";

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

sub EditGrubConfig($);
sub ShowHashArray($%);

my $program = basename($0);

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

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

###
# Some info about myself
my @Version =  split(' ','$Revision: 1.4 $ ');
my $Version = $Version[1];
my $OS_Version = get_os_version();
print "$program (Version $Version)\nCopyright (c) 2014 Bruker BioSpin GmbH\n\n" if $verbose;
print "OS_Version = $OS_Version\n\n" if $verbose;

my @net = `ls /sys/class/net|grep -v lo`;
my $grubcfg;
my $bioscfg = '/boot/grub2/grub.cfg';
my $ueficfg = '/boot/efi/EFI/centos/grub.cfg';
$grubcfg = $bioscfg if (-f $bioscfg);
$grubcfg = $ueficfg if (-f $ueficfg);
$grubcfg = '/tmp/grub.cfg' if $test;

die "NO grub.cfg found\n" unless $grubcfg;

my $reqcmd = "grub2-mkconfig $grubcfg";

my $show_error = '/tmp/TopSpin-Installation-Problem.html';
open(my $fh,">$show_error") || die "error: $!\n";
print $fh '<HTML><HEAD><META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">';
print $fh '<TITLE>Installation Notice of bruker-topspin-environ</TITLE></HEAD><BODY LANG="en-US" DIR="LTR">';
print $fh '<CENTER><H1 LANG="en">Installation Notice of bruker-topspin-environ</H1>';
print $fh '<FONT COLOR="#ff3366"><B><H2>The current Network Configuration<br> is not supported by the Flexlm Licence Manager.</H2></B></FONT>';
print $fh "<P>A default installation creates ethernet device like 'eno1' /
        'ens1' or 'enp0s3' , wich are not detected by the flexlm library and
        'lmhostid' returns an empty string.</P>";
print $fh '<BR><BR>';
print $fh '<span>You may install the <B>bruker-network-service</B> which will create the required <B>eth0</B> as IP link on each reboot. The link is set to the default device ens1. If you use or want a other device, change the config file <b>/lib/systemd/system/topspin-network.service</b>. Set --device=ens1 to the physical device you want.</span>';
print $fh '<UL> <LI><P><B>yum install --enablerepo=bruker-intranet &nbsp;bruker-network-service</B>';
print $fh '<BR><BR> </P> </UL>';
print $fh  '<FONT COLOR="#ff3366"><h2>Reboot is required after these changes!</h2></FONT>';
print $fh  '</CENTER></BODY></HTML>';
close($fh);

print "USE $grubcfg\nEXEC: xdg-open $show_error\n" if $verbose;

 
system("xdg-open $show_error");

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;
}


__END__

=head1 NAME

rename-eth-devices - rename ethernet devices according to topspin requirements

=head1 SYNOPSIS

rename-eth-devices [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

=item B<-save>

    Modifiy /etc/sysconfig/network-scripts/ifcfg-eth* files
    and change MAC addresse of ifcfg-eth1 to the device which
    has 'Hewlett Packard' as Vendors  OUI

=item B<-show>

    Display the Vendors  OUI of the HWADDR in 
    /etc/sysconfig/network-scripts/ifcfg-eth* files
=back


=head1 DESCRIPTION

rename-eth-devices - rename ethernet devices according to topspin requirements
 
This script checks if ethernet device are named eth0, eth2, ... and defines
the kernel option 'net.ifnames=0' to disable the renaming scheme.

=cut

