#!/usr/bin/perl
# cuonly.pl:  Restricts access to web pages.

foreach (@ARGV){			#set remove flag
  if (m/^-/){
      if (m/--remove|-R/) {$remove=1;}
      else {@usage;}
      next;
      }
   else {@argv= (@argv,$_);}
}
@ARGV=@argv;

sub usage{
print <<EOT;
Usage: 1 arguments:   webaccess.pl dir
Where dir is the (relative path) directory that will have restricted access. 
Program writes/overwrites the file .htaccess
Usage: 1 argument and -R:   webaccess.pl -R dir
Removes all Web access files from dir. Allows for unrestricted browsing.
EOT
exit;
}

$dir=$ARGV[0];

use Cwd;					#Camel 386!  get pwd PWD
$path= cwd();


$htaccess= "$path/$dir/.htaccess";


####### MAIN STUFF STARTS HERE ########
if ($remove){
  $direct= "$path/$dir";
  if (!-e $direct){&nodir;}
  unlink($htaccess,$htpasswd,$htgroup) || &no_unlink;
  exit;
}

# MAKES .htaccess
open(OUT, ">$htaccess") || die("Can't open file $htaccess for write: $!");

print OUT <<EOT;
AuthName CUonly
AuthType Basic

<Limit GET POST>
Order Deny,Allow
Deny from all
Allow from clarkson.edu
</Limit>
EOT
;

close(OUT);
chmod(0664,$htaccess);


############# END MAIN ##################

sub nodir{
print <<EOT;
Directory $dir doesn't exist.
EOT
}

sub no_unlink{
print "Couldn't remove files: $!\n";
}
