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

install_srv(){

    # restart sendmail after setting up ensim sysv script
    setup_ensim_sys_v_script "sendmail" "_app_init" "" "restart"

    set_context "Backing up /etc/mail/sendmail.cf to /etc/mail/sendmail.cf.ensim"
    exe "backup_srv_file /etc/mail/sendmail.cf"
#    save_copy /etc/mail/sendmail.cf /etc/mail/sendmail.cf.ensim
 
    set_context "Generating Ensim's /etc/mail/sendmail.cf file"
    exe "/usr/bin/m4 $OCW_BASE/sendmail/install/sendmail.mc > /etc/mail/sendmail.cf"
#    exit_on_failure $? "/usr/bin/m4 $OCW_BASE/sendmail/install/sendmail.mc > /etc/mail/sendmail.cf"

    # Replace PAM configuration so that we can use SMTP AUTH
    set_context "Configuring /etc/pam.d/smtp to manage smtp"
    exe "backup_pam_srv_config 'smtp'"
    rm -rf $PAM_CONFIG_DIR/smtp
    exe "cp -f $OCW_BASE/sendmail/install/smtp.pam $PAM_CONFIG_DIR/smtp"

    # Needed to force sendmail+saslauthd to use PAM for authentication
    egrep '^[[:blank:]]*MECH=.*' /etc/sysconfig/saslauthd > /dev/null
    if [ $? != 0 ]; then
        set_context "Adding MECH=pam to /etc/sysconfig/saslauthd"
        exe "echo 'MECH=pam' >> /etc/sysconfig/saslauthd"
    fi
    chkconfig saslauthd on
    /etc/rc.d/init.d/saslauthd restart
}

upgrade_srv() {
    # Copy the old sendmail.cf to the new sendmail.cf location 
    old_sendmail_cf="/etc/sendmail.cf.rpmsave"
    new_sendmail_cf="/etc/mail/sendmail.cf"
    if [ -f $old_sendmail_cf ]; then
        exe "cp $old_sendmail_cf $new_sendmail_cf"
    else
        exit_on_failure "0" "Cannot find the old sendmail.cf in the /etc directory"
    fi
     
    # need to remove "AutoRebuildAliases" option from sendmail.cf
    # as it is deprecated now.
    sed1="s/O.*AutoRebuildAliases.*/#O AutoRebuildAliases/"
    sed -e "$sed1" $new_sendmail_cf >> $new_sendmail_cf.$$
    exit_on_failure $? "Failed: sed -e '$sed1'"
    exe "mv -f $new_sendmail_cf.$$ $new_sendmail_cf" 
 
    # Need to generate sendmail.cf.
    #set_context "Generating Ensim's /etc/mail/sendmail.cf file"
    #exe "/usr/bin/m4 $OCW_BASE/sendmail/install/sendmail.mc > /etc/mail/sendmail.cf"

    # Needed to force sendmail+saslauthd to use PAM for authentication
    egrep '^[[:blank:]]*MECH=.*' /etc/sysconfig/saslauthd > /dev/null
    if [ $? != 0 ]; then
        set_context "Adding MECH=pam to /etc/sysconfig/saslauthd"
        exe "echo 'MECH=pam' >> /etc/sysconfig/saslauthd"
    fi
    chkconfig saslauthd on
    /etc/rc.d/init.d/saslauthd restart
}
. /etc/appliance/scriptsLib-3.0/common.lib

G_WP_SRV_NAME=sendmail

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"
