#!/usr/local/bin/perl -Tw
# -*- Perl -*- Thu Oct 11 13:47:15 CDT 2001
###############################################################################
# Written by Tim Skirvin <tskirvin@ks.uiuc.edu>
# Copyright 2000-2001, Tim Skirvin and UIUC Board of Trustees.
# Redistribution terms are below.
###############################################################################

use vars qw( $VERSION $DEBUG $TEST );
$VERSION = "1.0";

###############################################################################
### Default Configuration #####################################################
###############################################################################

$DEBUG = 0; 	    ## Print debugging information?  Can also be set with '-V'
$TEST  = 0;	    ## Just test the script?  Can also be set with '-t'

###############################################################################
### main() ####################################################################
###############################################################################

use strict;				# With perl
use Getopt::Std;			# With perl
use TCB::AddUser "prompt_password";	# Stand-alone module
use vars qw( $opt_h $opt_v $opt_V $opt_t );
delete $ENV{PATH};

$0 =~ s%.*/%%;			# Shorten program name
getopts('hvVt');

$DEBUG++  if $opt_V;		# Verbose mode - print debugging information
$TEST++   if $opt_t;		# Testing mode - don't actually change PW's
Version() if $opt_v;
Usage()   if $opt_h;

$TCB::AddUser::DEBUG = 1 if $DEBUG;
$TCB::AddUser::TEST  = 1 if $TEST;

### Get username
my $username = (getpwuid($<))[0];
warn "Updating for username: '$username'\n" if $DEBUG;
Exit('No username') unless $username;

### Set web password
warn "Setting web password for '$username'\n" if $DEBUG;
print "Make sure this is different than your main password\n";
TCB::AddUser->set_htpasswd( $username, prompt_password("New web password", 0) )
	or warn "Failed to set web password\n";

exit 0;

###############################################################################
### Internal Functions ########################################################
###############################################################################

### Usage ()
# Prints out a short help file and exits.
sub Usage { 
  print <<EOM;
$0 v$VERSION
Web password setting utility.
Usage: $0 [-hv] [-Vt] [-ysw|a] [username]

$0 sets your web password.  

	-h	Prints this message and exits
	-v	Prints out the version number and exits
	-V	Verbose.  Prints out debugging information. 
	-t	Test mode.  Don't actually set the passwords, just prompt
		for them.
EOM

  Exit();
}

### Version ()
# Prints out the version number and exits
sub Version { Exit("$0 v$VERSION"); }

### Exit ( REASON )
# Exits the program cleanly with a warned error message
sub Exit { warn "@_\n" if (scalar @_);  exit; }

=head1 NAME

webpasswd - a script for modifying the Group web password

=head1 SYNOPSIS

  webpasswd [-hvVt] 

=head1 DESCRIPTION

webpasswd sets the web password for the running user.  

        -h      Prints this message and exits
        -v      Prints out the version number and exits
        -V      Verbose.  Prints out debugging information.
        -t      Test mode.  Don't actually set the passwords, just 
		prompt for them.

=head1 REQUIREMENTS

Getopt::Std, TCB::AddUser 

=head1 SEE ALSO

B<TCB::AddUser>, B<passwd.pl>

=head1 AUTHOR

Tim Skirvin <tskirvin@ks.uiuc.edu>

=head1 LICENSE

Currently for internal TCB use only - and, honestly, that's probably where
it's going to stay.  Code snippets may be used wherever you'd like, however.

=head1 COPYRIGHT

Copyright 2001 by the University of Illinois Board of Trustees and Tim
Skirvin <tskirvin@ks.uiuc.edu>.

=cut


##### Version History 
### v1.0 	Thu Oct 11 13:46:37 CDT 2001
# Commented, included as part of TCB::AddUser
### v1.01	Thu Oct 11 14:27:45 CDT 2001
# Since we want this to run 'setgid', deleted $ENV{PATH}
### v1.02	Thu Apr 22 14:25:35 CDT 2004 
# Now using TCB::AddUser instead.
