#!/usr/local/bin/perl -w

#
# Read in 'students', create directories for each.
#

if (scalar(@ARGV) != 1) {
	print "Please give me the name, on the command line, of a project to create.\n";
	exit;
}

$project = $ARGV[0];

if (-e $project) {
	print("Project directory '$project' ALREADY EXISTS!\n(running anyway)\n");
}

#first create the skeleton for the project results directory:
if (!(-e $project)) {
	print("  Making project directory '$project'\n");
	system("mkdir '$project'");
}
if (!(-e "$project/www")) {
	print("  Making project web directory '$project/www'\n");
	system("mkdir '$project/www'");
}
if (!(-e "$project/code")) {
	print("  Making project code directory '$project/code'\n");
	system("mkdir '$project/code'");
}
if (!(-e "$project/www/.htaccess")) {
	print("  Creating .htaccess file.\n");
	open OUT, ">", "$project/www/.htaccess";
	print OUT <<EOF;
order deny,allow
allow from all
EOF
	close OUT;
}

#now create and link directories per-student:

$file = "student_list";

open IN, "<", $file;

foreach (<IN>) {
	s/#.*$//;
	if (!/([^\s]+)/) { next }
	$name = $1;
	$dir = "$name/$project";
	$code_dir = "$name/$project/code";
	$www_dir = "$name/$project/www";
	if (!(-e $name)) {
		print("!!! Student '$name' doesn't have a directory. Skipping.\n");
		next;
	}
	if (!(-e $dir)) {
		print("  Creating directory '$dir'.\n");
		system("mkdir '$dir'");
	}
	if (!(-e $www_dir)) {
		print("  Creating directory '$www_dir'.\n");
		system("mkdir '$www_dir'");
		system("ln -s '../../$name/$project/www' '$project/www/$name'");
	}
	if (!(-e $code_dir)) {
		print("  Creating directory '$code_dir'.\n");
		system("mkdir '$code_dir'");
		system("ln -s '../../$name/$project/code' '$project/code/$name'");
	}
	print("  Setting permissions on '$www_dir'.\n");
	system("fs sa -dir '$www_dir' -acl system:anyuser rl");
	print("  Setting permissions on '$code_dir'.\n");
	system("fs sa -dir '$code_dir' -acl system:anyuser none");
	print("  Setting permissions on '$dir'.\n");
	system("fs sa -dir '$dir' -acl $name rl");
}

close IN;
