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

#ident "$Id: assign-network-devices,v 1.1 2015/04/24 14:33:29 gsc Exp $
# Copyright (c) 2014 Bruker BioSpin GmbH"";

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

sub ShowHashArray($%);

my $program = basename($0);

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

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.1 $ ');
my $Version = $Version[1];
print "$program (Version $Version)\nCopyright (c) 2015 Bruker BioSpin GmbH\n\n" if $verbose;


openlog('topspin-network.service', 'nofatal,pid', 'user') if $syslog;

##
# On master image SELinux is enabled
my $selinuxenabled = 1;
#DESCRIPTION
#  selinuxenabled  Indicates  whether SELinux is enabled or disabled. 
#  It exits with status 0 if SELinux is enabled and 1 if it is not enabled.
system('selinuxenabled')  and $selinuxenabled = 0;


###
# default file names
my $netcfg	= '/etc/sysconfig/network-scripts/ifcfg';
my $statusfile	= '/etc/sysconfig/bruker-setup';
my $grubcfg 	= '/etc/default/grub';
my $sysclass	= '/sys/class/net';

####
# Now get MAC addresses and PCI Bus info of Ethernet Cards
####
my %mac;
my %manufacturer;
my %interface;

opendir(NET,$sysclass) || die "can't open to $sysclass: $!\n";
my @ethdevs = grep(!/^\./, readdir(NET));
closedir(NET);

print "Found Ether Devices: @ethdevs\n" if $verbose;

foreach my $dev (@ethdevs)
{
    # check for physical devs
    my $linkname = readlink(join('/',$sysclass,$dev));
    next if ($linkname =~ /virtual\/net/);  # skip virtual

    print "$linkname\n" if $verbose;
    my $addrpath = join('/',$sysclass,$dev, 'address');
    open(ADR,$addrpath) || die "can't open to $addrpath: $!\n";
    my $addr = <ADR>;
    close(ADR);
    chomp $addr;
    print "$addr\n" if $verbose;

    if ( $dev eq 'eno1' or $dev eq 'enp0s8')
    {
	my $cmd = 'ip link add link ' . $dev . ' eth1 type macvlan';
	print "$cmd\n" if $verbose;
	syslog(LOG_INFO,$cmd ) if $syslog;
	system($cmd) unless $test;

	$cmd = 'ip link set eth1 address ' . $addr;
	print "$cmd\n" if $verbose;
	syslog(LOG_INFO,$cmd ) if $syslog;
	system($cmd) unless $test;
    }
    else
    {
	my $cmd = 'ip link add link ' . $dev . ' eth0 type macvlan';
	print "$cmd\n" if $verbose;
	syslog(LOG_INFO,$cmd ) if $syslog;
	system($cmd) unless $test;
	$cmd = 'ip link set eth0 address ' . $addr;
	print "$cmd\n" if $verbose;
	syslog(LOG_INFO,$cmd ) if $syslog;
	system($cmd) unless $test;
    }
}

closelog if $syslog;

exit;


sub ShowHashArray($%)
{
    my $head = shift;
    my %hash = @ARG;
    print "$head:\n";
    while (my($key,$val) = each(%hash))
    {
	print " $key => $val\n";
    }
}
