#!/usr/bin/eperl -w
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
# Exit codes (on failure error message goes to stderr):
#  0 - success
#  1 - failure
# 
# Boolean arguments on the command line are given as 0 or 1. All the
# command line arguments are encoded to avoid problems with escapes.
#
# All functions defined here either return an error message if an
# error occured and "" if everything went well or allways return a
# valid value, but exit (with code 1) printing an error message if an
# error occurs. This second type of functions have _e appended to
# their name. Functions are allowed to print results onto stdout, but
# errors are printed only in the main program.
#

use strict;
push @INC, ($ENV{'OCW_SVCPATH'} || "/usr/lib/opcenter")."/cmdline_common";
if ($0 =~ /^(.+)\/([^\/]+)$/) {
  push @INC, $1; #the directory where the script resides
}
require CmdLineCoder;
require VHConfiguration;
require VHConst;
require VHCommon;
require VHQuota;

use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/virtualhosting";
use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/cmdline_common";

use ERRORS;
use Carper;

$0 =~ /([^\/]+)$/;

#unescape the command line arguments
&CmdLineCoder::decode_args();

# 
# This function returns information about a virtual user from a
# virtual domain. Its arguments are the domain name and the user
# name. It returns the flags (email and ftp) followed by the full
# name, using a single space as a separator.
#
# GetInfoVirtUser vx.com tina
#
my $error_message = &GetInfoVirtUser;

if ($error_message) {
  print STDERR $error_message;
  exit 1;
}
exit 0;

#
# Main function for geting information about a virtual user
#
sub GetInfoVirtUser {
  my ($Domain,$User);
  #Process the command line arguments
  if ($#ARGV != 1) {
    $cerr += $E_ARGS ;
    return "The number of arguments is wrong.\n";
  }

  $Domain = $ARGV[0];
  $User = $ARGV[1];

  my ($error_message);

  #Check the domain name. 
  if ($error_message = &VHConfiguration::CheckDomain($Domain)) {
      return $error_message;
  }
  #Check the user name
  if ($error_message = &VHConfiguration::CheckUser($User)) {
      return $error_message;
  }

  #Get the domain info from the conf file
  my ($record);
  if ($error_message = &VHConfiguration::GetDomainConf($Domain,\$record))
    {return $error_message;}

  my $vroot = VHCommon::GetVRoot($record) ;

  unless(open (PASSWD, "$vroot/etc/passwd")) {
    $cerr += $E_UNABLEREADVIRTPWDFILE and $cerr += { vroot => $vroot } ;
    return "Cannot read virtual password file $vroot/etc/passwd .\n";
  }
  while (<PASSWD>) {
    if ($_ =~ /^$User:/) {
      my ($passwdline)=$_;
      my ($used,$quota);
      if (not ref ($error_message=&VHQuota::GetUsageVirtUser($User,$record))) {
	  print STDERR $error_message;
	  ($used,$quota)=("","");
      } else {
	  ($used,$quota)=@{$error_message};
      }
      print &VHCommon::FormatVirtUser($record->{User},$record->{Telnet},$record->{FTP},$record->{DomainType},$passwdline,$used,$quota, $record->{User});
      return "";
    }
  }
  close (PASSWD);

    $cerr += $E_USERNOTINSITE and $cerr += { user => $User, domain => $Domain};
  return "User $User not found in site $Domain .\n";
}
