#!/usr/local/bin/perl
# Usage: admin.pl
# Process automation script for Microsoft FrontPage Server Extensions.
#
# Copyright 1996 Microsoft Corporation -- All Rights Reserved.
#
#

$0 =~ m|(.*)/([^/]*$)|, $PROG = $2; $EXECDIR = $1; # find program name

# These are standard FrontPage Defaults
$PORT = '80';
$TYPE = 'msiis';
$CREATEUSER = 'false';
$ROOT = 'C:\\Program Files\\Microsoft FrontPage\\';
$FTPRO = 'r';

# check for options
while ($#ARGV >= 0) {
   $_ = shift @ARGV;
   $OPERATION = shift                 ,next if (/^-operation/);
   $PORT = shift                      ,next if (/^-port/);
   $TYPE = shift                      ,next if (/^-type/);
   $WEB = shift                       ,next if (/^-web/);
   $SERVCONF = shift                  ,next if (/^-servconf/);
   $MULTIHOST = shift                 ,next if (/^-multihost/);
   $USERNAME = shift                  ,next if (/^-username/);
   $PASSWORD = shift                  ,next if (/^-password/);
   $CREATEUSER = shift                ,next if (/^-createuser/);
   $GROUPS = shift                    ,next if (/^-groups/);
   $LOCALGROUPS = shift               ,next if (/^-localgroups/);
   $WWWROOT = shift                    ,next if (/^-wwwroot/);
   $FTPROOT = shift                   ,next if (/^-ftproot/);
   $FTPRW = shift                     ,next if (/^-ftprw/);


   # we only get here for -h or unknown options
   print STDERR "\nUnknown argument: $_" if (!/^-h/i);
   print STDERR <<"ENDOFHELP";

      Usage:  $PROG [-operation <install>] 
                    [-port <port>] 
                    [-type <server type>] 
                    [-web  <webname>] 
                    [-servconf <server config file>] 
                    [-multihost <hostname>] 
                    [-username <username>] 
                    [-password <password>] 
                    [-createuser <true|false>] 
                    [-groups <comma separated list list of groups>] 
                    [-localgroups <comma separated list list of local groups>] 
                    [-wwwroot <folder>] 
                    [-ftproot <folder>] 
                    [-ftprw <rw>] 
                    [-h for help] 
ENDOFHELP
    exit 1;
}

# Add user account if necessary

if ($CREATEUSER eq 'true')
{
    &step("Creating user account.");
    if ($USERNAME ne '' && $PASSWORD ne '')
    {
        system("net user $USERNAME $PASSWORD /ADD");
    }

    if ($USERNAME ne '' && $GROUPS ne '')
    {
        &step("Adding user to groups.");
        foreach $grp ($GROUPS)
        {
            system("net group $grp $USERNAME /ADD");
        }
    }

    if ($USERNAME ne '' && $LOCALGROUPS ne '')
    {
        &step("Adding user to local groups.");
        foreach $grp ($LOCALGROUPS)
        {
            system("net group $grp $USERNAME /ADD");
        }
    }
}

# Create document root
if ($WWWROOT ne '' && ! -f $WWWROOT)
{
    &step("Creating folder.");
    system("mkdir $WWWROOT");
}

# Create WWW virtual server
if ($WWWROOT ne '')
{
    &step("Creating WWW virtual server.");
    if ($MULTIHOST ne '')
    {
        system("iisadmin.exe -o add -s www -u / -d $WWWROOT -v $MULTIHOST");
    }
    else
    {
        system("iisadmin.exe -o add -s www -u / -d $WWWROOT");
    }
}
        
# Create FTP virtual server
if ($FTPROOT ne '')
{
    &step("Creating FTP virtual server.");

    system("mkdir $FTPROOT") if ( ! -f $FTPROOT);

    $R = '';
    $W = '';
    
    $R = '-read true' if (index($FTRPW,'r') ne $[-1);
    $W = '-write true' if (index($FTRPW,'w') ne $[-1);

    if ($MULTIHOST ne '')
    {
        system("iisadmin.exe -o add -s ftp -u / -d $FTPROOT -v $MULTIHOST $R $W");
    }
    else
    {
        system("iisadmin.exe -o add -s www -u / -d $FTPROOT $R $W");
    }
}
        

# Install FP against virtual server
if ($OPERATION eq 'install')
{
    &step("Installing FrontPage Server Extensions.");
    $cmd = 'fpsrvadm.exe -o install';
    $cmd .= " -m $MULTIHOST" if ($MULTIHOST ne '');
    $cmd .= " -p $PORT" if ($PORT ne '');
    $cmd .= " -t $TYPE" if ($TYPE ne '');
    $cmd .= " -w $WEB" if ($WEB ne '');
    $cmd .= " -s $SERVCONF" if ($SERVCONF ne '');
    $cmd .= " -u $USERNAME" if ($USERNAME ne '');
    $cmd .= " -pw $PASSWORD" if ($PASSWORD ne '');
    system($cmd);
}

exit 0;

#  Helper subroutines

# status message
sub step {
    local($message) = @_;
    printf "Step %2d: $message\n", ++$step;
}

# status message
sub note {
    local($message) = @_;
    print "         $message\n";
}








