#!/usr/bin/perl
#
# This script is a component of Warewulf,
# http://www.runlevelzero.net/greg/warewulf
#
#########################################################################
#
# Copyright (c) 2003, The Regents of the University of California,
# through
# Lawrence Berkeley National Laboratory (subject to receipt of any
# required approvals from the U.S. Dept. of Energy).  All rights
# reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# The GNU GPL Document can be found at:
# http://www.gnu.org/copyleft/gpl.html
#
#########################################################################
#
# Written and maintained by:
#       Greg Kurtzer, <gmkurtzer@lbl.gov>

use lib "/usr/lib/warewulf/", "/usr/lib64/warewulf/";
use Warewulf::Config;
use Warewulf::Util;
use IO::Socket;
use Net::hostent;

#use Getopt::Long;
#use strict;

my $random = &generate_random_string('10');
my $dirpath = "/tmp/.$random";
system("mkdir -p $dirpath");


#GetOptions(
#   'help'      => \$help,
#);

$group = $ARGV[0];

if ( ! $group ) {
   die "Groupname please!\n";
}

my %nodes = &node_config();
my %master = &master_config();
my %groups = &group_config();

if ( ! $groups{$group} ) {
   die "Group name '$group' is not configured!\n";
}

system("mkdir -p $dirpath/etc");

# Read in all the password/useracct entries on the master.
while ( @l = getpwent() ) {
   $passwd_line{"$l[0]"} = "$l[0]:x:$l[2]:$l[3]:$l[6]:$l[7]:$l[8]";
   ($gname, $gpassword, $gid, $gmembers) = getgrgid($l[3]);
   $passwd_gname{$gname}{"$l[0]"} = "1";
}
endpwent();

# Read in all the group entries on the master.
while ( @l = getgrent() ) {
   $group_members{$l[0]} = $l[3];
   $users = $l[3];
   $users =~ s/\s/,/g;
   $group_line{$l[0]} = "$l[0]:x:$l[2]:$users";
   @users = split(/\s*,\s*/, $users);
   foreach $u ( @users ) {
      $passwd_gname{"$l[0]"}{$u} = "1";
   }
}
endgrent();

#foreach $group ( keys %groups ) {
   $def_accts = ();
   $def_groups = ();
   my $vnfsdir = "$master{'paths'}{'vnfs dir'}/$groups{$group}{'vnfs'}";
   open(PASSWD, "$vnfsdir/etc/passwd");
   while(<PASSWD>) {
      $def_passwd .= $_;
   }
   close PASSWD;
   open(GROUP, "$vnfsdir/etc/group");
   while(<GROUP>) {
      $def_groups .= $_;
   }
   close GROUP;
   @users = split(/[\s|,]+/, $groups{$group}{'user names'});
   foreach $u ( @users ) {
      if ( $passwd_line{$u} ) {
         $passwd{$u} = $passwd_line{$u};
      }
   }
   @groups = split(/[\s|,]+/, $groups{$group}{'user groups'});
   foreach $g ( @groups ) {
      foreach $u ( keys %{$passwd_gname{"$g"}} ) {
         if ( $passwd_line{$u} ) {
            $passwd{$u} = $passwd_line{$u};
            $group_entries{$g} = $group_line{$g};
         }
      }
   }
   open(PASSWD, "> $dirpath/etc/passwd");
   print PASSWD $def_passwd;
   foreach $l ( keys %passwd ) {
      print PASSWD "$passwd{$l}\n";
   }
   close PASSWD;
   open(GROUP, "> $dirpath/etc/group");
   print GROUP $def_groups;
   foreach $g ( keys %group_entries ) {
      print GROUP "$group_entries{$g}\n";
   }
   close GROUP;

   $outfile = ();
   open(HOSTS, "< $master{'paths'}{'vnfs dir'}/$groups{$group}{'vnfs'}/etc/hosts");
   while (<HOSTS>) {
      chomp;
      $outfile .= "$_\n";
   }
   close HOSTS;

   $outfile .= &gen_hosts(\%nodes, \%master);

   open(CONF, "> $dirpath/etc/hosts");
   print CONF $outfile;
   close CONF;

   $outfile = ();
   open(HOSTSEQUIV, "< $master{'paths'}{'vnfs dir'}/$groups{$group}{'vnfs'}/etc/hosts.equiv");
   while (<HOSTSEQUIV>) {
      chomp;
      $outfile .= "$_\n";
   }
   close HOSTSEQUIV;

   $outfile .= "$master{'network'}{'nodename'}\n";
   $outfile .= "$master{'network'}{'nodename'}-admin\n";
   $outfile .= "$master{'network'}{'nodename'}-clust\n";
   $outfile .= "$master{'network'}{'nodename'}-sharedfs\n";

   nodeloop0: foreach ( sort keys %nodes ) {
      next nodeloop0 if ( $nodes{$_}{'skip'} );
      $outfile .= "$_\n";
   }
   open(CONF, "> $dirpath/etc/hosts.equiv");
   print CONF $outfile;
   close CONF;
#}

print "$dirpath\n";
