#!/usr/bin/perl
#
# Copyright (C) 2001-2003 Caldera International, Inc.  All Rights Reserved.
#
# genclientconfig
#
# generate vbscript code which will be downloaded to the client to
# configure Outlook Express, Outlook, install Outlook Com-Addin,
# and create desktop shortcut to the GUI.
# 
#

$DEBUG	= 0;				# must be first statement

BEGIN {
	use File::Basename();
	$newdir = File::Basename::dirname($0);
	unshift (@INC, "$newdir");
}

sub SetenvUnixCharset {
	$lang =  $ENV{"LANG"};
   	@vals =  split(/\./,$lang);
   	$lang = $vals[0] . ".ISO-8859-1";
	if ( $lang =~ /^(pl|cs|sk)/ ) { 
	     # Polish, Czech, Slovak
   	     $lang = $vals[0] . ".ISO-8859-2";
	}
	$ENV{"LANG"} = $lang;
}
sub SetenvWindowsCodeset {
	$lang = $ENV{"LANG"};
   	@vals = split(/\./,$lang);
   	$lang = $vals[0] . ".CP1252";
	if ( $lang =~ /^(pl|cs|sk)/ ) { 
	     # Polish, Czech, Slovak
   	     $lang = $vals[0] . ".CP1250";
	}
	$ENV{"LANG"} = $lang;
}

SetenvUnixCharset(); # call before using msglib which calls msgi18ninit()
use 		msglib;
 
sub Usage {
  	print STDERR msgi18nmsg(1); # usage message
        exit(1);
}
 
#
# MAIN Main main
#

# flags on what to generate
$pop3		= 0;
$imap		= 1;
$omi		= 1;
$ldap		= 1;

# item values
$uid		= "";
$displayname	= "";
$emailaddress	= "";
$emailname   	= "";
$pop3server	= "";
$imapserver	= "";
$smtpserver	= "";
$ldapserver	= "";
$userdn		= "";
$httppreferences = "";
$httpmail	= "";

msgi18ninit(22); # set 22 is msggenclientconfig
 
# parse the command line arguments
$argc = @ARGV;
if($argc != 1 ) { Usage };                      # must supply uid

$uid = $ARGV[0];
chomp($uid);
argcheck("uid", $uid);

# go get our ldap strings for lookups
parse_conf;


$ldap   = Net::LDAP->new($ldaphost) || die msgi18nmsg(1) . "$@\r\n";
$result = $ldap->bind("$msgadmindn", password => "$msgadminpw", version => 3);
if ( $result->code ) {
  print getLDAPerrorString("bind",$result) . "\r\n";
  exit(1);
}

$searchAttr = "uid";
$searchVal  = $uid;
@attrlist = (dn, cn, mail);

# search LDAP for the searchAttr (mail or uid)
$result = $ldap->search (
	base   => $ldapsuffix,
	scope  => "sub",
        filter => fbuild("(&(objectclass=msgUser)(uid=%1))", $uid),   
	attrs  => \@attrlist,
);

# error?
if ( $result->code ) {
  print getLDAPerrorString("search",$result) . "\r\n";
  $ldap->unbind;
  exit(1);
}
if ( $result->count == 0 ) {
	print STDERR msgi18nmsg(3); # User $uid not found\\r\n
	$ldap->unbind;
	exit(1);
}
if ($DEBUG) { print "number of entries = " . $result->count . "\r\n" };

# gather results
foreach $entry ($result->all_entries) {
	$userdn = $entry->dn;
	$displayname = $entry->get_value("cn");
	$emailaddress = $entry->get_value("mail");
}

# close ldap
$ldap->unbind;

# set up all of our config parameters

$iphost		= hostname() . "." . hostdomain();
$pop3server	= $iphost;
$imapserver	= $iphost;
$smtpserver	= $iphost;
$fbserver	= $iphost;
$ftpserver	= $iphost;
$ldapserver	= $ldaphost;

if ($ldapserver eq "localhost") { $ldapserver	= $iphost; }

@tmp = split('@', $emailaddress);
$emailname = $tmp[0];

$preferencesurl = "https://$iphost/msg/pg.php/preferences.php";
$httpmail       = "https://$iphost/imp";

