#!/usr/bin/perl
use strict;
use warnings;
use v5.16;

my $hostname = `hostname`;
chomp $hostname;
my $self = $hostname;
$self = 0 unless $self =~ m,^pontos,,;
$self =~ s,^pontos(..).*,$1,sm;
$self = 0 if $self > 4;


my %pontos =
(
  '01' => [ 'xrockai', 'xbaranov', 'xbarnat', 'brim', 'cerna', 'xolliaro', 'xvozarov', 'abrs', 'xmisenko' ],
  '02' => [ 'xstill', 'xmatous3', 'xbenes3', 'xceska', 'xsafran1', 'xmichel', 'xtrojak', 'xzacek1', 'xchalup4' ],
  '03' => [ 'xlauko1', 'xsarnik', 'xmihalk', 'xsevc', 'xjonas', 'xnausova', 'xsvobo25', 'xhorace1', 'xadamove' ],
  '04' => [ 'xkorenc1', 'xmrazek7', 'xdemko', 'xpastva', 'xbendik', 'xstrejc', 'xvitovs1', 'xchlup2', 'xondika', 'xkonicar' ]
);

my %all_pontos;
$all_pontos{$_} = 1 for ( map { @$_ } values %pontos );

my %psyche = ( 'xdemko' => 1, 'xpastva' => 1, 'xtrojak' => 1 );
my %pythia = ( 'xsarnik' => 1, 'xbaranov' => 1 );
my %primary_on_pontos = ( 'xstill' => 1, 'xmisenko' => 1, 'xadamove' => 1, 'xondika' => 1, 'xkrumlo1' => 1, 'xchalup4' => 1, 'xkonicar' => 1 );
$primary_on_pontos{'xmrazek7'} = 1 if $hostname =~ /^pontos/;
$primary_on_pontos{'xrockai'} = 1 if $hostname =~ /^pontos/;

my %all;
$all{$_} = 1 for ( split /\n/, `getent passwd | grep /home/ | cut -d: -f1` );
my %missing;
$missing{$_} = 1 for ( keys %all );
for my $v ( values %pontos ) { delete $missing{$_} for ( @$v ); };
print "missing: $_\n" for ( keys %missing );

my @from_antea = grep { !$primary_on_pontos{$_} } sort keys %all;

my @local;
@local = @{$pontos{$self}} if $self;
print "self: $self\n";
print "local: @local\n";

my %prefix;

for my $user ( keys %all ) {
    my $where = "/home";
    $where = "/pontos" if ( !$self && !$primary_on_pontos{$user} );
    $prefix{$user} = $where;
}

system "mkdir -p $prefix{$_}/$_ && chown $_:paradise $prefix{$_}/$_" for ( keys %all );
if ( $self )
{
    mkdir "/etc/exports.d";
    open E, ">/etc/exports.d/home-pontos-auto.exports" or die "Opening exports failed: $!";

    say E "# this is auto-generated file, edits will be discared on next generation";

    sub export
    {
        my ( $login, $perm, @host ) = @_;
        s,$,.fi.muni.cz, for ( @host );
        my $opt = 'no_subtree_check,root_squash,async,mp=/home';
        print E "/home/$login -$perm,$opt @host\n";
    }

    for ( @local )
    {
        export( $_, 'rw', 'pheme*', 'pandora*' );
        export( $_, 'rw', 'pontos*', 'arke' );
        export( $_, 'rw', 'anna', 'antea' );
        export( $_, 'rw', 'pythia*' ) if ( $primary_on_pontos{$_} || $pythia{$_} );

        export( $_, 'rw', 'psyche*' ) if ( $psyche{$_} );
        export( $_, 'rw', 'midas' ) if ( /xbenes/ );
        export( $_, 'rw', 'hesperia' ) if ( /xstill/ );
    }
    close( E );
    system 'exportfs -rav';
}

my $fstab = `cat /etc/fstab | grep -Ev '^(pontos..|antea|pandora01):/home(/|\\>)'`;
open F, ">/etc/fstab.new" or die "Opening fstab.new failed: $!";
print F "$fstab";
for my $k ( sort keys %pontos )
{
    next if $k eq $self;
    for ( @{$pontos{$k}} ) {
        next if ( $hostname =~ /^pythia/ && !$primary_on_pontos{$_} && !$pythia{$_} );
        next unless exists $prefix{$_};
        next if ( $hostname =~ /^pontos/ && $prefix{$_} =~ m,^/pontos, );
        print F "pontos$k:/home/$_ $prefix{$_}/$_ nfs noauto,vers=3,x-systemd.automount 0 0\n";
    }
}
if ( $hostname ne "antea" && !$self )
{
    print F "antea:/home/$_ /home/$_ nfs noauto,vers=3,x-systemd.automount 0 0\n" for ( sort @from_antea );
}
close F;
print("\n/etc/fstab:\n");
system("diff /etc/fstab.new /etc/fstab");
