#!/usr/bin/perl
# Copyright (c) 2017 Bruker BioSpin GmbH"";

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

my $program = basename($0);

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

my $exitCode = 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;

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

###
# Some info about myself
###
my $Version = "1.0";
print "$program (Version $Version)\nCopyright (c) 2017 Bruker BioSpin GmbH\n" if $verbose;

# path to Bruker license container
my $brukerkey    = "/usr/share/bruker-setup/Codemeter/BRUKER_BBIO_WMLV.wbb";
my $cmu          = "/usr/bin/cmu";

sub getCodemeterContainerNumber {
	my $cmd = "$cmu -l";
	open(CM, "$cmd|") || die "Can't pipe to cmu: $!\n";
	my @lines = <CM>;
	close(CM);
	my $numSerial;
	
	if (@lines) {
	  $numSerial = (split / /, $lines[$#lines])[1];
	}
	return $numSerial;
}

###
#   Basis Test, everything available and accesable
###
my $errormsg;

unless (-e $brukerkey) {
	$errormsg = "Aborting script $program, because the Bruker CmContainer is missing.";
	syslog(LOG_ERR, "$errormsg") if $syslog;
	print "$errormsg" if $verbose;
	closelog if $syslog;
	exit 1;
}

unless (-x $cmu) {
	$errormsg = "Aborting script $program, because cmu command is missing.";
	syslog(LOG_ERR, "$errormsg") if $syslog;
	print "$errormsg" if $verbose;
	closelog if $syslog;
	exit 2;
}



# Check number of serials
unless (getCodemeterContainerNumber() eq '0') {
	$errormsg = "Aborting script $program, because a CM Container is found.";
	syslog(LOG_ERR, "$errormsg") if $syslog;
	print "$errormsg" if $verbose;
	closelog if $syslog;
	exit 3;
}

## All tests are successful
my $cmd = "$cmu --import --file $brukerkey";
print "Execute: $cmd\n" if $verbose;
system($cmd);

## Check import afterwards
unless (getCodemeterContainerNumber() eq '1') {
	$errormsg = "Aborting script $program, import of CM Container went wrong.";
	syslog(LOG_ERR, "$errormsg") if $syslog;
	print "$errormsg" if $verbose;
	closelog if $syslog;
	exit 4;
}

closelog if $syslog;
exit;

__END__

=head1 NAME

check-codemeter-license - Import the Bruker CodeMeter License Key within initial restart

=head1 SYNOPSIS

check-codemeter-license [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

This script imports the Bruker CmContainer after the cloning process.

=cut