#!/bin/bash
set -e

[ -e /run/boottime.stamp ] || touch /run/boottime.stamp

. /etc/xensource-inventory

unset IP
unset BANNER
unset HOSTTYPE

if [ ! -z "${PRODUCT_VERSION_TEXT}" ]; then
    BANNER="${COMPANY_PRODUCT_BRAND} ${PRODUCT_VERSION_TEXT}"
    HOSTTYPE="${PRODUCT_BRAND} host"
    if [ -n "${BRAND_CONSOLE_URL}" ]; then
        MANAGE="To manage this server please use Xen Orchestra.
You can install Xen Orchestra from https://xen-orchestra.com."
    else
        MANAGE="To manage this server please use Xen Orchestra.
You can install Xen Orchestra from https://xen-orchestra.com."
    fi

else
    BANNER="Xen.org XCP Host ${PLATFORM_VERSION}"
    HOSTTYPE="XCP Host"
    MANAGE="To manage this server please use the xe command line utility."
fi


[ -z "$MANAGEMENT_INTERFACE" ] || {
   IP=$(ip addr show dev $MANAGEMENT_INTERFACE | awk '/ inet / { print $2 }')
   IP=${IP%%/*}
}
: ${IP:="IP address not configured"}

unset FINGERPRINT
if [ -f /etc/xensource/xapi-ssl.pem ] ; then
    FINGERPRINT=`openssl x509 -in /etc/xensource/xapi-ssl.pem -fingerprint -noout`
    FINGERPRINT=${FINGERPRINT#SHA1 Fingerprint=}
    FINGERPRINT_SHA2=`openssl x509 -in /etc/xensource/xapi-ssl.pem -fingerprint -noout -sha256`
    FINGERPRINT_SHA2=${FINGERPRINT_SHA2#SHA256 Fingerprint=}
fi
cat > $mnt/etc/issue <<EOF
${BANNER}

System Booted: `date -r /run/boottime.stamp +"%F %R"`

Your ${HOSTTYPE} has now finished booting. 
${MANAGE}

You can connect to this system using one of the following network
addresses:

$IP

EOF

if [ -n "${FINGERPRINT}" ] ; then
    cat >> $mnt/etc/issue <<EOF
${HOSTTYPE} TLS certificate SHA1 fingerprint:
$FINGERPRINT

${HOSTTYPE} TLS certificate SHA256 fingerprint:
$FINGERPRINT_SHA2

EOF
fi

exit 0

