From 5336a4f51b0d2cfd7c7811296dca8eda72a06f07 Mon Sep 17 00:00:00 2001
From: Peter Marquardt <wwwutz@molgen.mpg.de>
Date: Thu, 13 Jun 2019 10:03:01 +0200
Subject: [PATCH 1/2] mkmotd.pl: reformat source

---
 mkmotd/mkmotd.pl | 220 ++++++++++++++++++++++++-----------------------
 1 file changed, 112 insertions(+), 108 deletions(-)

diff --git a/mkmotd/mkmotd.pl b/mkmotd/mkmotd.pl
index 36fa3511..27ae847c 100755
--- a/mkmotd/mkmotd.pl
+++ b/mkmotd/mkmotd.pl
@@ -6,12 +6,13 @@
 # Mutter: https://github.molgen.mpg.de/mariux64/mxtools/tree/master/mkmotd
 #
 
-my ($os_release)=`/bin/os-release`;chomp($os_release);
+my ($os_release) = `/bin/os-release`;
+chomp($os_release);
 
 our $TEMPLATE;
 
-if ($os_release eq 'mariux_x86_64' || $os_release eq 'mariux_i686') {
-	$TEMPLATE=<<'EOF';
+if ( $os_release eq 'mariux_x86_64' || $os_release eq 'mariux_i686' ) {
+    $TEMPLATE = <<'EOF';
 
   #     #                  ###         #     #  %HOST%
   ##   ##    ##    #####    #   #    #  #   #
@@ -26,8 +27,9 @@
   http://twiki.molgen.mpg.de/twiki/bin/view/Edv/MarIuXNews
 
 EOF
-} elsif ($os_release eq 'mariux64') {
-	$TEMPLATE=<<'EOF';
+}
+elsif ( $os_release eq 'mariux64' ) {
+    $TEMPLATE = <<'EOF';
 
   #     #                  ###         #     #  %HOST%
   ##   ##    ##    #####    #   #    #  #   #   system %SYSTEM%
@@ -39,74 +41,77 @@
 
 EOF
 
-$TEMPLATE.="    / to bee or not to bee /   ";
+    $TEMPLATE .= "    / to bee or not to bee /   ";
 
-if ( -d '/node/tags' ) {
-  $_=`ls /node/tags`;s/\n/ /g;
-  $TEMPLATE.="tags: $_";
-}
+    if ( -d '/node/tags' ) {
+        $_ = `ls /node/tags`;
+        s/\n/ /g;
+        $TEMPLATE .= "tags: $_";
+    }
 
-$TEMPLATE.="\n";
+    $TEMPLATE .= "\n";
 
-} else {
-	die "$os_release : unknown os_release\n";
+}
+else {
+    die "$os_release : unknown os_release\n";
 }
 
 sub readstr {
-  my $f = shift;
-  if ( -e $f ) {
-    open I,'<',$f or die "cannot readstr $f: $!";
-    my $str = <I>;
-    chomp ($str);
-    close I;
-    return $str;
-  }
-  return undef;
+    my $f = shift;
+    if ( -e $f ) {
+        open I, '<', $f or die "cannot readstr $f: $!";
+        my $str = <I>;
+        chomp($str);
+        close I;
+        return $str;
+    }
+    return undef;
 }
 
-sub policy {				# policy('theinternet') --> ('zeile1,'zeile2',....)
-  my ($hostname)=@_;
-
-  open IN,'<','/etc/mxpolicy' or return ();
-  while (<IN>) {
-    if (/^\S/ && /\b$hostname\b/) {
-      my @out;
-      while (<IN>) {
-	/^\S/ and last;
-	/\s+\*\s*(.+)/ and push @out,$1;
-	if (/\s+\`\s*(.+)/) {
-	  my $x = `$1`;chomp($x);
-	  push @out,split(/\n/,$x);
-	}
-      }
-      return @out;
+sub policy {    # policy('theinternet') --> ('zeile1,'zeile2',....)
+    my ($hostname) = @_;
+
+    open IN, '<', '/etc/mxpolicy' or return ();
+    while (<IN>) {
+        if ( /^\S/ && /\b$hostname\b/ ) {
+            my @out;
+            while (<IN>) {
+                /^\S/ and last;
+                /\s+\*\s*(.+)/ and push @out, $1;
+                if (/\s+\`\s*(.+)/) {
+                    my $x = `$1`;
+                    chomp($x);
+                    push @out, split( /\n/, $x );
+                }
+            }
+            return @out;
+        }
     }
-  }
-  return ();
+    return ();
 }
 
-
 my %T;
 
-my @un = split(/ /,`uname -a`);
+my @un = split( / /, `uname -a` );
 chomp(@un);
-$_=$un[1];s/\.molgen\.mpg\.de//;
+$_ = $un[1];
+s/\.molgen\.mpg\.de//;
 $T{HOST}   = $_;
 $T{KERNEL} = "$un[2] $un[3]";
-$T{BITS} = 32;     # default (may change sometime...)
-$T{ARCH} = 'i686';
-if ($T{KERNEL} =~ /x86_64|mx64/) {
-  $T{BITS} = 64;
-  $T{ARCH} = 'x86_64';
+$T{BITS}   = 32;                # default (may change sometime...)
+$T{ARCH}   = 'i686';
+if ( $T{KERNEL} =~ /x86_64|mx64/ ) {
+    $T{BITS} = 64;
+    $T{ARCH} = 'x86_64';
 }
 
 $_ = `/lib/libc.so.6`;
 /version (\d\S+)[\.,]/;
 $T{GLIBC} = $1;
 
-my @pc=`cat /proc/cpuinfo`;
-my $mn='unknown CPU';
-$T{NCPU} = grep {/model name/ and $mn = $_; /processor/} @pc;
+my @pc = `cat /proc/cpuinfo`;
+my $mn = 'unknown CPU';
+$T{NCPU} = grep { /model name/ and $mn = $_; /processor/ } @pc;
 $mn =~ s/\(.*?\)|\d+-Core Processor|xeon|Virtual|Athlon|model name\s*:|six.core|quad.core|dual.core|intel|Processor|cpu|core| with Radeon Vega Graphics//ig;
 $mn =~ s/\s(E\d\-)\s/$1/;
 $mn =~ s/\s\d\s\@/ \@/;
@@ -115,82 +120,81 @@ sub policy {				# policy('theinternet') --> ('zeile1,'zeile2',....)
 $mn =~ s/^\s+|\s+$//g;
 $T{CPU} = $mn;
 
-$T{SYSTEM}='';
+$T{SYSTEM} = '';
 {
-  my $sys = '';
-
-#  for i in /sys/devices/virtual/dmi/id/*_{name,vendor,version}; do echo -en "$i\t"; cat $i; done
-#  for i in /sys/devices/virtual/dmi/id/{product_{name,version},sys_vendor}; do echo -en "$i\t"; cat $i; done
+    my $sys = '';
 
-#  /sys/devices/virtual/dmi/id
+    #  for i in /sys/devices/virtual/dmi/id/*_{name,vendor,version}; do echo -en "$i\t"; cat $i; done
+    #  for i in /sys/devices/virtual/dmi/id/{product_{name,version},sys_vendor}; do echo -en "$i\t"; cat $i; done
 
-  my $pn = readstr('/sys/devices/virtual/dmi/id/product_name');
-  if ($pn =~ m/All Series|empty/) {
-    $pn = readstr('/sys/devices/virtual/dmi/id/board_name');
-    $pn =~ s/^TYAN Transport\s+//;
-  }
+    #  /sys/devices/virtual/dmi/id
 
-  my $mf = readstr('/sys/devices/virtual/dmi/id/sys_vendor');
-  if ($mf =~ m/(empty|NVIDIA)/) {
-    $mf = readstr('/sys/devices/virtual/dmi/id/board_vendor');
-  }
+    my $pn = readstr('/sys/devices/virtual/dmi/id/product_name');
+    if ( $pn =~ m/All Series|empty/ ) {
+        $pn = readstr('/sys/devices/virtual/dmi/id/board_name');
+        $pn =~ s/^TYAN Transport\s+//;
+    }
 
-  ($mf eq 'Supermicro') and $pn =~ s/^AS -//;
-  ($mf =~ /^Gigabyte/) and $pn =~ s/^GA-//;
+    my $mf = readstr('/sys/devices/virtual/dmi/id/sys_vendor');
+    if ( $mf =~ m/(empty|NVIDIA)/ ) {
+        $mf = readstr('/sys/devices/virtual/dmi/id/board_vendor');
+    }
 
+    ( $mf eq 'Supermicro' ) and $pn =~ s/^AS -//;
+    ( $mf =~ /^Gigabyte/ )  and $pn =~ s/^GA-//;
 
-  $mf =~ s/( Inc.| Computer Corporation)//;
+    $mf =~ s/( Inc.| Computer Corporation)//;
 
-  my $vn = readstr('/sys/devices/virtual/dmi/id/product_version');
-  if ($vn =~ m/empty/) {
-    $vn = undef;
-  }
+    my $vn = readstr('/sys/devices/virtual/dmi/id/product_version');
+    if ( $vn =~ m/empty/ ) {
+        $vn = undef;
+    }
 
-  if ( defined($pn) and $pn eq 'CK8 ') {
-    $mf = $vn;
-    $pn = '';
-  }
-  ($mf eq 'QEMU') and $pn='';
+    if ( defined($pn) and $pn eq 'CK8 ' ) {
+        $mf = $vn;
+        $pn = '';
+    }
+    ( $mf eq 'QEMU' ) and $pn = '';
 
-  ($pn ne '') and $pn = " $pn";
-  $T{SYSTEM} = "$mf$pn";
-  $T{SYSTEM} =~ s/_/ /g;
-  $T{SYSTEM} =~ s{(^HP | WorkStation| Transport| Tower|Hewlett-Packard |/S2932-E/S2932-SI| Thunder| Computer Corp|Technology Co., Ltd. | Standard PC)}{}i;
+    ( $pn ne '' ) and $pn = " $pn";
+    $T{SYSTEM} = "$mf$pn";
+    $T{SYSTEM} =~ s/_/ /g;
+    $T{SYSTEM} =~ s{(^HP | WorkStation| Transport| Tower|Hewlett-Packard |/S2932-E/S2932-SI| Thunder| Computer Corp|Technology Co., Ltd. | Standard PC)}{}i;
 }
 
 my %BC = (
-  'bugblatterbeastoftraal' => 'TYAN VX50 B4985',
-#  'crap03' => 'TYAN GT20-B2925/B2925-E',
-  'keineahnung' => 'FUJITSU ESPRIMO P756',
-  'furunkel' => 'HP Z200 Workstation',
+    'bugblatterbeastoftraal' => 'TYAN VX50 B4985',
+
+    #  'crap03' => 'TYAN GT20-B2925/B2925-E',
+    'keineahnung' => 'FUJITSU ESPRIMO P756',
+    'furunkel'    => 'HP Z200 Workstation',
 );
-if (exists($BC{$T{HOST}}) and $BC{$T{HOST}} ne $T{SYSTEM}) {
-  system("banner WRONG");
-  die "$BC{$T{HOST}} ne $T{SYSTEM}";
+if ( exists( $BC{ $T{HOST} } ) and $BC{ $T{HOST} } ne $T{SYSTEM} ) {
+    system("banner WRONG");
+    die "$BC{$T{HOST}} ne $T{SYSTEM}";
 }
 my @mem = `cat /proc/meminfo`;
 my $mem = '??';
 for (@mem) {
-  if (/MemTotal:\s+(\d+)/) {
-     my $mm = $1;
-     if ( int($mm / 1024 / 1000 ) >= 1 ) {
-        $mem  = (int($mm / 1024 / 100)/10) + 0;
-	$mem .= ' GB';
-     }
-     elsif ( int($mm / 1000 ) >= 1 ) {
-        $mem  = (int($mm / 100)/10) + 0;
-	$mem .= ' MB';
-     }
-     else {
-        $mem = "$mm kB";
-     }
-     last;
-  }
+    if (/MemTotal:\s+(\d+)/) {
+        my $mm = $1;
+        if ( int( $mm / 1024 / 1000 ) >= 1 ) {
+            $mem = ( int( $mm / 1024 / 100 ) / 10 ) + 0;
+            $mem .= ' GB';
+        }
+        elsif ( int( $mm / 1000 ) >= 1 ) {
+            $mem = ( int( $mm / 100 ) / 10 ) + 0;
+            $mem .= ' MB';
+        }
+        else {
+            $mem = "$mm kB";
+        }
+        last;
+    }
 }
 $T{MEM} = $mem;
 
-
-$TEMPLATE=~s/%(\S+?)%/$T{$1}||$1/eg;
-open O, '>','/etc/motd' or die "/etc/motd: $!\n";
-print O $TEMPLATE,  (join("\n",map ("  ** $_ **",policy($T{HOST})))||"  ** **"),"\n\n";
+$TEMPLATE =~ s/%(\S+?)%/$T{$1}||$1/eg;
+open O, '>', '/etc/motd' or die "/etc/motd: $!\n";
+print O $TEMPLATE, ( join( "\n", map ( "  ** $_ **", policy( $T{HOST} ) ) ) || "  ** **" ), "\n\n";
 close O;

From 6c8f58d336bcb04366ad2cc7843198c16539f1bf Mon Sep 17 00:00:00 2001
From: Peter Marquardt <wwwutz@molgen.mpg.de>
Date: Fri, 13 Dec 2019 08:49:13 +0100
Subject: [PATCH 2/2] wip

---
 mkmotd/mkmotd.pl | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/mkmotd/mkmotd.pl b/mkmotd/mkmotd.pl
index 27ae847c..ab9a2652 100755
--- a/mkmotd/mkmotd.pl
+++ b/mkmotd/mkmotd.pl
@@ -70,13 +70,27 @@ sub readstr {
 
 sub policy {    # policy('theinternet') --> ('zeile1,'zeile2',....)
     my ($hostname) = @_;
-
+    my @hostconfig=split(/ |\n/, `hostconfig`);
     open IN, '<', '/etc/mxpolicy' or return ();
+    my @out;
     while (<IN>) {
-        if ( /^\S/ && /\b$hostname\b/ ) {
-            my @out;
+LOOP:
+        my ( $tag ) = /^\@(\S+)/;
+        if ( defined($tag) && grep { $tag eq $_ } @hostconfig ) {
+            while (<IN>) {
+                /^\S/ and goto LOOP;
+                /\s+\*\s*(.+)/ and push @out, $1;
+                if (/\s+\`\s*(.+)/) {
+                    my $x = `$1`;
+                    chomp($x);
+                    push @out, split( /\n/, $x );
+                }
+            }
+        }
+
+        if ( /^\S/ && /\b$hostname\b/) {
             while (<IN>) {
-                /^\S/ and last;
+                /^\S/ and goto LOOP;
                 /\s+\*\s*(.+)/ and push @out, $1;
                 if (/\s+\`\s*(.+)/) {
                     my $x = `$1`;
@@ -84,10 +98,9 @@ sub policy {    # policy('theinternet') --> ('zeile1,'zeile2',....)
                     push @out, split( /\n/, $x );
                 }
             }
-            return @out;
         }
     }
-    return ();
+    return @out;
 }
 
 my %T;