#!/bin/bash
### BEGIN INIT INFO
# Provides:          ofr
# Required-Start:    $syslog $time $local_fs
# Required-Stop:     $syslog $time $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: Vyatta Router
# Description:       Debian init script for the Vyatta Router
### END INIT INFO
# **** License ****
# Version: VPL 1.0
#
# The contents of this file are subject to the Vyatta Public License
# Version 1.0 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.vyatta.com/vpl
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
#
# Author:	Tom Grennan <tgrennan@vyatta.com>
# **** End License ****

. /lib/lsb/init-functions

: ${vyatta_env:=/etc/default/vyatta}
source $vyatta_env

declare progname=${0##*/}
declare action=$1; shift

declare -x BOOTFILE=$vyatta_sysconfdir/config/config.boot

declare -a subinit
declare -a all_subinits=(
    rl-system
    firewall )

if [ $# -gt 0 ] ; then
    for s in $@ ; do
	[ -x ${vyatta_sbindir}/${s}.init ] && subinit[${#subinit}]=$s
    done
else
    for s in ${all_subinits[@]} ; do
	[ -x ${vyatta_sbindir}/${s}.init ] && subinit[${#subinit}]=$s
    done
    if [ -x ${vyatta_sbindir}/rtrmgr.init ] ; then
	subinit+=( rtrmgr )
	GROUP=xorp
    else
	GROUP=vyattacfg
    fi
fi


have_rl_system () {
    test -x $vyatta_sbindir/rl-system.init
}

# if necessary, provide initial config
init_bootfile () {
    if [ ! -f $BOOTFILE ] ; then
	if [ -f $vyatta_sysconfdir/config.boot.default ]; then
	  cp $vyatta_sysconfdir/config.boot.default $BOOTFILE
        else
          $vyatta_sbindir/vyatta_current_conf_ver.pl > $BOOTFILE
        fi
    fi
    chgrp ${GROUP} $BOOTFILE 
    chmod 660 $BOOTFILE
    ## remove the unnecessary and potentially conflicting
    ## config-directory statement (i.e. /mnt/floppy vs. /media/floppy
    sed -i '/^rtrmgr {$/,/^}$/d' $BOOTFILE
}

# if necessary, migrate initial config
migrate_bootfile ()
{
    if [ -x $vyatta_sbindir/vyatta_config_migrate.pl ]; then
        log_progress_msg migrate
        $vyatta_sbindir/vyatta_config_migrate.pl $BOOTFILE
    fi
}

# load the initial config
load_bootfile ()
{
  if [ -x $vyatta_sbindir/vyatta-config-loader.pl ]; then
    log_progress_msg configure
    sg ${GROUP} -c "$vyatta_sbindir/vyatta-config-loader.pl $BOOTFILE"
  fi
}

start ()
{
    log_action_begin_msg "Mounting Vyatta Config"
    mount -o nosuid,nodev,mode=775 -t tmpfs none ${vyatta_configdir} && \
	chgrp ${GROUP} ${vyatta_configdir}
    log_action_end_msg $?
    init_bootfile
    log_daemon_msg "Starting Vyatta Router"
    migrate_bootfile
    for s in ${subinit[@]} ; do
	log_progress_msg $s
	${vyatta_sbindir}/${s}.init start || (log_end_msg $? && return)
    done
    load_bootfile
    log_end_msg $?
}

stop()
{
    local -i status=0
    log_daemon_msg "Stopping Vyatta Router"
    for ((i=${#sub_inits[@]} - 1; i >= 0; i--)) ; do
	s=${subinit[$i]}
	log_progress_msg $s
	${vyatta_sbindir}/${s}.init stop
	let status\|=$?
    done
    log_end_msg $status
    log_action_begin_msg "Un-mounting Vyatta Config"
    umount ${vyatta_configdir}
    log_action_end_msg $?
}

case "$action" in
    start) start ;;
    stop)  stop ;;
    restart|force-reload) stop && start ;;
    *)	log_failure_msg "usage: $progname [ start|stop|restart ] [ subinit ... ]" ;
	false ;;
esac

exit $?

# Local Variables:
# mode: shell-script
# sh-indentation: 4
# End: