#!/usr/local/bin/perl
use IO::Handle;

$TMCC="/usr/local/etc/emulab/tmcc";
$SHARE="USERS";
$PUSER="jhickey";
$ADDR=`cat /usr/local/etc/emulab/bossnode`;

chomp $ADDR;
print $ADDR;

#
# Do SMB Mounts
#
#system("umount -A -f -t nfs,smbfs"); # or die("Failed to unmount NFS");

open(TM, "$TMCC -s $ADDR accounts windows |") or die("Failed to execute TMCC");

# create a smb config to set  autoflush for smb config
open(CONFIG, ">/etc/nsmb.conf") or die("Failed to open /etc/nsmb.conf");
CONFIG->autoflush(1);

print CONFIG "[$SHARE]\naddr=$ADDR\n\n";

while(<TM>) {
        if (/ADDUSER LOGIN=(\S+) PSWD=(\S+) UID=(\d+) GID=(\d+).*HOMEDIR=(\S+) / ) {
                $user = $1;
                $pswd = $2;
                $uid  = $3;
                $gid  = $4;
                $homedir = $5;

                $user =~  tr/a-z/A-Z/;
                my $pass = `smbutil crypt '$pswd'`;

                print CONFIG "[$SHARE:$user]\npassword=$pass\n";
                print "mount_smbfs -N -f 744 -d 755 -u $uid -g $gid //$user\@$SHARE/$user $homedir\n";
                system("mount_smbfs -N -f 744 -d 755 -u $uid -g $gid //$user\@$SHARE/$user $homedir");

		if($user =~ m/$PUSER/i) {
			print "mount_smbfs -N -f 774 -d 755 -u $uid -g $gid //$user\@$SHARE/share /share\n";
			system("mount_smbfs -N -f 774 -d 755 -u $uid -g $gid //$user\@$SHARE/share /share");
			open(MOUNTS, "$TMCC -s $ADDR mounts |") or die("Failed to execute TMCC");
			while(<MOUNTS>) {
				if(/LOCAL=\/proj\/(\S+)/) {
					print "mount_smbfs -N -f 774 -d 755 -u $uid -g $gid //$user\@$SHARE/proj-$1 /proj/$1\n";
					system("mount_smbfs -N -f 774 -d 755 -u $uid -g $gid //$user\@$SHARE/proj-$1 /proj/$1");
				}
			}
		}
        }
}

close(CONFIG);

