#! /bin/sh
#
# Copyright 1998 Microsoft Corporation -- All Rights Reserved.
#
# $Revision: 1.1 $
# $Date: 1998/12/02 21:58:21 $
#


step1()
{
 echo 
 echo "Step 1.  Setting Up Installation Environment" 
 echo 

 checkuser || error                 # Check to make sure we are root
 banner
 usrlocalexists || error            # Check to make sure that /usr/local exists
 checkforaccess || error            # Check for RWX access to /usr/local
 wheretoinstall || error            # Find out where to install the extensions

}

step2()
{
 echo 
 echo "Step 2.  Untarring the Extensions and Checking Protections" 
 echo 
 
 untarext   || error                # Untar the extensions

}

step3()
{
 echo  echo "Step 3.  Upgrading/Installing the extensions" 
 echo 
 
 upgradeexistingservers || error    # Check to see if servers need upgrading
 chownexistingservers || error     
 installrootweb || error            # Install the root web
 installnewsubwebs $PORT || error   # Install new servers
 installvirtualwebs || error        # Install any virtual webs

}

initialize()
{
  VERSION="4.0"
  PATH=".:/bin:/usr/bin:/sbin:/usr/sbin:/usr/ucb:/etc:/usr/bsd"
  INSTALLDIRDEFAULT="/usr/local/frontpage"

case "`echo 'x\c'`" in
   'x\c')   echo="echo -n"    nnl= ;;      #BSD
   x)       echo="echo"       nnl="\c" ;;  # Sys V
   *)       echo "$0 quitting:  Can't set up echo." ; exit 1 ;;
esac

 TAB="	"
 system=`uname -a`

 case "$system" in
    OSF1*)              machine="alpha" ;;
    Linux*mips*)        machine="mips-linux" ;;
    Linux*)             machine="linux" ;;
    HP-UX*)             machine="hp700" ;;
    AIX*)               machine="rs6000" ;;
    IRIX*)              machine="sgi" ;;
    SunOS*5.*sun4*)     machine="solaris" ;;
    SunOS*5.*i386*)     machine="solarisx86" ;;
    BSD/OS*)            machine="bsdi" ;;
    SCO_SV*)            machine="sco5" ;;
    FreeBSD*)		machine="freebsd" ;;
    UnixWare\ *\ *\ 7*\ i*)   machine="uware7" ;;
    *)                  echo "ERROR:  Unsupported platform!  Uname is $system."
                        return 1
                         ;;
 esac

 case "$machine" in 
   sunos) functions="function toupper(CHAR) {
                     UPPER = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";
                     LOWER = \"abcdefghijklmnopqrstuvwxyz\";
                     if (POS = index(LOWER,CHAR))
                         RETVAL = substr(UPPER,POS,1);
                     else 
                         RETVAL = CHAR;

                     return RETVAL 

                     }

                     function tolower(CHAR) {
                     UPPER = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";
                     LOWER = \"abcdefghijklmnopqrstuvwxyz\";
                     if (POS = index(UPPER,CHAR))
                        RETVAL = substr(LOWER,POS,1);
                     else 
                        RETVAL = CHAR;

                     return RETVAL 

                     }"
          ;;
       *) functions="" 
          ;;
 esac    

 case "$machine" in
       solaris) awk=nawk 
                lsg="ls -ld"
                ;;
    solarisx86) awk=nawk
                lsg="ls -ld"
                ;;
           sgi) awk=nawk 
                lsg="ls -ld"
                ;;
        uware7) awk=nawk 
                lsg="ls -ld"
                ;;
             *) awk=awk
                lsg="ls -ld"
                ;;
 esac

}

checkuser()
{
 #
 # Make sure we are not running as root.
 #

whoami=`whoami 2>/dev/null` || whoami=`/usr/bin/id | sed -e ' s/).*//; s/^.*(//;'`
 retval=0
 
 echo "Setting umask 002" 
 umask 002

 if [ $whoami != "root" ]
 then
    echo "ERROR:  Logged in as $whoami.  Must be root to do this installation!"
    retval=1
 else
    echo "Logged in as root." 
 fi

 return $retval

}

banner()
{
cat <<EOF


 fp_install.sh

 Revision$Revision: 1.1 $dummy
 Date$Date: 1998/12/02 21:58:21 $dummy

This script will step the user through upgrading existing and installing 
new servers and webs.  As with any software installation, a backup should be
done before continuing.  It is recommended that the FrontPage installation
directory, server configuration file directory, and all web content be 
backed up before continuing with this installation.

EOF

 myprompt 'yYnN' "Are you satisfied with your backup of the system (y/n)" "N"
 if [ $answer = n ] || [ $answer = N ]
 then
    exit 0
 fi
 echo

 return 0
}

myprompt()
{
 answer="|"
 until echo $1 | grep $answer >/dev/null
 do
    $echo "${2} [${3}]?  ${nnl}" 
    read answer
    if [ "$3" != "" ] && [ "$answer" = "" ]
    then
       answer=$3
    fi
 done
}

getpassword()
{
 prompt=$1
 PASSWORD=""
 until [ "$PASSWORD" != "" ]
 do
    $echo "$prompt:  ${nnl}" 
    stty -echo
    read password1
    stty echo
    echo
    $echo "Re-enter $prompt:  ${nnl}" 
    stty -echo
    read password2
    stty echo
    echo 
    if [ "$password1" = "$password2" ]
    then
        PASSWORD=$password1
    fi
 done

 return 1
}

usrlocalexists()
{
 #
 # Now lets make sure that /usr/local exists.
 #

 localdir="/usr/local"
 prot=755
 retval=0

 if [ ! -d "$localdir" ]
 then
    myprompt 'yYnN' "$localdir does not exist.  Would you like to create it (y/n)" "Y"
    if [ $answer = y ] || [ $answer = Y ]
    then
       echo "Creating $localdir" 
       if mkdir "$localdir"
       then
          echo "Directory $localdir has been created." 
          if chmod "$prot" "$localdir"
          then 
             echo "Directory $localdir chmoded to $prot." 
          else
             echo "ERROR:  Unable to chmod $localdir to $prot." 
             retval=1
          fi  
       else
          echo "ERROR:  Unable to create $localdir!" 
          retval=1
       fi
    else
       echo "ERROR:  Directory $localdir must exist!" 
       retval=1
    fi
 else
    if [ ! -d "$localdir" ]
    then
       echo "ERROR:  $localdir exists but is not a directory!" 
       retval=1
    else
       echo "Directory $localdir exists." 
    fi
 fi

 return $retval

}


checkforaccess()
{
 #
 # Make sure that we have the correct access to /usr/local
 #

 retval=0

 #
 # Check to make sure that we have read access to the directory
 #
 comma=""
 if [ ! -r /usr/local ]
 then
    noaccess="Read"
    comma=", "
 fi

 #
 # Check to make sure that we have write access to the directory
 #
 if [ ! -w /usr/local ]
 then
    noaccess="$noaccess${comma}Write"
    comma=", "
 fi

 #
 # Check to make sure that we have execute access to the directory
 #
 if [ ! -x /usr/local ]
 then
    noaccess="$noaccess${comma}Execute"
 fi

 if  [ "$noaccess" != "" ]
 then
    echo "ERROR:  Root does not have $noaccess access to /usr/local!" 
    retval=1
 else
    echo "Root has necessary access to /usr/local." 
 fi

 return $retval

}

