#!/bin/bash
. /etc/appliance/scriptsLib-3.0/maintenance.lib

# XXXX COMMON STUFF checks if install/upgrade

# checks for maintenance_state for previous attempts

isMaintenance=`isMaintenanceMode`
if [ $isMaintenance -ne 1 ]; then
    echo "[`date`] set_post_maintenance: Not in maintenance mode" >> $ERROR_LOG
    exit 1
fi

#remove the install counter for service
if [ "$1" ]; then
   rm -f /etc/virtualhosting/maintenance/install/$1
    echo "[`date`] set_post_maintenance: $1 action completes">> $ERROR_LOG
fi

state=`getMaintenanceState`
if [ $? -ne 0 ]; then
    echo "[`date`] set_post_maintenance: Unable to determine maintenance state" >> $ERROR_LOG
    exit 1
fi

case "$state" in
    $STATE_MAINTENANCE)
	;;
    $STATE_PRE_MAINTENANCE)
	;;
    $STATE_POST_MAINTENANCE)
	;;
    *)
	echo "[`date`] set_post_maintenance: Unexpected state" >> $ERROR_LOG
	exit 1
esac

#check if all add-on installations are complete
service=`ls /etc/virtualhosting/maintenance/install/`
if [ "$service" ]; then
    echo "[`date`] [$1] set_post_maintenance: Installing $service still not complete" >> $ERROR_LOG
else
    setMaintenanceState $STATE_POST_MAINTENANCE
fi

