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

handle_nss() {
    local file=/etc/nsswitch.conf

    if [ ! -f ${file}.ensimsave ]; then
	if [ -f $file ] ; then
	    set_context "Backing up $file to ${file}.ensimsave"
	    #just overwrite backup file if exists
	    backup_srv_file "$file" 1
	fi
    fi

    test -f $file
    exit_on_failure $? "test -f $file"

    # Determine if file already has modification, and if it does,
    # it must be the last passwd line
	
    egrep '^[[:blank:]]*passwd:[[:blank:]]*.*' $file | tail -n 1 | \
	egrep '^.*:[[:blank:]]*ensimvwh.*' > /dev/null
    if [ $? != 0 ]; then
	set_context "Adding ensimvwh to $file"
	exe "sed 's/\(^[[:blank:]]*passwd:[[:blank:]]*\)\(.*\)/\1ensimvwh \2/' $file > /tmp/nsswitch.conf.$$"
	set_context "Committing modification to $file"
	exe "mv -f /tmp/nsswitch.conf.$$ $file"
    else
	# Writing this into the log file instead of obsoleted install_history
	# XXX Is this even needed???
	set_context "$file already constains ensimvwh"
    fi
}

handle_post_install(){

    /usr/bin/update_ocwinodes
    local REPOS=$SCRIPTDIR/virtualhosting

    #create_vwh_files
    # restart syslog after setting up ensim sysv script
    setup_ensim_sys_v_script "syslog" "_vwh_init" 1 "restart"
    handle_nss

    set_context "Configuring the /etc/motd file"
    # copy over motd file
    if [ -e /etc/motd ] ; then
	exe "backup_srv_file /etc/motd"
    fi
    exe "cp -f ${OCW_BASE}/virtualhosting/install/motd /etc/motd"
    exe "chmod 0644 /etc/motd"

    set_context "Creating the file containing the list of file system template inodes"

}

create_shadow() {
    # create the shadow file
    set_context "Creating the shadow file"
    exe "/usr/sbin/pwconv > /dev/null"
} 

create_tmpdirs() {
    # create the tmpdirs file
    set_context "Creating the tmpdirs mapping file"
    exe "touch /etc/virtualhosting/tmpdirs"
    exe "chmod 0755 /etc/virtualhosting/tmpdirs"
    exe "echo \"/etc:/etc/virtualhosting/tmp\" >> /etc/virtualhosting/tmpdirs"
    exe "echo \"/home:/home/virtual/FILESYSTEMTEMPLATE/.tmp\" >> /etc/virtualhosting/tmpdirs"
}

add_table() {
cat << EOF | psql appldb
\set ON_ERROR_STOP

CREATE TABLE siteinfo (
  site_id INT PRIMARY KEY NOT NULL,
  domain varchar(256),
  email varchar(256),
  admin_user varchar(256)
);
CREATE UNIQUE INDEX domain_key on siteinfo (domain);
INSERT INTO siteinfo (site_id, domain, email) VALUES (0, '', '');
EOF
}


install_srv() {
    # add_table needs to be before syslog scripts
    add_table
    handle_post_install
    create_shadow
    create_tmpdirs
    config_sys_v_init_srv "virtualhosting" 1

    # publisher expecting virthost instead of virtualhosting
    G_WP_SRV_NAME="virthost"
}

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

G_WP_SRV_NAME=virtualhosting

if [ "$1" = "1" ]; then
    install_srv
elif [ "$1" ]; then
    upgrade_srv
fi

flush_context 0

set_context "Creating the service database entry"
exe "/usr/bin/edit_svcdb -i $G_WP_SRV_NAME"

script_exit_success "post"