wheretoinstall()
{
 #
 # Find out where to install the extensions and create directory and/or link
 # as necessary.
 #
 
 retval=0

 echo 
 echo "Where would you like to install the FrontPage Extensions.  If you" 
 echo "select a location other than /usr/local/frontpage/ then a symbolic" 
 echo "link will be created from /usr/local/frontpage/ to the location that" 
 echo "is chosen." 
 echo 
 $echo "FrontPage Extensions directory [/usr/local/frontpage/]:  ${nnl}" 
 read installdir
 
 if [ "$installdir" = "" ]
 then
    installdir=$INSTALLDIRDEFAULT
 fi
 installdir=`dirname $installdir`/`basename $installdir`

 if [ ! -d "$installdir" ]
 then
    echo "Creating $installdir" 
    if mkdir "$installdir"
    then
       echo "Directory $installdir has been created." 
       if chmod "$prot" "$installdir"
       then 
          echo "Directory $installdir chmoded to $prot." 
       else
          echo "ERROR:  Unable to chmod $installdir to $prot." 
          retval=1
       fi  
    else
       echo "ERROR:  Unable to create $installdir!" 
       retval=1
    fi
 else
    echo "WARNING:  Directory $installdir already exists." 
    echo "Installation will overwrite existing files." 
    echo 

    myprompt 'yYnN' "Continue the installation (y/n)" "N"
    echo 
    if [ $answer = n ] || [ $answer = N ]
    then
       exit 0
    fi
 fi
 
 if [ "$installdir" != "/usr/local/frontpage" ]
 then
    if [ ! -d "/usr/local/frontpage" ]
    then
       if ln -s "$installdir" "/usr/local/frontpage"
       then
          echo "Snapped link /usr/local/frontpage --> $installdir"        
       else
          echo "ERROR:  Unable to  snap link /usr/local/frontpage --> $installdir!" 
          retval=1
       fi
    else
       echo "ERROR:  Unable to snap link /usr/local/frontpage --> $installdir, /usr/local/frontpage already exists!" 
       retval=1
    fi
 fi 

 return $retval 

}

