#!/usr/bin/perl

#############################################################################
# smbmounts.pl: Setup Automounter to mount via SMB for federated experiments
# $Id: smbmount.FreeBSD.pl,v 1.1 2008-05-18 00:09:31 faber Exp $
#

$SHARE=shift || "USERS";
$ADDR=shift || `cat /usr/local/etc/emulab/bossnode`;
$PUSER=shift || "jhickey";
$PNAME=shift || "emulab-ops";
$FSTAB="/etc/fstab";
$HOMEROOT="/users";
$PROJROOT="/proj";
$AMDROOT="/auto";
$AMDMAP="/etc/amd.users";
$PAMDMAP="/etc/amd.proj";

chomp $ADDR;

$TMCC="/usr/local/etc/emulab/tmcc";
$UMOUNT="/sbin/umount";
$MOUNT="/sbin/mount";
$MKDIR="/bin/mkdir";
$CP="/bin/cp";
$KILLALL="/usr/bin/killall";
$AMD="/usr/sbin/amd";

my $share = 0;

print "Killing amd.\n";
system("$KILLALL amd");

print "Unmounting all nfs and smb filesystems.\n";
system("$UMOUNT -A -f -t nfs,smbfs"); # or die("Failed to unmount NFS");

# Backup the fstab so we can run multiple times
if (! -f "$FSTAB.bak") {
	system("$CP $FSTAB $FSTAB.bak") && die("Unable to backup $FSTAB");
} else {
	system("$CP $FSTAB.bak $FSTAB") && die("Unable to restore $FSTAB");
}

# open(TM, "$TMCC accounts windows |") or die("Failed to execute TMCC");
open(TM, "/usr/local/federation/etc/userconf")or die("Failed to open userconf");
open(CONFIG, ">/etc/nsmb.conf") or die("Failed to open /etc/nsmb.conf");
open(MAP, ">$AMDMAP") or die("Failed to open $AMDMAP");
open(PMAP, ">$PAMDMAP") or die("Failed to open $PAMDMAP");
open(FSTAB,">>$FSTAB") or die("Cannot Open File $FSTAB");

print FSTAB "\n# SMB Configuration Generated by smbmount.pl\n";
print MAP "# AMD Configuration Generated by smbmount.pl\n";
print CONFIG "# SMB Configuration Generated by smbmount.pl\n\n";
print CONFIG "[$SHARE]\naddr=$ADDR\n\n";

while(<TM>) {
    /ADDUSER/ && do {
	/LOGIN=(\S+)/ && do { $user = $1; };
	/WPSWD=(\S+)/ && do { $pswd = $1; };
	/UID=(\S+)/ && do { $uid = $1; };
	/GID=(\S+)/ && do { $gid = $1; };
	/HOMEDIR=(\S+)/ && do { $homedir = $1; };

	$userlc = $user;
	# SMB demands upper case.
	$user =~  tr/a-z/A-Z/;
	my $pass = `smbutil crypt '$pswd'`;

	print CONFIG "[$SHARE:$user]\npassword=$pass\n";
	
	if(! -d "$AMDROOT$homedir") {
		system("$MKDIR -p $AMDROOT$homedir") && 
		    die("Failed to make directory");
	}

	print FSTAB "//$user\@$SHARE/$user\t";
	print FSTAB "$AMDROOT$homedir\tsmbfs\t";
	print FSTAB "noauto,rw,-N,-f744,-d755,-u$uid,-g$gid\t0\t0\n";

	print MAP "\n$userlc type:=program;fs:=$AMDROOT$homedir;\\\n";
	print MAP "mount:=\"$MOUNT mount $AMDROOT$homedir\";\\\n";
	print MAP "unmount:=\"$UMOUNT unmount $AMDROOT$homedir\";";

	#
	# If we are PUSER, the user assigned to mount the project directory,
	# then we add in the mount for that.  The share is called proj-$PNAME
	# where PNAME is the name of the project at the mothership.  We need
	# a second map for this since we will be mounting under /proj.

	if($user =~ m/$PUSER/i) {
	    print FSTAB "//$user\@$SHARE/proj-$PNAME\t";
	    print FSTAB "$AMDROOT/proj\tsmbfs\t";
	    print FSTAB "noauto,rw,-N,-f774,-d775,-u$uid,-g$gid\t0\t0\n";

	    print PMAP "$PNAME type:=program;fs:=$AMDROOT/proj;\\\n";
	    print PMAP "mount:=\"$MOUNT mount $AMDROOT/proj\";\\\n";
	    print PMAP "unmount:=\"$UMOUNT unmount $AMDROOT/proj\";\n";

	    if(! -d "$AMDROOT/proj") {
		    system("$MKDIR -p $AMDROOT/proj") && 
			die("Failed to make directory");
	    }
	    if ( $share) {
		print FSTAB "//$user\@$SHARE/share\t";
		print FSTAB "$AMDROOT/share\tsmbfs\t";
		print FSTAB "noauto,rw,-N,-f774,-d775,-u$uid,-g$gid\t0\t0\n";

		print PMAP "$PNAME type:=program;fs:=$AMDROOT/share\\\n";
		print PMAP "mount:=\"$MOUNT mount $AMDROOT/share\";\\\n";
		print PMAP "unmount:=\"$UMOUNT unmount $AMDROOT/share\";\n";

		if(! -d "$AMDROOT/share") {
			system("$MKDIR -p $AMDROOT/share") && 
			    die("Failed to make directory");
		}
	    }
	}
    }
}

close(CONFIG);
close(FSTAB);
close(MAP);
close(PMAP);

print "Starting the automounter: $AMD -l syslog -a $AMDROOT $HOMEROOT $AMDMAP $PROJROOT $PAMDMAP\n";
system("$AMD -l syslog -a $AMDROOT $HOMEROOT $AMDMAP $PROJROOT $PAMDMAP") && die("Unable to start amd");
