#!/bin/bash
#
# Xrnginstall - install image during both external/internal pxe boot
# 
# This script is included in hostos rpm as well as initrd.img in 
# host.iso. It overrides the one that comes from OS-SDK during build time.
#
# Copyright (c) 2014-2015 by Cisco Systems, Inc.

# Source function library.
. /etc/init.d/functions
. /etc/init.d/disk-functions
. /etc/init.d/spirit-functions

readonly LOGDIR=/var/log
readonly LOGFILE=host-install.log
readonly LOGFILE0=host-install0.log
USE_FINAL=0

function redirect_output
{
    local logfile
    logfile=$1
    exec 107> >( exec cat > $logfile )
    redirect_output_pid=$!
    exec 105>&1
    exec 106>&2
    exec 1> >( exec tee -a /dev/fd/107 )
    exec 2> >( exec tee -a /dev/fd/107 >&2 )
    export LVM_SUPPRESS_FD_WARNINGS=1
}

function unredirect_output
{
    exec 1>&105-
    exec 2>&106-
    exec 107>&-
    if [ -n "$redirect_output_pid" ]; then
        kill -HUP $redirect_output_pid
        while [ -e /proc/$redirect_output_pid ]; do :; done
    fi
}

# Only read this once.
cmdline=$(cat /proc/cmdline)

# -----------Should this script run?----------------
# This script is only used for initial 'install' RAM boot phase.
# Exit if we are not in install phase.
INSTALLDEV=`cat /proc/cmdline | grep install=`
if [ ! -n "$INSTALLDEV" ]; then
  exit 0
fi

redirect_output $LOGDIR/$LOGFILE

function step_log_console {
    local log_msg=${1}
    local time_str=$(date)
    echo "${time_str}: ${log_msg}"
    if [ $USE_FINAL -gt 0 ]; then
        sync
    fi
}
readonly -f step_log_console

function step_log_file {
    local log_msg=${1}
    local time_str=$(date)
    echo "${time_str}: ${log_msg}" >&107
    if [ $USE_FINAL -gt 0 ]; then
        sync
    fi
}
readonly -f step_log_file

# -----------Can script pass control to a better one?----------
# This script comes from thirdparty lineup and is too much effort
# to modify and release. If possible, locate new version of script
# (maintained in calvados lineup and embedded in hostos RPM) and 
# pass control to it. Implementation: expand detected RPMs, and 
# locate spirit-install-cal.sh PI script. 
# When expanding RPM, exclude bulky libs and binaries we know we
# dont need.

redirect_output $LOGDIR/$LOGFILE

if [ -f /iso/$HOST_ISO ]; then
    # external pxe boot with bundle iso
    step_log_console "Detected /iso/$HOST_ISO"
    ISOMNT=$(mktemp -d /tmp/isomnt.XXXXXX)
    mount -o loop /iso/$HOST_ISO ${ISOMNT} || exitclean	    
    step_log_console "Mounted /iso/$HOST_ISO to ${ISOMNT}"
    
    HOSTOS_RPM_SRC=`ls -1 ${ISOMNT}/rpm/*hostos.all*.rpm | head -1`
    if [ -n "$HOSTOS_RPM_SRC" ]; then
        step_log_console "Found $HOSTOS_RPM_SRC in host.iso"
        step_log_console "Installing $HOSTOS_RPM_SRC"
        rpm -iv --nodeps --ignoresize --excludepath /opt/cisco/hostos $HOSTOS_RPM_SRC
    else
        step_log_console "Error, could not find  ${ISOMNT}/rpm/$HOST_ISO file"
    fi

    # unmount ISO, we no longer need it
    [ -d "${ISOMNT}" ] && umount ${ISOMNT} && rmdir ${ISOMNT}
fi

# internal pxe boot with initrd.img
# if newer version of script available, pass control to it
NEWER_INSTALL_SCRIPT=/etc/init.d/pxe_install.sh
if [ -f $NEWER_INSTALL_SCRIPT ]; then 
    step_log_console "Passing control to $NEWER_INSTALL_SCRIPT $1"
    # Stop writing to log, save log to a different file.
    unredirect_output
    cp -p $LOGDIR/$LOGFILE $LOGDIR/$LOGFILE0
    $NEWER_INSTALL_SCRIPT $1
    exit 0;
fi