untarext()
{
 #
 # Untar the extensions into the installation directory and check permissions
 #
 retval=0
 
 if [ -d "${installdir}/version${VERSION}" ]
 then
    echo "Version $VERSION FrontPage Server Extensions found."
    myprompt 'yYnN' "Would you like to overwrite? (y/n)" "Y"
    echo 
    if [ $answer = n ] || [ $answer = N ]
    then
        echo "No need to un-tar the extensions.  Continuing..."
        return $retval
    else
        echo "Looking for tar file..."
    fi
 else
     echo "Version $VERSION FrontPage Server Extensions not found."
     echo "Looking for tar file..."
 fi

 vtfile="fp40.$machine.tar"
 echo "Platform is $machine." 
 
 vtfilelocation="`pwd`/"

 getextfilename $vtfilelocation $vtfile || return 1

 olddir=`pwd`

 if [ "$installdir" != "/usr/local/frontpage" ]
 then
    cd $installdir
 else
    cd /usr/local
 fi 
 case $fullvtfile in 
     *tar) echo "Untarring file $fullvtfile into /usr/local..." 
           tar -xf $fullvtfile ;;
       *Z) echo "Uncompressing/Untarring file $fullvtfile into /usr/local..."
           zcat $fullvtfile | tar -xf - 
           ;;
      *gz) zcat=""
           while [ "$zcat" = "" ] || [ ! -f "$zcat" ]
           do
               $echo "Where is the zcat which can uncompress gz files? ${nnl}"
               read zcat
               base=`basename zcat`
               case $base in
                  zcat) ;; 
                     *) zcat=`dirname $zcat`/`basename $zcat`/zcat ;;
               esac
           done
           echo "Uncompressing/Untarring file $fullvtfile into /usr/local..."
           $zcat $fullvtfile | tar -xf -
           ;;
        *) echo "ERROR:  Unknown file type: $fullvtfile"
           return 1
           ;;
 esac

 if [ "$installdir" != "/usr/local/frontpage" ]
 then
     mv frontpage/* .
     rm -rf frontpage
 fi

 cd $olddir

 return $retval

}

getextfilename()
{
 vtfilelocation=$1
 vtfile=$2
 fullvtfile="${vtfilelocation}${vtfile}"
 if [ ! -f "$fullvtfile" ]
 then
    if [ -f "${fullvtfile}.Z" ]
    then
       fullvtfile="${fullvtfile}.Z"       
    else
       if [ -f "${fullvtfile}.gz" ]
       then
          fullvtfile="${fullvtfile}.gz"
       else          
        if [ -f "${fullvtfile}.z" ]
        then
           echo "Renaming ${vtfile}.z to ${vtfile}.Z"
           mv "${fullvtfile}.z" "${fullvtfile}.Z"
           fullvtfile="${fullvtfile}.Z"
        else
          echo "Cannot find the FrontPage Extensions tar file in ${vtfilelocation}." 
          $echo "Which directory is the file located in (X to cancel)?  ${nnl}" 
          read vtfilelocation
          if [ "$vtfilelocation" = "X" ] || [ "$vtfilelocation" = "x" ]
          then
             return 1
          else
             
             vtfilelocation=`echo $vtfilelocation | sed -e 's/\/$//'`

             if [ ! -d "$vtfilelocation" ]
             then
                 vtfilelocation=`dirname $vtfilelocation`
             fi

             getextfilename $vtfilelocation/ $vtfile
          fi  
        fi
       fi
    fi
 fi


}

upgradeexistingservers()
{
 retval=0

 echo "Checking for existing web servers to upgrade..." 

 upgrade="none"
 for file in ${installdir}/*.cnf
 do
    if [ "$file" = "${installdir}/*.cnf" ]
    then
        echo "No existing web servers found to upgrade." 
        return $retval
    fi

    cat <<EOF

Existing web servers were found.
You can upgrade them later by re-running fp_install.sh and
answering yes to the following question.

EOF
    myprompt 'yYnN' "Would you like to upgrade them now (y/n)" "Y"

    if [ $answer = n ] || [ $answer = N ]
    then
        upgrade="no"
        echo "For details on how to upgrade servers manually, please see" 
        echo "the Server Extension Resource Kit (SERK), located in"
        echo "/usr/local/frontpage/version${VERSION}/serk"
        echo
        return $retval
    else
        upgrade="yes"
    fi
    break
 done

 cat <<EOF
        
The file ${installdir}/version${VERSION}/upgrade_results.txt will
contain Success/Fail status for the upgrades.  When the upgrade is
complete you should examine this file to make sure that all of the
upgrades completed successfully.

EOF
 $echo "Hit enter to continue${nnl}" 
 read continue
 echo

 echo "All existing servers will now be upgraded:" 
 echo " " 
 
 createdate=`date`
 cat >${installdir}/version${VERSION}/upgrade_results.txt <<EOF 
#
# Server Upgrade Results
#
# Automatically generated by fp_install.sh on $createdate
#
EOF

 for weconfigfile in ${installdir}/*.cnf
 do
    echo 
    echo "Upgrading using configuration file:  "$weconfigfile 
    if verifywebserver $weconfigfile
    then
        if upgradeserver $weconfigfile
        then
            echo "Upgrade Successful  $weconfigfile" >> ${installdir}/version${VERSION}/upgrade_results.txt
        else
            echo "Upgrade Failed      $weconfigfile" >> ${installdir}/version${VERSION}/upgrade_results.txt
            echo "ERROR:  Server upgrade failed!"
            echo "Continuing with next server." 
            $echo "Hit enter to continue${nnl}" 
            read continue
        fi
    else
        echo "Cancelling upgrade..."
        echo "Upgrade Failed      $weconfigfile" >> ${installdir}/version${VERSION}/upgrade_results.txt
    fi
 done

 weconfigfile=""

 return $retval
}

verifywebserver()
{
 weconfigfile="$1"

 conf=`basename $weconfigfile`
  port=`echo  $conf | sed -e '
    s/:/:/
    tmulti
    s/.cnf$//
    s/.*[^0-9]//
    q
    :multi
    s/.cnf$//'`

 echo "Verifying web server configuration..."

 configfile=`grep -i "^serverconfig:" $weconfigfile | sed -e '
             s/serverconfig://g
             s/fakeconf.*\///'`
 if [ ! -f "$configfile" ]
 then
     echo "$configfile does not exist."
     return 1
 fi
  
 servertype=`grep -i "^servertype:" $weconfigfile | sed -e 's/servertype://g'`
 configfiledir=`dirname $configfile`"/"

 if [ $servertype = "cern" ]
 then
     echo "This version of FrontPage does not suppport cern servers."
     echo "For more information about supported servers, please see" 
     echo "the Server Extension Resource Kit (SERK), located in"
     echo "/usr/local/frontpage/version${VERSION}/serk"
     return 1
 fi

 if [ $servertype = "apache-fp" ]
 then
     echo "This server has the Microsoft FrontPage 98 Apache patch installed."
     echo "Be sure to install the FrontPage 2000 Apache patch by running the"
     echo "change_server.sh script, or this web server will not be upgraded to"
     echo "Microsoft FrontPage 2000."
     $echo "Hit enter to continue${nnl}" 
     read continue
 fi

 getdocroot $weconfigfile ||
 {
    echo "ERROR:  Unable to get DocumentRoot/UserDir" 
    return 1
 } 

 case $servertype in 
      *pache*) getHttpDirective $configfile AccessConfig $port
               if [ "$param" != "" ]
               then
                   file=`basename $param`
                   accessconffile="${configfiledir}${file}"
               else
                   accessconffile="${configfiledir}access.conf"
               fi

               if [ ! -f "$accessconffile" ]
               then
                   echo "$accessconffile does not exist."
                   return 1
               fi
               ;;
         ncsa) getHttpDirective $configfile AccessConfig $port
               if [ "$param" != "" ]
               then
                   file=`basename $param`
                   accessconffile="${configfiledir}${file}"
               else
                   accessconffile="${configfiledir}access.conf"
               fi

               if [ ! -f "$accessconffile" ]
               then
                   echo "$accessconffile does not exist."
                   return 1
               fi
               ;;
 esac

 servicesfile=${docroot}"/_vti_pvt/services.cnf"

 if [ ! -f "$servicesfile" ]
 then
     echo "There are no services to upgrade for this web."
     return 1
 fi

 return 0
}

chownexistingservers()
{
 retval=0

 if [ "$upgrade" = "yes" ]
 then
     echo
     echo "Preparing to chown webs..." 
     cat <<EOF

Your webs have been upgraded to use the new FrontPage Server Extensions.  The
next step is to chown the web in order to guarantee that the extensions will
work properly.  At this point you have two options:

   1. This script will prompt you interactively for an owner and group of
      each web and then perform the chown.  If you do not have a lot of 
      webs you might want to choose this option.

   2. This script will generate a script, which you can edit to fill in the
      owner and group for each web, to run at a later date.  If you have a
      large number of webs you might want to choose this option.

EOF
 
     echo "Would you like to be prompted interactively for"
     myprompt 'yYnN' "each webs owner/group (y/n)" "Y"
     if [ $answer = y ] || [ $answer = Y ]
     then
        chownwebs 
     else
        generatechownscript $file  ||
        (
            echo "ERROR:  Server chown failed!  Continuing with next server." 
            $echo "Hit enter to continue${nnl}" 
            read continue
        )

     fi
     return $retval
 fi
}

chownwebs()
{
 retval=0

 for weconfigfile in ${installdir}/*.cnf
 do
     if grep Failed ${installdir}/version${VERSION}/upgrade_results.txt | grep ${weconfigfile} > /dev/null
     then
         echo
         echo "Upgrade of ${weconfigfile} failed."
         echo "See ${installdir}/version${VERSION}/upgrade_results.txt" 
         echo "Skipping chown..."
     else
         chownWeb $weconfigfile
     fi
 done

 weconfigfile=""

 return $retval
}

chownWeb()
{
 weconfigfile=$1

 echo 
 conf=`basename $weconfigfile`
 webport=`echo  $conf | sed -e '
     s/:/:/
     tmulti
     s/.cnf$//
     s/.*[^0-9]//
     q
     :multi
     s/.cnf$//'`

 port=$webport

 echo "Processing webs in port $webport..."
 echo

 servertype=`grep -i "^servertype:" $weconfigfile | sed -e 's/servertype://g'`
 configfile=`grep -i "^serverconfig:" $weconfigfile | sed -e '
             s/serverconfig://g
             s/fakeconf.*\///'`
 configfiledir=`dirname $configfile`"/"

 getdocroot $weconfigfile ||
 {
    echo "ERROR:  Unable to get DocumentRoot/UserDir" 
    return 1
 } 

 servicesfile=${docroot}"/_vti_pvt/services.cnf"
 
 exec 4<&0
 exec <$servicesfile
 n=0
 while read service
 do 
    echo
    if [ $service = "/" ]
    then
       webname=""
       webtext="root web"
       web=""
    else
       webname="$service"
       webtext="$service"
       web="-w $webname"
    fi
    exec 5<&0
    exec 0<&4

    getdocroot $weconfigfile ||
    {
        echo "ERROR:  Unable to get DocumentRoot/UserDir" 
        return 1
    } 

    case $service in
       /~*)  owner=`echo $service | sed -e 's/\///'`

             webowner=`echo $service | sed -e 's/\/~//'`
             homedir=`finger $webowner |  $awk ' { if (NR==2) print $2}'`
             if [ -d "${homedir}/${userdir}" ]
             then
                 echo "Web ${webtext} on port ${webport} will be owned by ${webowner}"
                 defwebgroup=`$lsg ${homedir}/${userdir} | $awk ' { print $4}'`
                 exists=0
             else
                 exists="${homedir}/${userdir}"
             fi
             ;;
         *)  if [ -d "${docroot}/${service}" ]
             then
                 defwebowner=`$lsg ${docroot}${service} | $awk ' { print $3}'`
                 defwebgroup=`$lsg ${docroot}${service} | $awk ' { print $4}'`

                 webowner=""
                 until [ "$webowner" != "" ]
                 do
                     $echo "Who should own web ${webtext} on port ${webport} [${defwebowner}]:  ${nnl}"
                     read webowner
                     if [ "$webowner" = "" ]
                     then
                         webowner=$defwebowner
                     fi
                 done

                 exists=0
             else
                 exists="${docroot}/${service}"
             fi
        
             ;;
    esac

    if [ "$exists" = "0" ]
    then
        webgroup=""
        until [ "$webgroup" != "" ]
        do
            $echo "What should the group for web ${webtext} on port ${webport} be [${defwebgroup}]:  ${nnl}"
            read webgroup
            if [ "$webgroup" = "" ]
            then
                webgroup=$defwebgroup
            fi
        done

        /usr/local/frontpage/version${VERSION}/bin/fpsrvadm.exe -o chown -p $webport $web -xUser $webowner -xGroup $webgroup ||
        {
            echo 
            echo "ERROR:  Unable to chown web ${webtext} in port ${webport}"
            $echo "Hit enter to continue${nnl}" 
            read continue
        }
    else
        echo "ERROR:  web $service - $exists does not exist!  Skipping to next web."  
    fi
        exec 0<&5
 done
 exec <&4

}

generatechownscript()
{
 retval=0
 scriptout="/usr/local/frontpage/version${VERSION}/fp_chown.sh"
 createdate=`date`

 cat <<EOF

A script will be generated in ${scriptout} 
which you can edit to chown your webs at a future date.  You will 
need to edit the script and set the owner and group for each web 
before running the script.

EOF

 cat >$scriptout <<EOF 
#! /bin/sh
#
#
# Copyright 1996 Microsoft Corporation -- All Rights Reserved.
#
#          Automatically generated by fp_install.sh
#
# Automatically generated by fp_install.sh on $createdate
#
# You will need to edit this script before running it.  Change each 
# <OWNER> and <GROUP> to reflect the ownership/group that you want 
# set for the web.
#
# Example:  80 /testweb webowner webgroup
#

VERSION="4.0"

chown_web()
{
 port=\$1
 webname=\$2
 webowner=\$3
 webgroup=\$4

 if [ "\$webowner" = "<OWNER>" ] || [ "\$webgroup" = "<GROUP>" ]
 then
     echo "WARNING:  Owner/Group not specified for web \$webname on \$port."
     echo "Skipping to next web..."
 else
     if [ "\$webname" != "/" ]
     then
        webname=\`echo \$webname | sed -e 's%^/%%g'\`
        webtext="\$webname"
        web="-w \$webname"
     else
        webtext="root web"
        web=""
     fi
     echo
     echo "Chowning web \${webtext} in port \${port} to owner \${webowner} group \${webgroup}"
     /usr/local/frontpage/version4.0/bin/fpsrvadm.exe -o chown -p \$port  \$web  -xUser \$webowner -xGroup \$webgroup
 fi
}

while read port webname webowner webgroup
do
    chown_web \$port \$webname \$webowner \$webgroup
done <<ENDCHOWN
EOF

 for weconfigfile in ${installdir}/*.cnf
 do
     if grep Failed ${installdir}/version${VERSION}/upgrade_results.txt | grep ${weconfigfile} > /dev/null
     then 
         echo
         echo "Upgrade of ${weconfigfile} failed."
         echo "See ${installdir}/version${VERSION}/upgrade_results.txt" 
         echo "Skipping chown..."
     else
         addChownWeb $weconfigfile
     fi
 done

 echo "ENDCHOWN" >>$scriptout

 chmod 764 $scriptout
 
 weconfigfile=""
 return $retval
}

addChownWeb()
{

 weconfigfile=$1

 echo 
 echo "Processing  $weconfigfile"
 conf=`basename $weconfigfile`
 webport=`echo  $conf | sed -e '
    s/:/:/
    tmulti
    s/.cnf$//
    s/.*[^0-9]//
    q
    :multi
    s/.cnf$//'`

 port=$webport

 echo "Adding webs in port ${webport} to chown script..."
 servertype=`grep -i "^servertype:" $weconfigfile | sed -e 's/servertype://g'`
 configfile=`grep -i "^serverconfig:" $weconfigfile | sed -e '
             s/serverconfig://g
             s/fakeconf.*\///'`
 configfiledir=`dirname $configfile`"/"
 getdocroot $weconfigfile ||
 {
     echo "ERROR:  Unable to get DocumentRoot/UserDir" 
     return 1
 } 

 servicesfile="${docroot}/_vti_pvt/services.cnf"
 if [ -r $servicesfile ]
 then
     exec 4<&0
     exec <$servicesfile
     n=0

     while read service
     do 
        if [ $service = "/" ]
        then
            webtext="root web"
        else
            webtext="$service"
        fi
        
        case $service in
           /~*)  owner=`echo $service | sed -e 's/\///'`

                 webowner=`echo $service | sed -e 's/\/~//'`
                 homedir=`finger $webowner |  $awk ' { if (NR==2) print $2}'`
                 if [ -d "${homedir}/${userdir}" ]
                 then
                     webgroup=`$lsg ${homedir}/${userdir} | $awk ' { print $4}'`
                     exists="0"

                 else
                     exists="${homedir}/${userdir}"
                 fi
                 ;;
             *)  if [ -d "${docroot}/${service}" ]
                 then
                     webowner=`$lsg ${docroot}${service} | $awk ' { print $3}'`
                     webgroup=`$lsg ${docroot}${service} | $awk ' { print $4}'`
                     exists="0"
                 else
                     exists="${docroot}/${service}"
                 fi
                 ;;
        esac


     if [ "$exists" = "0" ]
     then
        echo "web ${webtext}"
        echo "$webport $service $webowner $webgroup" >> $scriptout
     else
        echo "ERROR:  web ${webtext}- Path $exists does not exist!  Skipping to next web."  
     fi        
     done
     exec <&4
 else
     echo "WARNING:  Unable to read $servicesfile!" 
     echo "Skipping to next port."
 fi

}

upgradeserver()
{
 retval=0
 weconfigfile="$1"

 bindir=$installdir'/version'$VERSION'/bin/'

 conf=`basename $weconfigfile`

 echo "Upgrading server "$port 
 
 /usr/local/frontpage/version${VERSION}/bin/fpsrvadm.exe -o upgrade -p $port
 
 return $retval
}

installrootweb()
{
 retval=0

 configfile=""
 admin=""
 port=""
 multihost=""
 webname=""
 webowner=""
 servertype=""

 echo 
 echo "Note: If you have not installed the root web then you need to do it now." 
 echo 
 myprompt 'yYnN' "Do you want to install a root web (y/n)" "Y"
 if [ $answer = n ] || [ $answer = N ]
 then
     return $retval
 fi

 echo " " 
 echo "Installing the root web..." 
 echo " " 

 webname="/"

 configfile=""
 while ( [ "$configfile" = "" ] || [ ! -f $configfile ] )
 do
     $echo "Server config filename:  ${nnl}" 
     read configfile
 done

 admin=""
 until [ "$admin" != "" ]
 do
     $echo "FrontPage Administrator's user name:  ${nnl}" 
     read admin
 done

 getparam Port $configfile
 port=$param

 until [ "$port" != "" ]
 do
    $echo "Enter the new servers port number:  ${nnl}" 
    read port
 done

 getparam User $configfile
 defwebowner=$param

 weconfigfile="${installdir}/we${port}.cnf"

 webowner=""
 until [ "$webowner" != "" ]
 do
     $echo "Unix user name of the owner of this new web:[$defwebowner]  ${nnl}"
     read webowner
     if [ "$webowner" = "" ]
     then
         webowner=$defwebowner
     fi
 done

 getparam Group $configfile
 defgroup=$param
 webgroup=""
 until [ "$webgroup" != "" ]
 do
     echo 
     $echo "Unix group of this new web:[$defgroup]  ${nnl}" 
     read webgroup
     if [ "$webgroup" = "" ]
     then
        webgroup=$defgroup
     fi
 done
 
 until [ "$servertype" != "" ]
 do
     echo 
     echo "    1.  ncsa" 
     echo "    2.  apache" 
     echo "    3.  apache-fp"
     echo "    4.  netscape-fasttrack" 
     echo "    5.  netscape-enterprise"
     echo "    6.  stronghold"
     $echo "What type of Server is this:  ${nnl}" 
     read servertypenum
     echo 

     case $servertypenum in
          "1") servertype="ncsa" ;;
          "2") servertype="apache" ;;
          "3") servertype="apache-fp" ;;
          "4") servertype="netscape-fasttrack" ;;
          "5") servertype="netscape-enterprise" ;;
          "6") servertype="stronghold" ;;
            *) echo "Invalid option!  Please try again."   ;;
     esac 
 done

 multihost="."
 if [ "$configfile" = "" ]
 then
     configfile="."
 fi

 PORT=$port

 echo "Installing root web into port $port..." 
 echo 
 installserver $port $multihost $webname $webowner $webgroup $admin $configfile $servertype ||
 {
     echo "ERROR:  $webname installation failed." 
     $echo "Hit enter to continue${nnl}" 
     read continue
     return $retval
 }

 isItApacheFP
 handlelanguage

 return $retval
}

isItApacheFP()
{
 retval=0

 if [ "$servertype" = "apache-fp" ]
 then
     apachedir=$installdir'/version'$VERSION'/apache-fp'

     echo 
     echo "Chowning $apachedir to root..." 
     chown root $apachedir ||
       (
        echo "ERROR:  Unable to chown $apachedir to root" 
        retval=1
       )

     echo 
     echo "Chmoding $apachedir to 711..." 
     chmod 711 $apachedir ||
       (
        echo "ERROR:  Unable to chown $apachedir to 711" 
        retval=1
       )

     apachedir=$installdir'/version'$VERSION'/apache-fp/_vti_bin'

     echo 
     echo "Chowning $apachedir to root..." 
     chown root $apachedir ||
       (
        echo "ERROR:  Unable to chown $apachedir to root" 
        retval=1
       )

     echo 
     echo "Chmoding $apachedir to 755..." 
     chmod 755 $apachedir ||
       (
        echo "ERROR:  Unable to chown $apachedir to 755" 
        retval=1
       )

     fpexe=$installdir'/version'$VERSION'/apache-fp/_vti_bin/fpexe'

     echo 
     echo "Chowning $fpexe to root..." 
     chown root $fpexe ||
       (
        echo "ERROR:  Unable to chown $fpexe to root" 
        retval=1
       )

     echo "Setting $fpexe to SUID..." 
     chmod u+s $fpexe ||
       (
        echo "ERROR:  Unable to set SUID for "$fpexe 
        retval=1
       ) 
    
     rm -f ${installdir}/version${VERSION}/apache-fp/suidkey

     ps=`ps -ea | tail -10` ; echo $ps | sed -e 's/ //g' | cut -c10-137 > ${installdir}/version${VERSION}/apache-fp/suidkey
   
     chown root ${installdir}/version${VERSION}/apache-fp/suidkey
     chmod 600 ${installdir}/version${VERSION}/apache-fp/suidkey
 fi

 return $retval

}

handlelanguage()
{
 retval=0

 until [ "$charencoding" != "" ]
 do
     echo 
     echo "    1.  LATIN1 (ISO 8859-1)" 
     echo "    2.  LATIN2 (ISO 8859-2)" 
     echo "    3.  EUCJP  (Japanese EUC)" 
     echo "    4.  EUCKR  (Korean EUC)" 
     $echo "Which local character encoding does your system support: [1]  ${nnl}" 
     read charencodingnum
     echo

     case $charencodingnum in
         "") charencoding="latin1" ;;
         "1") charencoding="latin1" ;;
         "2") charencoding="latin2" ;;
         "3") charencoding="eucjp" ;;
         "4") charencoding="euckr" ;;
           *) echo "Invalid option!  Please try again."  ;;
     esac
 done

 until [ "$lang" != "" ]
 do
     echo 
     echo "    1.  English" 
     echo "    2.  French" 
     echo "    3.  German" 
     echo "    4.  Italian" 
     echo "    5.  Japanese" 
     echo "    6.  Spanish" 
     $echo "What should the default language be: [1] ${nnl}" 
     read langnum
     echo 

     case $langnum in
         "") lang="en" ;;
         "1") lang="en" ;;
         "2") lang="fr" ;;
         "3") lang="de" ;;
         "4") lang="it" ;;
         "5") lang="ja" ;;
         "6") lang="es" ;;
           *) echo "Invalid option!  Please try again."  ;;
     esac
 done

 conffile=$installdir'/version'$VERSION'/frontpage.cnf'
 echo "Setting "$conffile" to:" 
 echo 
 echo "defaultLanguage:${lang}"
 echo "localCharEncoding:${charencoding}"
 echo 
 echo "Moving ${conffile} to ${conffile}.orig"
 echo
 mv $conffile ${conffile}.orig ||
 {
    echo "ERROR:  Unable to backup $conffile to ${conffile}.orig!"
    $echo "Hit enter to continue${nnl}" 
    read continue
    return 1
 }

 echo "Creating and modifying new ${conffile}..."
 echo
 sed -e "s/defaultLanguage:.*/defaultLanguage:${lang}/g
         s/localCharEncoding:.*/localCharEncoding:${charencoding}/g" \
      ${conffile}.orig > $conffile ||
 {
    echo "ERROR:  Unable to create new $conffile!"
    echo "If the file has been corrupted you should be able to replace it with"
    echo "the backup file (${conffile}.orig)"
    $echo "Hit enter to continue${nnl}" 
    read continue
    return 1 
 }                          

 return $retval
}

