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

quota_enable_dir="/home" 

handle_quota() {
    set_context "Enabling Quota support"

    MOUNT_PT=`$OCW_BASE/appliance/install/set_tab_quota $quota_enable_dir`
    LINUX_VER=`uname -r | sed -e 's/^\([^\.]*\.[^.]*\).*$/\1/'`
 
    local QUOTACHECK_FLAG=""
    local SET_GRACE_PERIOD_CMD=""

    if [ $LINUX_VER = "2.2" ]
    then
	QUOTACHECK_FLAG="-F vfsold -ug"
	SET_GRACE_PERIOD_CMD="$OCW_BASE/appliance/install/set_quota_grace"

        exe "/bin/touch $MOUNT_PT/quota.user $MOUNT_PT/quota.group"
     
    elif [ $LINUX_VER = "2.4" ]
    then
    # we use quotacheck for only the mountpoint of the filesystem owning the
    # /home directory, hence the -a flag should not be specified (PR 26117)
	QUOTACHECK_FLAG="-mug"
	
	# need to make sure zone-length file of quota.user and quota.group
	# does not exist
	if [ -f $MOUNT_PT/quota.user ] && ! [ -s $MOUNT_PT/quota.user ];
	then
	    log_warning "removing zero length $MOUNT_PT/quota.user"
	    rm -f $MOUNT_PT/quota.user
	fi

	if [ -f $MOUNT_PT/quota.group ] && ! [ -s $MOUNT_PT/quota.group ];
	then
	    log_warning "removing zero length $MOUNT_PT/quota.group"
	    rm -f $MOUNT_PT/quota.group
	fi

        # $MOUNT_PT is the file system mount point for $quota_enable_dir
	SET_GRACE_PERIOD_CMD="/usr/sbin/setquota -t 0 0 $MOUNT_PT"

        exe "/bin/touch $MOUNT_PT/aquota.user $MOUNT_PT/aquota.group"
    fi

    setIgnoreError 1
    
    # make sure quota is off
    exe "/usr/local/sbin/zaphda1"
    exe "/sbin/quotaoff -auvg"
    exe "/sbin/quotacheck $QUOTACHECK_FLAG $MOUNT_PT"
    exe "/sbin/quotaon  -ugav"

    setIgnoreError ""

    set_context "Setting quota grace period to zero"
    exe "$SET_GRACE_PERIOD_CMD"
}

handle_syslog(){
    set_context "Configuring syslog configuration file"
    SYSLOGFILE='/etc/syslog.conf'

    set_context "Configuring main log (ensim_appliance.log)"
    local log1="local1.info /var/log/appliance/ensim_appliance.log"
    local hasLine=`egrep -v "^\s*#" $SYSLOGFILE | egrep "local1\."`
    if [ ! "$hasLine" ] ; then
        exe "echo -e \"$log1\" >> $SYSLOGFILE"
    else
        log_warning "could not register ensim_appliance.log to syslog.conf (local1)"
	log_warning "local1 already exists: $hasLine"
    fi

    set_context "Configuring setup debugging log (setup.log)"
    local log2="local2.info /var/log/appliance/setup.log"
    local hasLine=`egrep -v "^\s*#" $SYSLOGFILE | egrep "local2\."`
    if [ ! "$hasLine" ] ; then
        exe "echo -e \"$log2\" >> $SYSLOGFILE"
    else
        log_warning "could not register setup.log to syslog.conf (local2)"
	log_warning "local2 already exists: $hasLine"
    fi

    # This is done here since database creation below echoes messages
    exe "touch /var/log/appliance/setup.log"
    exe "chmod 0600 /var/log/appliance/setup.log"

    set_context "Configuring setup error log (setup.err)"
    local log3="local3.info /var/log/appliance/setup.err"
    local hasLine=`egrep -v "^\s*#" $SYSLOGFILE | egrep "local3\."`
    if [ ! "$hasLine" ] ; then
        exe "echo -e \"$log3\" >> $SYSLOGFILE"
    else
        log_warning "could not register setup.err to syslog.conf (local3)"
	log_warning "local3 already exists: $hasLine"
    fi

    /etc/rc.d/init.d/syslog restart 1> /dev/null 2> /dev/null

    # We only append messages to the history file using echo
    # Since we are not using syslog for this file, better create and change
    # permissions here
    exe "touch /var/log/appliance/history"
    exe "chmod 0600 /var/log/appliance/history"

}


