#!/bin/sh
###
#
# 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.
#
# --------------------------------------------------------------------------
# $Id: virtualhosting_detectchanges,v 1.11 2003/03/04 23:39:52 amit Exp $
# $Name:  $
# --------------------------------------------------------------------------

perlscript=""


ipchange=`/bin/egrep "^IP" /etc/detectchanges/configurationchanged 2>/dev/null`
ipflag=`echo $?`
if [ $ipflag = 0 ]
then
    oldIP=`echo $ipchange | /bin/awk '{print $2}'`
    newIP=`echo $ipchange | /bin/awk '{print $3}'`
fi
hostchange=`/bin/egrep "^hostname" /etc/detectchanges/configurationchanged 2>/dev/null`
hostflag=`echo $?`
if [ $hostflag = 0 ] || [ $ipflag = 0 ]
then
    oldname=`echo $hostchange | /bin/awk '{print $2}'`
    newname=`echo $hostchange | /bin/awk '{print $3}'`
    
    #edit the Redirects
    for i in `ls /etc/httpd/conf/virtual`
    do
    /bin/rm -f /tmp/httpdcnf.$$
    /usr/bin/eperl -pe "s/Redirect\s+(\S+)\s+https:\/\/$oldIP:19638/Redirect \$1 https:\/\/$newIP:19638/; "</etc/httpd/conf/virtual/$i>/tmp/httpdcnf.$$
    mv -f /tmp/httpdcnf.$$ /etc/httpd/conf/virtual/$i
    done
fi