installvirtualwebs()
{
 retval=0

 echo
 echo "Installing Virtual Webs.." 
 echo 
 myprompt 'yYnN' "Do you want to install Virtual Webs (y/n)" "Y"
 if [ $answer = n ] || [ $answer = N ]
 then
     return $retval
 fi

 defaultconfigfile=$configfile
 configfile=""
 while ( [ "$configfile" = "" ] || [ ! -f $configfile ] )
 do
     $echo "Server config filename [${defaultconfigfile}]:  ${nnl}" 
     read configfile
     if [ "$configfile" = "" ]
     then
         configfile=$defaultconfigfile
     fi
 done

 getparam Port $configfile
 port=$param
 getparam User $configfile
 webowner=$param

 case $configfile in 
      *magnus*) echo
                echo "Looking for Netscape virtuals..."
                n=0              
                resourcefile=`echo $configfile | sed -e 's/magnus\.conf/obj.conf/'`  
                urlhosts=`grep -i \urlhost $resourcefile`
                if [ "$urlhosts" ]
                then
                    novirtuals=0
                else
                    novirtuals=1
                fi

                while [ "$urlhosts" ]
                do       
                    urlhost=`echo $urlhosts | sed -e '
                           s/>.*/>/'`
                    virtweb=`echo $urlhost | sed -e '
                            s/\"//g
                            s/ *<.*= *//
                            s/ *> *//'` 
                    n=`expr $n + 1`                
                    eval virtwebs$n="$virtweb"
                    urlhost=`echo $urlhost | sed -e '
                           s/\./\\\./g
                           s/\"/\\\"/g'`
                    urlhosts=`echo $urlhosts | sed -e "s/$urlhost *//"`
                done
               
                virtuals=`grep -i NameTrans $resourcefile | grep -i \address | sed -e "
                        s/\"//g
                        s/.*[Aa][Dd][Dd][Rr][Ee][Ss][Ss]= *//
                        s/ .*//"`

                if [ "$virtuals" ]
                then
                    novirtuals=0
                fi

                for virtweb in $virtuals
                do
                    n=`expr $n + 1`                
                    eval virtwebs$n="$virtweb"
                done

                if [ $novirtuals = 1 ]
                then
                    echo "ERROR:  There are no valid virtual webs defined." 
                    $echo "Hit enter to continue${nnl}" 
                    read continue
                    return 1
                fi
                ;;
             *) virtnames=`cat $configfile | $awk "
                    BEGIN { 
                        value = \"\"; j=0 
                    } 


                        { x=0; y=0; server=\"\"; virtname=\"\" }


                        /^[^#]* *< *[Vv][Ii][Rr][Tt][Uu][Aa][Ll][Hh][Oo][Ss][Tt][ $TAB]/ { 
                            virtname = \\\$2; y=1; j++
                        }

                        /^[^#]*[Ss][Ee][Rr][Vv][Ee][Rr][Nn][Aa][Mm][Ee][ $TAB]/  { 
                            server = \\\$2; y=2
                        }

                        {
                            if (y == 1) {  gsub(/>/,\"\",virtname); ARRAY[j] = virtname }
                            if (y == 2) { ARRAY[j] = server }
                        }

                    
                    END { 
                            for ( i =1; i <= j; i++ ) { 
                                print ARRAY[i] \" \" 
                            } 
                    }"`

                virtnames=`echo $virtnames | sed -e 's/>//g'`
                n=0
                for word in $virtnames 
                do 
                    n=`expr $n + 1` 
                    eval virtwebs$n="$word" 
                done
                    
                ;;
 esac

 while echo
       n=1
       val=`eval echo $"virtwebs$n"`
       while [ "$val" != "" ]
       do
          echo "  $n) $val" 
          n=`expr $n + 1`
          val=`eval echo $"virtwebs$n"`
       done

       echo 
       $echo "Select the virtual web to install (CTRL-D if no more webs): ${nnl}" 
       read virtwebno
 do
    getparam Port $configfile
    port=$param
    until [ "$port" != "" ]
    do
        $echo "Enter the new servers port number:  ${nnl}" 
        read port
    done

    port="`eval echo $"virtwebs$virtwebno"`:$port"

    getHttpVirtualDirective $configfile $port User
    defwebowner=$param

    webowner=""
    until [ "$webowner" != "" ]
    do
        $echo "Unix user name of the owner of this new web:[$defwebowner]  ${nnl}"
        read webowner
        if [ "$webowner" = "" ]
        then
            webowner=$defwebowner
        fi
    done

    getHttpVirtualDirective $configfile $port Group
    defwebgroup=$param

    webgroup=""
    until [ "$webgroup" != "" ]
    do
        echo 
        $echo "Unix group of this new web:[$defgroup]  ${nnl}" 
        read webgroup
        if [ "$webgroup" = "" ]
        then
            webgroup=$defgroup
        fi
    done
     
    webname="/"

    admin=""
    until [ "$admin" != "" ]
    do
        $echo "FrontPage Administrator's user name:  ${nnl}" 
        read admin
    done

    until [ "$servertype" != "" ]
    do
     echo 
     echo "    1.  ncsa" 
     echo "    2.  apache" 
     echo "    3.  apache-fp"
     echo "    4.  netscape-fasttrack" 
     echo "    5.  netscape-enterprise" 
     echo "    6.  stronghold"
     $echo "What type of Server is this:  ${nnl}" 
     read servertypenum
     echo 

     case $servertypenum in
          "1") servertype="ncsa" ;;
          "2") servertype="apache" ;;
          "3") servertype="apache-fp" ;;
          "4") servertype="netscape-fasttrack" ;;
          "5") servertype="netscape-enterprise" ;;
          "6") servertype="stronghold" ;;
            *) echo "Invalid option!  Please try again."   ;;
       esac 
    done

    multihost="`eval echo $"virtwebs$virtwebno"`"
    if [ "$configfile" = "" ]
    then
       configfile="."
    fi
    if [ "$servertype" = "" ]
    then
       servertype="."
    fi


    PORT=$port
    echo "Installing virtual root web into port $port..." 
    echo 
    installserver $port $multihost $webname $webowner $webgroup $admin $configfile $servertype ||
    {
        echo "ERROR:  $webname installation failed." 
        $echo "Hit enter to continue${nnl}" 
        read continue
        return $retval
    }

    isItApacheFP
    handlelanguage
    
    weconfigfile=""

    installnewsubwebs $PORT || error     # Install new servers
    echo
 done
 
 return $retval

}

