#
# 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.
#
# 

disable_vds(){
    #disable all virtualdomains
    set_context "Disabling all virtual domains"
    exe "rm -f /tmp/myvirtualdomains.$$"
    exe "/usr/local/bin/ListAllVirtDomains > /tmp/myvirtualdomains.$$"
    myvds=$(cut -d ' ' -f 1 /tmp/myvirtualdomains.$$)
    for vd in  $myvds
    do
	exe "/usr/local/bin/DisableVirtDomain $vd"
    done
    exe "rm -f /tmp/myvirtualdomains.$$"
 
}
 
handle_nss() {
    local file=/etc/nsswitch.conf

    if [ -f $file ]; then
	egrep '^[[:blank:]]*passwd:[[:blank:]]*ensimvwh.*' $file > /dev/null
	if [ $? = 0 ]; then
	    set_context "Removing ensimvwh from $file"
	    exe "sed 's/\(^[[:blank:]]*passwd:[[:blank:]]*\)ensimvwh\(.*\)/\1\2/' $file > /tmp/nsswitch.conf.$$"
	    set_context "Committing modification to $file"
	    exe "mv -f /tmp/nsswitch.conf.$$ $file"
	else
	    set_context "$file doesn't contain ensimvwh"
	fi
    fi
}

remove_tmpdirs() {
    set_context "Removing temporary file directories"
    exe "if [ -d /etc/virtualhosting/tmp ]; then rm -rf /etc/virtualhosting/tmp; fi"
    exe "if [ -d /home/virtual/FILESYSTEMTEMPLATE/.tmp ]; then rm -rf /home/virtual/FILESYSTEMTEMPLATE/.tmp; fi"
    set_context "Removing tmpdirs mapping file"
    exe "rm -f /etc/virtualhosting/tmpdirs"
}
 
pre_uninstall(){
    set_context "Uninstall $current_version"
    disable_vds
    set_context "Restoring the motd file"
    # restore the original motd file
    if [ -e /etc/motd.webppliancesave ] ; then
	exe "restore_srv_file /etc/motd"
    fi
    set_context "Stopping virtualhosting service"
    #Dont exit the process if stopping failed > /dev/null 2>&1
    /etc/rc.d/init.d/virtualhosting stop
    set_context "Removing virtualhosting from chkconfig management"
    exe "/sbin/chkconfig --del virtualhosting"
    set_context "Removing VHAppConf.pm.orig"
    save_remove /usr/lib/opcenter/virtualhosting/VHAppConf.pm.orig
    restore_init_script "syslog" "_vwh_init" "restart"
    handle_nss
    remove_tmpdirs
}


remove_srv() {
    pre_uninstall
}

upgrade_srv() {
    :
}
. /etc/appliance/scriptsLib-3.0/common.lib

G_WP_SRV_NAME=virtualhosting

if [ "$1" = "0" ]; then
    /usr/bin/edit_svcdb -d $G_WP_SRV_NAME
    setIgnoreError 1
    remove_srv
elif [ "$1" ]; then
    upgrade_srv
fi

script_exit_success "preun"
