#!/bin/bash
_DEBUG="1"
. /etc/appliance/scriptsLib-3.0/maintenance.lib

enable_svc() {
    local svc=$1
    local marker_dir=/etc/virtualhosting/maintenance/markers
    if [ -f $marker_dir/$svc ]; then
        chkconfig $svc on
	/etc/rc.d/init.d/$svc restart 
        rm -f $marker_dir/$svc
    fi
}

disable_svc() {
    local svc=$1
    local marker_dir=/etc/virtualhosting/maintenance/markers
    if chkconfig $1; then
        touch $marker_dir/$svc
        chkconfig $svc off
    fi
}

disable_svc crond

log_messages 2 "POST_MAINTENANCE" "Starting exit_post_maintenance"
if [ $_DEBUG == "1" ] ; then
    echo "Starting exit_post_maintenance"
fi

# XXXX COMMON STUFF checks if expected state

log_messages 1 "Inside exit_post_maintenance"
# checks for maintenance_state for previous attempts
state=`getMaintenanceState`

if [ $? -ne 0 ]; then
    echo "Unable to determine maintenance state"
    log_messages 1 "POST_MAINTENANCE" "Unable to determine maintenance state"
    exit 1
fi

if [ $_DEBUG == "1" ] ; then
    echo "Maintenance State is : $state"
fi

if [ $state -ne $STATE_POST_MAINTENANCE ]; then
    # nothing to be done here
    if [ $_DEBUG == "1" ]; then
        echo "nothing to be done here ... State is $state"
    fi
    exit 0
fi

# Prelinking can wreak havoc on FST updates; so don't run if it is
# running, and prevent it from running temporarily if it isn't
if [ -f /usr/sbin/prelink ]; then
  if [ -f /etc/prelink.conf ]; then
    # Save the conf file and truncate the original
    /bin/cp -f /etc/prelink.conf{,.ensim}
    /bin/echo "" > /etc/prelink.conf
  fi

  if pidof prelink >/dev/null 2>&1; then
    # It's already running; bail out
    if [ -f /etc/prelink.conf.ensim ]; then
      /bin/mv /etc/prelink.conf{.ensim,}
    fi
    log_messages 1 "POST_MAINTENANCE" "prelinking is currently in progress; please try again later"
    exit 1
  fi
fi

# SXC private server hack to fix /dev/hda1 not pointing to correct dev
# (21761)
/usr/local/sbin/zaphda1

# Ugly, but we really need to make sure that quotas are off
/sbin/quotaoff -aug

/usr/bin/ensim-python /usr/local/sbin/UpgradeActiveVirtDomains >> $SETUP_LOG 2>&1
# virthost.SUCCESS =0, virthost.WARNING =1, virthost.ERROR =2
if [ $? -ge 2 ]; then
    log_messages 1 "POST_MAINTENANCE" "Upgrading domains failed"
    exit 1
fi

# Restore the prelink configuration
if [ -f /etc/prelink.conf.ensim ]; then
  /bin/mv /etc/prelink.conf{.ensim,}
fi

# Clean up the flags left by the fst RPMs
rm -f  /etc/virtualhosting/filelists/*.old
rm -rf /etc/virtualhosting/templates/*.old

for service in httpd proftpd sendmail bandwidth_manager tomcat4 MailScanner; do
  if /sbin/chkconfig $service 1>/dev/null 2>&1; then
    /etc/rc.d/init.d/$service restart
  fi
done

enable_svc crond

/sbin/quotaon -aug

# enable all the gui/CLI

# exit the maintenance mode

exitMaintenanceMode 
if [ $? -ne 0 ]; then
    log_messages 1 "POST_MAINTENANCE" "Could not exit post maintenance mode"
    exit 1
fi

log_messages 0 "POST_MAINTENANCE" "Successfully left post maintenance mode"
if [ $_DEBUG == "1" ]; then
    echo "SUCCESS ... POST_MAINTENANCE"
fi