installnewsubwebs()
{
 port=$1

 retval=0

 echo 
 myprompt 'yYnN' "Install new sub/per-user webs now (y/n)" "Y"
 if [ $answer = n ] || [ $answer = N ]
 then
     echo "For details on how to upgrade servers manually, please see" 
     echo "the Server Extension Resource Kit (SERK), located in"
     echo "/usr/local/frontpage/version${VERSION}/serk"
     echo
     return $retval
 fi
 echo 

 if [ "$weconfigfile" = "" ]
 then
     until [ "$weconfigfile" != "" ] && [ -f $weconfigfile ]
     do
         port=""
         if [ "$port" = "" ]
         then
             cat <<EOF

You need to specify which server to install the subweb into.
Examples:
          80
          www.virtual.web:80

          where 80 is the port the server is running on.

EOF
             $echo "Which server would you like to install the subweb into:  ${nnl}" 
             read port
         fi
         echo 
         case $port in 
              *:*) weconfigfile="${installdir}/${port}.cnf" ;;
                *) weconfigfile="${installdir}/we${port}.cnf" ;;
         esac
     done
 fi

 echo
 echo "Using FrontPage Configuration File:  ${weconfigfile}"
 echo

 servertype=`grep -i "^servertype:" $weconfigfile | sed -e 's/servertype://g'`
 configfile=`grep -i "^serverconfig:" $weconfigfile | sed -e '
             s/serverconfig://g
             s/fakeconf.*\///'`
 configfiledir=`dirname $configfile`"/"

 getdocroot $weconfigfile ||
 {
    echo "ERROR:  Unable to get DocumentRoot/UserDir" 
    return 1
 } 

 services=${docroot}"/_vti_pvt/services.cnf"
 
 if [ ! -f $services ]
 then
    echo
    echo "ERROR:  The root web must be installed before sub/per-user webs!"
    echo "Document Root:  ${docroot}"
    $echo "Hit enter to continue${nnl}" 
    read continue
    echo
    return $retval
 fi

 echo 
 while $echo "Enter the web name (CTRL-D if no more webs): ${nnl}" 
       read webname    
 do
    admin=""
    until [ "$admin" != "" ]
    do
        $echo "FrontPage Administrator's user name:  ${nnl}" 
        read admin
    done

    case "$webname" in
        ~*) PERUSER=1
            echo 
            echo "Web $webname is a per-user web" 
            echo
            webowner=$webname
            ;;
         *) PERUSER=0
            echo 
            echo "Web $webname is a subweb"  
            echo
            webowner=""
            ;;
    esac
 
    defwebowner=`$lsg ${docroot}${service} | $awk ' { print $3}'`
    until [ "$webowner" != "" ]
    do
       echo
       $echo "Unix user name of the owner of this new web:[$defwebowner]  ${nnl}"
       read webowner
       if [ "$webowner" = "" ]
       then
           webowner=$defwebowner
       fi
    done

    getparam Group $configfile
    defgroup=$param
    webgroup=""
    until [ "$webgroup" != "" ]
    do
       echo 
       $echo "Unix group of this new web:[$defgroup]  ${nnl}" 
       read webgroup
       if [ "$webgroup" = "" ]
       then
           webgroup=$defgroup
       fi
    done
  
    multihost="."

    if [ "$configfile" = "" ]
    then
       configfile="."
    fi

    if [ "$servertype" = "" ]
    then
       servertype="."
    fi

    installserver $port $multihost $webname $webowner $webgroup $admin $configfile $servertype ||
    {
        echo "ERROR:  $webname installation failed." 
        $echo "Hit enter to continue${nnl}"
        retval=0 
        read continue
    }
    echo 
 done

 return $retval
}