install_langfiles() {
    # Here we need to install the language files. Here's how it works:
    # the top-level Makefile (in $WebGui) will put all the language files 
    # in the /usr/lib/locale/<lang>/LC_MESSAGES directory. what we are
    # supposed to do here is create symlinks to the above files into the
    # /usr/share/locale/<lang>/LC_MESSAGES directory also. this is because
    # the locale handling has changed slightly in the newer versions of glibc

    pkg_fe=`find /usr/lib/locale -type f -name frontend.mo`
    for i in $pkg_fe
    do
        glibccompatdir=`dirname $i |sed 's/^\/usr\/lib\//\/usr\/share\//g'`
        if [ -e $glibccompatdir ]
        then
            # directory already exists. make a symlink to message file
            [ -e $glibccompatdir/frontend.mo ] || ln -sf $i $glibccompatdir
        else
            # directory does not exist, make a symlink to the directory itself
            lang=`echo $i |cut -d/ -f5`
            [ -e /usr/share/locale/$lang ] || ln -sf /usr/lib/locale/$lang /usr/share/locale/
        fi
    done
}

install_srv() {

    # language files are separate now
    # install_langfiles

    # restart postgresql after setting up ensim sysv script
    rh_detect

    #VERSION_OS=`cat /etc/redhat-release | cut -d " " -f 5`

    if [ "$rhOS" = "RHEL" ]
    then
       srv_name="rhdb"
    elif [ "$rhOS" = "Fedora" ]
    then
       srv_name="postgresql"
    fi

    setup_ensim_sys_v_script "$srv_name" "_app_init" "" "restart"

    handle_syslog
    handle_quota
    #/etc/rc.d/init.d/postgresql stop 1> /dev/null 2> /dev/null
    #/etc/rc.d/init.d/postgresql start 1> /dev/null 2> /dev/null
    /etc/rc.d/init.d/$srv_name stop 1> /dev/null 2> /dev/null
    /etc/rc.d/init.d/$srv_name start 1> /dev/null 2> /dev/null

    /sbin/chkconfig $srv_name on 1> /dev/null 2> /dev/null
    #/sbin/chkconfig postgresql on 1> /dev/null 2> /dev/null
    /sbin/chkconfig --add atd 1> /dev/null 2> /dev/null
    /sbin/chkconfig atd on 1> /dev/null 2> /dev/null
    /etc/rc.d/init.d/atd start 1> /dev/null 2> /dev/null

    su - postgres -c "createuser --createdb -q --no-adduser root" 1> /dev/null 2> /dev/null

    # Issue: switching between ensim-appliance and ensim-appliance-l.
    #
    # Details: These two packages obsolete each other, but due to the
    # way obsolete works, what effectively happens is that the new
    # package thinks it is being freshly installed, and the old package
    # thinks it is being removed. The removal is not problematic, as
    # the only thing it does is restore the init script. The install
    # does cause problems, because we reset the database.
    #
    # Solution: Each of the two rpms carries a triggerpostun for the
    # other. When being installed, we will back up the database into
    # /tmp before resetting it. If the triggerpostun is run, we will
    # then restore the database.
    # NOTE: anything you change here must be compatible with the
    # triggerpostun's for ensim-applianc[-l]
    #/etc/rc.d/init.d/postgresql restart
    /etc/rc.d/init.d/$srv_name restart
    rm -f /tmp/appldb.out.gz
    psql appldb < /dev/null > /dev/null 2>&1
    if [ $? = 0 ]; then
	pg_dump appldb | gzip --best -c > /tmp/appldb.out.gz 2> /dev/null
        if [ ${PIPESTATUS[0]} != 0 -o ${PIPESTATUS[1]} != 0 ]; then
	    rm -f /tmp/appldb.out.gz
        else
	    dropdb -q appldb 1> /dev/null 2> /dev/null
        fi
    else
        dropdb -q appldb 1> /dev/null 2> /dev/null
    fi
    
    set_context "Initializing database"
    log_warning "Initializing database"
    exe "createdb -q appldb >> $G_VERBOSE_LOG"

    /etc/rc.d/init.d/xinetd restart 1> /dev/null 2> /dev/null

}

upgrade_srv() {

    if [ -f /etc/rc.d/init.d/bandwidth_manager ]; then 
       /etc/rc.d/init.d/bandwidth_manager stop
    fi
    rh_detect

    #VERSION_OS=`cat /etc/redhat-release | cut -d " " -f 5`

    if [ "$rhOS" = "RHEL" ]
    then
       srv_name="rhdb"
    elif [ "$rhOS" = "Fedora" ]
    then
       srv_name="postgresql"
    fi

    # restart postgresql after setting up ensim sysv script
    setup_ensim_sys_v_script "$srv_name" "_app_init" "" "restart"

    if [ -f /etc/rc.d/init.d/bandwidth_manager ]; then 
       /etc/rc.d/init.d/bandwidth_manager start
    fi

}
. /etc/appliance/scriptsLib-3.0/common.lib

G_WP_SRV_NAME=appliance

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

flush_context 0

script_exit_success "post"
