#!/bin/bash
#
# Perform any post-install operations, e.g. creating SRs

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)"

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() {
    # make sure we never do this twice:
    RUNLIST=$(get_runlist)
    if [ -n "${RUNLIST}" ] ; then
        for s in $(get_runlist) ; do
            mark_started ${s}
            head -1 ${FIRSTBOOT_SCRIPTS_DIR}/${s} | grep -q '/bin/bash' ; rc=$?
            if [ ${rc} -eq 0 ] ; then
                bash -x ${FIRSTBOOT_SCRIPTS_DIR}/${s} start &>$(get_logfile_name ${s}) ; rv=$?
            else
                ${FIRSTBOOT_SCRIPTS_DIR}/${s} start &>$(get_logfile_name ${s}) ; rv=$?
            fi
            if [ ${rv} -eq 0 ] ; then
                mark_success ${s}
            else
                mark_fail ${s}
                echo "Failed: $(basename ${s})"
            fi
        done
    fi
}

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

reset_and_reboot() {
    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 "Resetting firstboot state and rebooting"
    for i in ${SCRIPTS} ; do
        mark_new $i
    done
    /sbin/reboot
}

case "$1" in
    start)
        start
        ;;
    status)
        status
        ;;
    reset-and-reboot)
        reset_and_reboot
        ;;
    *)
        echo "Unknown action '$1'."
        ;;
esac