installserver()
{
 retval=0

 port="$1"
 multihost="$2"
 webname="$3"
 webowner="$4"
 webgroup="$5"
 admin="$6"
 configfile="$7"
 servertype="$8"
 
 echo 
 echo "installing server "$webname" on port "$port 
 
 if [ "$PERUSER" = 1 ]
 then
     user=`echo $webowner | sed -e 's/~//g'`
     echo 
     echo "Will chown per-user web to $user as part of install." 
     echo "Will chgrp per-user web to $webgroup as part of install." 
     chown="-xUser $user -xGroup $webgroup"  
 else
     if [ "$webowner" != "." ]
     then
        echo 
        echo "Will chown web to $webowner as part of install." 
        echo "Will chgrp web to $webgroup as part of install." 
        chown="-xUser $webowner -xGroup $webgroup"
     else
        chown=""
     fi
 fi

 if [ "$configfile" != "." ]
 then
     config="-s $configfile"
 else
     config=""
 fi
 
 if [ "$servertype" != "." ]
 then
     server="-type $servertype"
 else
     server=""
 fi

 if [ "$webname" != "/" ]
 then 
     web="-w $webname"
 else
     web=""
 fi

 if [ "$multihost" != "." ]
 then
    /usr/local/frontpage/version${VERSION}/bin/fpsrvadm.exe -o install -p $port $web $config -u $admin $server $chown -m $multihost || retval=1
 else
    /usr/local/frontpage/version${VERSION}/bin/fpsrvadm.exe -o install -p $port $web $config -u $admin $server $chown -m "" || retval=1
 fi

 return $retval

}

