#!/bin/bash
## BEGIN INIT INFO
# Provides: oraclevm-template
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Required-Start:
# Should-Start:
# Short-Description: Starts the Oracle VM Template configuration
# Description: Template config runs the first time an virtual machine
#              template is booted. It checks for the existance of an
#              /etc/sysconfig/oraclevm-template file.  If the file exists and
#              contains RUN_TEMPLATE_CONF=NO, oraclevm-template will not run.
#              If "template-reconfig" is provided in the kernel boot arguments
#              oraclevm-template will run even if it is turned off in the
#              /etc/sysconfig/oraclevm-template file.
## END INIT INFO

#
# oraclevm-template: Starts the OracleVM Template appliance configuration
#
# chkconfig: 345 99 01
#
# description: OracleVM Template is the virtual machine configuration \
#              script that runs the first time the Oracle VM Template is \
#              booted. It checks for the existence of an \
#              /etc/sysconfig/oraclevm-template file. If the file exists and \
#              contains RUN_TEMPLATE_CONF=NO, oraclevm-template will not run. \
#              If "template-reconfig" is provided in the kernel boot \
#              arguments, oraclevm-template will run even if it is turned \
#              off in the /etc/sysconfig/oraclevm-template file.
#              If the paramter TEMPLATE_CONFIG_SCRIPT is set in the
#              /etc/sysconfig/oraclevm-template file, the script will try to
#              execute that instead of the default oraclevm-template.sh
#

#
#  Copyright (c) 2008, Oracle. All rights reserved
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, version 2.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# Source function library.
. /etc/init.d/functions

CONFFILE=/etc/sysconfig/oraclevm-template

case "$1" in
    start)
        echo "Starting oraclevm-template..."
        doconfig=0
        forceconfig=0
        ret=0
        if grep -i -q "template-reconfig" /proc/cmdline; then
            forceconfig=1
        else
            if [ ! -e "$CONFFILE" ]; then
                action $"$CONFFILE does not exist." /bin/false
                ret=2
            elif ! grep -q "RUN_TEMPLATE_CONF=" $CONFFILE; then 
                action $"$CONFFILE is not correctly configured." /bin/false
                ret=3
            else 
                . $CONFFILE
                case "$RUN_TEMPLATE_CONF" in
                    YES|yes|Yes|1)
                        doconfig=1
                        ;;
                    NO|no|No|0)
                        action "" /bin/true 
                        ret=0
                        ;;
                    *)
                        action "Unknown RUN_TEMPLATE_CONF value." /bin/false
                        ret=4
                        ;;
                esac
            fi
        fi
	[ -x /usr/bin/plymouth ] && /usr/bin/plymouth --hide-splash
        if [ $forceconfig -eq 1 ] || [ $doconfig -eq 1 ]; then
            # foce to run reconfig as trigged by kernel cmd-line
            if [ $forceconfig -eq 1 ]; then
                /usr/sbin/oraclevm-template --config --force
                ret=$?
            else
                /usr/sbin/oraclevm-template --config
                ret=$?
            fi
            # Just disable no matter firstboot reconfig succeeds or not.
            /usr/sbin/oraclevm-template --disable
            if [ $ret -eq 0 ]; then
               action "" /bin/true 
            else
               action "" /bin/false
            fi
        fi
	[ -x /usr/bin/plymouth ] && /usr/bin/plymouth --show-splash
        exit $ret
        ;;
    stop)
        exit 0
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 3
        ;;
esac