# configure connection to mail servers with SSL? default is no (0)
# incoming $msgsecure values are  0, 1, no, yes, true, false
# outgoing $msgsecure values are  0, 1
# values originate in the msg.conf file and are parsed by msglib.pm
# the values are interepreted here for use in the client config script
if    ($msgsecure =~ /^no/i) 	{ $msgsecure = 0; } 
elsif ($msgsecure =~ /^yes/i) 	{ $msgsecure = 1; } 
elsif ($msgsecure =~ /^off/i) 	{ $msgsecure = 0; } 
elsif ($msgsecure =~ /^on/i) 	{ $msgsecure = 1; } 
elsif ($msgsecure =~ /^true/i)  { $msgsecure = 1; } 
elsif ($msgsecure =~ /^false/i) { $msgsecure = 0; } 
elsif (($msgsecure ne "0") && ($msgsecure ne "1"))  { $msgsecure = 0; }

# do we configure IMAP or POP3 or EITHER type mailboxes? default is IMAP
# incoming $msgmboxtype values are  imap, pop3, either
# outgoing $msgmboxtype values are  imap, pop3, either
# values orginate in the msg.conf file and are parsed by msglib.pm
# the values are interepreted here for use in the client config script
if    ($msgoutlkmboxtype =~ /^imap/i)    { $msgoutlkmboxtype = "imap";   } 
elsif ($msgoutlkmboxtype =~ /^pop/i)     { $msgoutlkmboxtype = "pop3";   } 
elsif ($msgoutlkmboxtype =~ /^either/i)  { $msgoutlkmboxtype = "either"; } 
else 				         { $msgoutlkmboxtype = "imap";   }

# pass through a user to use for free/busy calendar publication
# this user authenticates with ldap but is not a real mail user
# and can not login through the gui
if ($msguser eq "" ) { $msguser = "msguser"; }
if ($msgpw   eq "" ) { $msgpw   = "pwd_goes_here"; }

# If the outlook connector filename is defined in msg.conf
# verify that it exists in ~ftp/pub/clientconfig so that it can
# be downloaded by msgclientconfig script. If it does not exist then
# set $msgctoc to "" to indicate that the connector is
# not available.
if ($msgctoc != "") {
   ($ftp_home) = (getpwnam("ftp"))[7];
   $ctoc_pathname = $ftp_home . "/pub/clientconfig/" . $msgctoc;
   if ((! -e $ctoc_pathname)) { $msgctoc = ""; }
}

# sort out the freebusy information.  depending on which server we
# are on, we have to handle this differently.
if ( $freebusypw eq "" ) {
	$freebusy   = "$uid";
	$freebusypw = "password_for_$uid";
} else {
	$freebusy  = "freebusy";
}

# create the configscript.vbs file that will be downloaded to the
# windows system and run to configure the system.

# the file to which we pre-pend our generated code
open(VBFD, $msgvaropt . "/msgconfigscript") || die msgii18nmsg(4, "msgconfigscript") . "$@\r\n";

# Set Windows CodeSet before spitting out Windows vbscript code
msgi18nclose();
SetenvWindowsCodeset();
msgi18ninit(22);

