#!/bin/bash
#
# Perform any post-install operations, e.g. creating SRs
#
# chkconfig: 2345 24 76
# description: Perform post-install operations, e.g. creating SRs

. /etc/init.d/functions

export FIRSTBOOT_SCRIPTS_DIR=/etc/firstboot.d
export FIRSTBOOT_DATA_DIR=/etc/firstboot.d/data
export FIRSTBOOT_STATE_DIR=/etc/firstboot.d/state
export FIRSTBOOT_LOG_DIR=/etc/firstboot.d/log
export XENSOURCE_INVENTORY=/etc/xensource-inventory
export XE=/opt/xensource/bin/xe

SCRIPTS="$(find ${FIRSTBOOT_SCRIPTS_DIR} -maxdepth 1 -mindepth 1 -type f -perm -100 | sort)"

## STATE MANAGEMENT
get_statefile_name() {
    echo "${FIRSTBOOT_STATE_DIR}/$(basename $1)"
}

mark_started() {
    echo "started $(date)" >$(get_statefile_name $1)
}

mark_success() {
    echo "success $(date)" >$(get_statefile_name $1)
}

mark_fail() {
    echo "failed $(date)" >$(get_statefile_name $1)
}

mark_new() {
    rm -f $(get_statefile_name $1)
}

get_state() {
    if [ ! -f "$(get_statefile_name $1)" ]; then
        echo "new"
    else
        awk '{print $1;}' $(get_statefile_name $1)
    fi
}

get_logfile_name() {
    echo ${FIRSTBOOT_LOG_DIR}/$(basename $1).log
}

get_runlist() {
    localrl=""
    for s in ${SCRIPTS} ; do
        if [ $(get_state $s) == "new" ]; then
            rl="$rl $(basename $s)"
        fi
    done
    echo $rl
}

start() {
    echo -n $"Performing remaining startup actions: "
    
    # check that xapi is running:
    if ! pidof xapi &>/dev/null ; then
        problem="xapi is not running"
        echo "${problem}"
        failure ${problem}
        echo
        return 2
    fi

    # make sure we never do this twice:
    RUNLIST=$(get_runlist)
    if [ -n "${RUNLIST}" ] ; then
        for s in $(get_runlist) ; do
            mark_started ${s}
            if ! bash -x ${FIRSTBOOT_SCRIPTS_DIR}/${s} start &>$(get_logfile_name ${s}) ; then
                problem="Failed: $(basename ${s})"
                mark_fail ${s}
                echo -n ${problem}
                failure ${problem}
                echo
            else
                mark_success ${s}
            fi
        done
    fi

    success; echo
}

status() {
    for s in ${SCRIPTS} ; do
        STATE="$(get_state ${s})"
        echo "$(basename ${s}): ${STATE}"
    done
}

stop() {
    :
}

restart() {
    stop
    start
}

activate() {
    grep -sv '^UPGRADE=' ${FIRSTBOOT_DATA_DIR}/host.conf >/tmp/host.conf.$$
    rm -f ${FIRSTBOOT_DATA_DIR}/host.conf
    [ -s /tmp/host.conf.$$ ] && mv -f /tmp/host.conf.$$ ${FIRSTBOOT_DATA_DIR}/host.conf
    rm -f /tmp/host.conf.$$

    # CA-21443: restore initial management interface configuration
    if [ -d /etc/firstboot.d/data/initial-ifcfg ] ; then
	if [ -r /etc/firstboot.d/data/initial-ifcfg/network.dbcache ] ; then
	    cp -fp /etc/firstboot.d/data/initial-ifcfg/network.dbcache /var/xen/network.dbcache
	else
            for f in /etc/sysconfig/network-scripts/ifcfg* ; do
		[ -e $f -a $f != '/etc/sysconfig/network-scripts/ifcfg-lo' ] && rm -f $f
	    done
	    cp -fp /etc/firstboot.d/data/initial-ifcfg/* /etc/sysconfig/network-scripts
	fi
    fi

    echo -n $"Activating firstboot scripts: "
    for i in ${SCRIPTS} ; do
        mark_new $i
    done
    echo
    success
    /sbin/reboot
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status
        ;;
    restart)
        restart
        ;;
    activate)
        activate
        ;;
    *)
        echo "Unknown action '$1'."
        ;;
esac
