#!/bin/sh

. /etc/rc.subr

name="onapp-run"
start_cmd="onapp-run_start"
stop_cmd=":"

onapp-run_start()
{   
    ONAPP_SRC="/onapp.src"
    ONAPP_DST="/onapp.dst"
    mkdir -p $ONAPP_SRC $ONAPP_DST
    #echo "Mounting /dev/ad1 ${ONAPP_SRC} ufs rw"
    echo "Mounting /dev/ad1 ${ONAPP_SRC}  ext2fs rw"
    #mount -t ufs -o rw /dev/ad1 ${ONAPP_SRC}
    mount -t ext2fs -o rw /dev/ad1 ${ONAPP_SRC}
    if [ $? -ne 0 ]; then
        exit 0
    fi

    if [ -e ${ONAPP_SRC}/onapp-freebsd-prepare.conf ]; then
        echo "Running with some post-start OnApp preconfiguration tasks"
        VIF='xn0'
        VIRTUALIZATION='xen'
	IPADDR=
	NETMASK=
	BROADCAST=
	GATEWAY=
	ROOT_PASSWD=

        . ${ONAPP_SRC}/onapp-freebsd-prepare.conf
        if [ "x${VIRTUALIZATION}" = "xkvm" ]; then
            VIF='re0'
        fi
        if [ "x${VIRTUALIZATION}" = "xxen" ]; then
            VIF='xn0'
        fi
        if [ "x${IPADDR}" != "x" -a "x${NETMASK}" != "x" -a "x${BROADCAST}" != "x" -a "x${GATEWAY}" != "x" ]; then
            echo "Configuring network ..."
            ifconfig ${VIF} ${IPADDR} netmask ${NETMASK} broadcast ${BROADCAST}
	    route add default ${GATEWAY}
        fi
        if [ "x${ROOT_PASSWD}" != "x" ]; then
            echo "Setting root user password"
            sh -c "chpass -p '${ROOT_PASSWD}' root"
        fi
    fi
   
    echo "Running OnApp tasks for FreeBSD"
    if [ -e ${ONAPP_SRC}/onapp-freebsd-prepare.sh ]; then
        sh ${ONAPP_SRC}/onapp-freebsd-prepare.sh
    fi 
}

load_rc_config $name
run_rc_command "$1"