getdocroot()
{
 weconfigfile=$1

 servertype=`grep -i "^servertype:" $weconfigfile | sed -e 's/servertype://g'`
 configfile=`grep -i "^serverconfig:" $weconfigfile | sed -e '
             s/serverconfig://g
             s/fakeconf.*\///'`

 echo
 echo "Getting DocumentRoot and UserDir."

 if [ ! -f "$configfile" ]
 then
     echo "$configfile does not exist."
     return 1
 fi

 configfiledir=`dirname $configfile`"/"
  
 docroot=""

 case $weconfigfile in
      *:*) getvirtualdocroot $weconfigfile $configfile
           ;;
        *) case $servertype in 
                *etscape*) getnetscapedocroot $configfile
                           ;; 
                        *) getHttpDirective $configfile ResourceConfig $port
                           if [ "$param" != "" ]
                           then
                               if [ "${param}" = "/dev/null" ]
                               then
                                   resconffile="${configfile}"
                               else
                                   file=`basename $param`
                                   resconffile="${configfiledir}${file}"
                               fi
                           else
                               resconffile="${configfiledir}srm.conf"
                           fi

                           if [ ! -f "$resconffile" ]
                           then
                               echo "ERROR: $resconffile does not exist!"
                               return 1
                           fi

                           echo "Getting DocumentRoot from $resconffile."
                           getHttpRootDirective $resconffile DocumentRoot
                           docroot=$param
 
                           echo "Getting UserDir from $resconffile."
                           getHttpRootDirective $resconffile UserDir
                           userdir=$param
                           ;; 
           esac  
           ;;
 esac

 if [ ! -d "${docroot}" ]
 then
     echo "ERROR:  ${docroot} does not exist!"
     return 1
 fi

 if [ "${docroot}" = "" ]
 then
     echo "ERROR:  DocumentRoot not defined!"
     return 1
 fi

 echo
 echo "DocumentRoot: ${docroot}"

 if [ "${userdir}" = "" ]
 then
     echo "WARNING:  UserDir not defined."
 else
     echo "UserDir: ${userdir}"
 fi

 echo

 return 0

}

getHttpDirective()
{
 configfile=$1
 directive=$2
 port=$3

 case $port in
    *:*) getHttpVirtualDirective $configfile $port $directive ;;
      *) getHttpRootDirective $configfile $directive ;;
 esac
 
 if [ "$param" = "" ]
 then
     echo "Directive $directive not found."
 else 
     echo "Found Directive $directive, value $param."
 fi
}

getMixedCase() {

#
# getMixedCase takes a string and converts it to a regular expression which
# matches all possible case combinations.
#
# String = [Ss][Tt][Rr][Ii][Nn][Gg]
#

mc_string=`echo $1 | $awk "
               BEGIN {
                   newstring = \"\";
                   oldstring = \"$1\"; 

                   } 

                   $functions
 
                   {
                   
                   strlen = length(oldstring);

                   for ( i = 1; i <= strlen; ++i ) {
                       char = substr(oldstring, i, 1)
                       newstring = newstring \"[\" toupper(char) tolower(char) \"]\";
                   };
                   print newstring}"`


}

