#! /bin/sh

# the following is magic to allow perl to run with multiple locations of
# perl.  It only works with perls that have the -x switch, however!
exec `which perl` -x $0 ${1+"$@"}
exit
#!perl

# ====================================================================
# The Vovida Software License, Version 1.0 
# 
# Copyright (c) 2003 Vovida Networks, Inc.  All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 
# 3. The names "VOCAL", "Vovida Open Communication Application Library",
#    and "Vovida Open Communication Application Library (VOCAL)" must
#    not be used to endorse or promote products derived from this
#    software without prior written permission. For written
#    permission, please contact vocal@vovida.org.
# 
# 4. Products derived from this software may not be called "VOCAL", nor
#    may "VOCAL" appear in their name, without prior written
#    permission of Vovida Networks, Inc.
# 
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
# NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
# NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
# IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
# 
# ====================================================================
# 
# This software consists of voluntary contributions made by Vovida
# Networks, Inc. and many individuals on behalf of Vovida Networks,
# Inc.  For more information on Vovida Networks, Inc., please see
# <http://www.vovida.org/>.

# $Id: manageusers,v 1.1.2.2 2003/02/28 20:27:38 bko Exp $ 

require "getopts.pl";

&Getopts("L:cdp:hqQP");

if($opt_L) {
    push(@INC, $opt_L);
}

use lib "/opt/vocal/share/webpages";
require vprov::vpp;
import vprov::vpp;
require vprov::ui;
import vprov::ui;
require provactions;
import provactions;

if($opt_p) {
    $pserver = $opt_p;
} else {
    $pserver = "localhost";
}

if($#ARGV < 0 || $opt_h) {
    &usage();
}

sub usage() {
    print <<EOUSAGE
Usage: 
    Create user(s):      $0 -c user...
    Delete user(s):      $0 -d user...
    Print user(s) data:  $0 -P user...

Options:
    -p        set pserver host (defaults to localhost)
    -q        quiet mode (do not print to stdout unless of error)
    -Q        veryquiet mode (do not print at all)
EOUSAGE
  ;
    exit();
}

if(!$password) {
    print "Password: " if(!$opt_q);
    system "stty -echo" if(!$opt_q);
    chomp($password = <STDIN>);
    print "\n" if(!$opt_q);
    system "stty echo" if(!$opt_q);
}

($result, $output) = &u_login("vocal", $password, "localhost", "6005");

if(!$result) {
    print "$output\n" if(!$opt_Q);
    exit -1;
} else {
    #print "ok!\n";
}

$bad = 0;

foreach $user (@ARGV) {
    if($opt_c) {
	($result, $output) = &u_create_user($user, 1);
	if(!$result) {
	    print "creation failed: $output\n" if(!$opt_Q);
	    $bad = 1;
	}
    }
    
    if($opt_d) {
	($result, $output) = &u_delete_user($user);
	if(!$result) {
	    print "deletion failed: $output\n" if(!$opt_Q);
	    $bad = 1;
	}
    }

    if($opt_P) {
	($result, $output) = &u_print_user($user);
	if(!$result) {
	    print "could not print user $user: $output\n" if (!$opt_Q);
	} else {
	    print $output, "\n";
	}
    }
}

exit $bad;


### Local Variables: ###
### mode: cperl ###
### End: ###