# generate variable declarations and do some initialization
print  '\' begin msggenclientconfig ' . "\r\n";
print  '\'' . "\r\n";
print  '\' begin generated code '     . "\r\n";
print  '\'' . "\r\n";
print  '\' Copyright (C) 2001-2003 Caldera International, Inc.  All Rights Reserved.' . "\r\n";
print  '\'' . "\r\n";
print  '\'############################################################################' . "\r\n";
print  '\'# user and server specific config data' . "\r\n";
print  '\'############################################################################' . "\r\n";
print  ' \'' . "\r\n";
print  ' Option Explicit' . "\r\n";
print  ' On Error Resume Next' . "\r\n";
print  ' \'' . "\r\n";
print  ' Dim UID'     . "\r\n";
print  ' Dim FREEBUSYUID'     . "\r\n";
print  ' Dim FREEBUSYPW'     . "\r\n";
print  ' Dim EMAILNAME' . "\r\n";
print  ' Dim DISPLAYNAME'   . "\r\n";
print  ' Dim MAILADDRESS' . "\r\n";
print  ' Dim IMAPSERVER'    . "\r\n";
print  ' Dim IMAPSECURE' . "\r\n";
print  ' Dim SMTPSERVER'    . "\r\n";
print  ' Dim SMTPSECURE' . "\r\n";
print  ' Dim LDAPSERVER'    . "\r\n";
print  ' Dim LDAPSEARCHBASE' . "\r\n";
print  ' Dim LDAPSECURE' . "\r\n";
print  ' Dim FTPSERVER' . "\r\n";
print  ' Dim GUI_URL' . "\r\n";
print  ' Dim FBSERVER' . "\r\n";
print  ' Dim PUTFREEBUSY' . "\r\n";
print  ' Dim GETFREEBUSY' . "\r\n";
print  ' Dim FREEBUSYFILE' . "\r\n";
print  ' Dim BIGICONFILENAME' . "\r\n";
print  ' Dim SHORTCUTTITLE' . "\r\n";
print  ' Dim COMADDINFILENAME' . "\r\n";
print  ' Dim OUTLKMBOXTYPE' . "\r\n";
print  ' Dim CTOCINSTALLER' . "\r\n";
print  ' Dim CRLF' . "\r\n";
print  ' \'' . "\r\n";
print  ' CRLF             = chr(13) & chr(10)' .  "\r\n";
print  ' UID              = "' . $uid . '"' . "\r\n";
print  ' FREEBUSYUID      = "' . $freebusy . '"' . "\r\n";
print  ' FREEBUSYPW       = "' . $freebusypw . '"' . "\r\n";
print  ' EMAILNAME        = "' . $emailname . '"' . "\r\n";
print  ' DISPLAYNAME      = "' . msgi18n_tx_from_utf8($displayname) . '"' . "\r\n";
print  ' MAILADDRESS      = "' . $emailaddress . '"' . "\r\n";
print  ' OUTLKMBOXTYPE    = "' . $msgoutlkmboxtype . '"' . "\r\n";
print  ' CTOCINSTALLER    = "' . $msgctoc . '"' . "\r\n";
print  ' IMAPSERVER       = "' . $imapserver . '"' . "\r\n";
print  ' IMAPSECURE       = "' . $msgsecure . '"'  . "\r\n";
print  ' SMTPSERVER       = "' . $smtpserver . '"' . "\r\n";
print  ' SMTPSECURE       = "' . $msgsecure . '"'  . "\r\n";
print  ' LDAPSERVER       = "' . $ldapserver . '"' . "\r\n";
print  ' LDAPSEARCHBASE   = "' . $ldapsuffix . '"' . "\r\n";
print  ' LDAPSECURE       = "' . $msgsecure . '"'  . "\r\n";
print  ' GUI_URL          = "' . $preferencesurl . '"' . "\r\n";
print  ' FTPSERVER        = "' . $ftpserver . '"' . "\r\n";
print  ' FBSERVER         = "' . $fbserver  . '"' . "\r\n";
print  ' PUTFREEBUSY      = "http://' . $fbserver .  '/pub/calendar/"' . "\r\n";
print  ' GETFREEBUSY      = "ftp://' . $fbserver . 
                            '/pub/calendar/%NAME%.vfb"' . "\r\n";
print  ' FREEBUSYFILE     = "' . $emailname . ".vfb" . '"' . "\r\n";
print  ' BIGICONFILENAME  = "msg32.ico"' . "\r\n";
print  ' COMADDINFILENAME = "setup.exe"' . "\r\n";
print  " SHORTCUTTITLE    = \"$msgproduct Preferences\" \r\n";

print  ' \'' . "\r\n";

# Generate an array of localized messages (msgutil.cat)
# start at message 10 in set 22
$i    = 10;
$data = "";
while (msgi18nmsgck($i) ne "") {
  $vbtext  = msgi18nmsg($i); 
  $vbindex = $i - 10;
  $data   .=  " STRMSGS($vbindex) = $vbtext\r\n";
  $i += 1;
}
$num_messages = $vbindex + 1;
print  ' Dim STRMSGS(' . $num_messages . ')' . "\r\n";
print  $data;

# indicate the end of the autogenerated code
print  '\'' . "\r\n";
print  '\' end generated code' . "\r\n";
print  '\'' . "\r\n";

# now append the remainder of the msgconfigscript file
foreach(<VBFD>) { 
	print $_ 
}
close(FD);

# note end of the script
print  '\'' . "\r\n";
print  '\' end msggenclientconfig ' . "\r\n";
print  '\'' . "\r\n";

exit(0);