getHttpRootDirective()
{
 configfile=$1
 directive=$2

 getMixedCase $directive

 param=`cat $configfile | $awk "
            /^[^#]* *< *[Vv][Ii][Rr][Tt][Uu][Aa][Ll][Hh][Oo][Ss][Tt]/,/^[^#]* *< *\/[Vv][Ii][Rr][Tt][Uu][Aa][Ll][Hh][Oo][Ss][Tt]/ { next }
            /^[^#]* *${mc_string}[ $TAB]/  { print \\\$2 }"` 
 
 return 0

}

getHttpVirtualDirective()
{
 configfile=$1
 port=$2
 directive=$3

 virtweb=`echo  $port | sed -e 's/:[0-9]*.$//'`
 virtweb=`basename $virtweb`

 getMixedCase $directive

 param=`cat $configfile | $awk "
                          BEGIN    { value = \"\"; j=0 }
                                   { x=0; y=0; server=\"\"; virtname=\"\" }

                        /^[^#]* *< *[Vv][Ii][Rr][Tt][Uu][Aa][Ll][Hh][Oo][Ss][Tt][ $TAB]/ { 
                            virtname = \\\$2; y=1; j++
                        }

                        /^[^#]*[Ss][Ee][Rr][Vv][Ee][Rr][Nn][Aa][Mm][Ee][ $TAB]/  { 
                            server = \\\$2; y=2
                        }

                        /^[^#]* *${mc_string}[ $TAB]/  {
                            arg = \\\$2; y=3
                        }

                        
                        {
                            if (y == 1) { gsub(/>/,\"\",virtname); VIRTNAMES[j] = virtname }
                            if (y == 2) { SERVERNAMES[j] = server } 
                            if (y == 3) { ARRAY[j] = arg }
                        }

                    
                    END { 
                            for ( i =1; i <= j; i++ ) { 
                                if (SERVERNAMES[i] == \"$virtweb\" || VIRTNAMES[i] == \"$virtweb\" ) {
                                    print ARRAY[i] 
                                }
                            }
                    } "`

 if [ "$param" = "" ]
 then
    getHttpRootDirective $configfile $directive
 fi 

}

getnetscapedocroot()
{
 configfile=$1
 
 resourcefile=`echo $configfile | sed -e 's/magnus\.conf/obj.conf/'`  

 echo
 echo "Getting DocumentRoot from ${resourcefile}."

 exec 4<&0
 exec <$resourcefile
                
 while read line
 do 
     uc_line=`echo $line | tr [a-z] [A-Z]`
     case $uc_line in
          *DOCUMENT-ROOT*) docroot=`echo $line | sed -e '
                                    s/\"//g
                                    s/.* [Rr][Oo][Oo][Tt]= *//
                                    s/ .*//'` 
                           ;;
                 *SUBDIR*) userdir=`echo $line | sed -e '
                                    s/\"//g
                                    s/.* [Ss][Uu][Bb][Dd][Ii][Rr]= *//
                                    s/ .*//'` 
                     ;;
                \<CLIENT*) while read line
                           do 
                               uc_line=`echo $line | tr [a-z] [A-Z]`
                               case $uc_line in
                                   \</CLIENT\>) break 
                                                ;;
                               esac
                           done 
                           ;;
     esac
 done
 exec <&4

		

}

getvirtualdocroot()
{

 weconfigfile=$1
 configfile=$2

 virtweb=`echo  $weconfigfile | sed -e '
         s/:/:/
         tmulti
         s/.cnf$//
         s/.*[^0-9]//
         q
         :multi
         s/:[0-9]*.cnf$//'`
 virtweb=`basename $virtweb`

 case $configfile in
      *magnus*) n=0              
                resourcefile=`echo $configfile | sed -e 's/magnus\.conf/obj.conf/'`  
                exec 4<&0
                exec <$resourcefile
                echo
                echo "Looking for Client block of $resourcefile..." 
                
                while read line
                do 
                    uc_line=`echo $line | tr [a-z] [A-Z]`
                    case $uc_line in
                         *URLHOST*) virtname=`echo $line | sed -e '
                                             s/\"//g
                                             s/ *<.*= *//
                                             s/ *>.*//'` 

                                    if [ "$virtweb" = "$virtname" ]
                                    then
                                        echo "Found Client block.  Getting Document-Root and Subdir..."
                                        while read line
                                        do 
                                            uc_line=`echo $line | tr [a-z] [A-Z]`
                                            case $uc_line in
                                                  *DOCUMENT-ROOT*) docroot=`echo $line | sed -e '
                                                                    s/\"//g
                                                                    s/.* [Rr][)o][Oo][Tt]= *//
                                                                    s/ .*//'` 
                                                                 ;;
                                                         *SUBDIR*) userdir=`echo $line | sed -e '
                                                                    s/\"//g
                                                                    s/.* [Ss][Uu][Bb][Dd][Ii][Rr]= *//
                                                                    s/ .*//'` 
                                                                 ;;
                                                    \</CLIENT\>) break 
                                                                 ;;
                                            esac
                                        done 
                                    fi
                                    ;;
                    esac
                done
                exec <&4

		if [ "$docroot" = "" ]
		then
		    docroot=`grep -i NameTrans $resourcefile | grep -i $virtweb | sed -e "
					s/\"//g
					s/.*[Rr][Oo][Oo][Tt]= *//
					s/ .*//"`
 	        fi

                ;;
             *) getHttpVirtualDirective $configfile $port DocumentRoot
                docroot=$param

                if [ "$docroot" = "" ]
                then
                    echo "VirtualHost block does not contain DocumentRoot directive."
                    getHttpDirective $configfile ResourceConfig $port
                    if [ "$param" != "" ]
                    then
                        if [ "${param}" = "/dev/null" ]
                        then
                            resconffile="${configfile}"
                        else
                            file=`basename $param`
                            resconffile="${configfiledir}${file}"
                        fi
                    else
                        resconffile="${configfiledir}srm.conf"
                    fi
                    echo "Getting DocumentRoot from $resconffile."
                    getparam DocumentRoot $resconffile
                    docroot=$param
                fi
 
                getHttpVirtualDirective $configfile $port UserDir
                userdir=$param

                if [ "$userdir" = "" ]
                then
                    echo "VirtualHost block does not contain UserDir directive."
                    getHttpDirective $configfile ResourceConfig $port
                    if [ "$param" != "" ]
                    then
                        if [ "${param}" = "/dev/null" ]
                        then
                            resconffile="${configfile}"
                        else
                            file=`basename $param`
                            resconffile="${configfiledir}${file}"
                        fi
                    else
                        resconffile="${configfiledir}srm.conf"
                    fi
                    echo "Getting UserDir from $resconffile."
                    getparam UserDir $resconffile
                    userdir=$param
                fi
                ;; 
 esac
 
}

error()
{
 #
 # Print an error message and exit the program
 #

 echo 
 echo "Exiting due to an error!  Please fix the error and try again." 
 echo 
 
 exit 1
}

getparam() {
#
# gets the value of parameters from the config file
# 
    param=`egrep -i "^[ $TAB]*$1[ $TAB]" $2 | $awk '{print $2}'` || return 1
    return 0;
}
#
# This is the main part of the shell script.
#

initialize
step1
step2
step3
#
# Run the external permissions script.
#
/usr/local/frontpage/version${VERSION}/set_default_perms.sh

echo 
echo "Installation completed!  Exiting..." 

exit 0

