#!/bin/bash
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
# 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>
# Description:	Vyatta Router system setup
#		this is an indirect init sub-script executed by ofr.init
#
# **** End License ****

progname=${0##*/}
ACTION=$1

source /etc/default/vyatta

: ${vyatta_prefix:=/opt/vyatta}
: ${vyatta_bindir:=${vyatta_prefix}/bin}
: ${vyatta_sbindir:=${vyatta_prefix}/sbin}
: ${vyatta_sysconfdir:=${vyatta_prefix}/etc}
: ${hostname:=`hostname -s`}

[[ $PATH == *${vyatta_bindir}* ]] || PATH+=:${vyatta_bindir}
[[ $PATH == *${vyatta_sbindir}* ]] || PATH+=:${vyatta_sbindir}

export PATH

. /lib/lsb/init-functions

## BOOTFILE is provided by ofr.init
: ${BOOTFILE:=$prefix/etc/config/config.boot}

if [ -z "$LOGFILE" ] ; then
    if touch /var/log/messages &> /dev/null ; then
	LOGFILE=/var/log/messages
    else
	LOGFILE=/dev/null
    fi
fi

syslog ()
{
    logger -p user.warning -t "$progname" "$*"
}

# easy way to make empty file without any command
empty()
{
    >$1
}

shopt -s extglob nullglob

search_config_if_wan () {
    grep -q "\<serial\>.*\<$1\>" $BOOTFILE
}

add_new_serial_if () {
    __config_additions=/tmp/__config_additions
    rm -f $__config_additions
    ip link show |
    sed -n '/^[0-9]*: wan[0-9]*:/ s/.* \([^:]*\):.*$/\1/p' |
    while read wan ; do
	if ! search_config_if_wan $wan ; then
	    echo "    serial $wan" >> $__config_additions
	fi
    done
    if [ -e $__config_additions ]; then
	rm -f /tmp/__bootfile
	sed '/^interfaces {$/ r '$__config_additions \
	    $BOOTFILE > /tmp/__bootfile
	mv /tmp/__bootfile $BOOTFILE
	rm -f $__config_additions
    fi
}

## Update the version information
update_version_info () {
    if [ -f ${vyatta_sysconfdir}/version.master ]; then
	cp ${vyatta_sysconfdir}/version.master ${vyatta_sysconfdir}/version
    fi
}

clear_or_override_config_files ()
{
    for conf in ntp.conf \
        snmp/snmpd.conf snmp/snmptrapd.conf keepalived/keepalived.conf \
        ipvsadm.rules default/ipvsadm resolv.conf
    do
	if [ -f /etc/$conf ] ; then
	    empty /etc/$conf
	fi
    done
}

udev_rescan ()
{
    rm -f /tmp/vyatta_net_name_*
    udevadm trigger --subsystem-match=net && udevsettle
    for ff in /tmp/vyatta_net_name_* ; do
	f=${ff##*/}
	cmd_name_hwid=${f/vyatta_net_name_/}
	cmd=${cmd_name_hwid%%_*}
	name_hwid=${cmd_name_hwid#*_}
	name=${name_hwid%_*}
	hwid=${name_hwid#*_}
	syslog $cmd $name $hwid
	$vyatta_sbindir/${cmd}_bootfile_eth_hwid $BOOTFILE $name $hwid
    done
}

create_ssh_host_keys () {
    if [ ! -f "/etc/ssh/ssh_host_rsa_key" ]; then
        syslog "Creating ssh v2 rsa host key."
        ssh-keygen -q -N '' -t rsa -f /etc/ssh/ssh_host_rsa_key
    fi;
    if [ ! -f "/etc/ssh/ssh_host_dsa_key" ]; then
	syslog "Creating ssh v2 dsa host key."
        ssh-keygen -q -N '' -t dsa -f /etc/ssh/ssh_host_dsa_key
    fi;
    if [ ! -f "/etc/ssh/ssh_host_key" ]; then
	syslog "Creating ssh v1 host key."
        ssh-keygen -q -N '' -t rsa1 -f /etc/ssh/ssh_host_key
    fi;
}

set_ipv6_params ()
{
 # default values for ipv6 parameters do not take effect for interfaces at boot
 # time, so copy over default values to their interface specific parameter
 ipv6_params=(accept_source_route accept_redirects)
 num_ipv6_params=${#ipv6_params[*]}
 i=0
 while [ $i -lt $num_ipv6_params ]; do
   default_val=`cat /proc/sys/net/ipv6/conf/default/${ipv6_params[$i]}`
   array=(`ls /proc/sys/net/ipv6/conf/`)
   array_len=${#array[*]}
   j=0
   while [ $j -lt $array_len ]; do
     sudo sh -c "echo $default_val > \
       /proc/sys/net/ipv6/conf/${array[$j]}/${ipv6_params[$i]}"
     let j++
   done
   let i++
 done
}

start () {
    udev_rescan
    create_ssh_host_keys || \
	log_failure_msg "can't initialize ssh host keys"
    clear_or_override_config_files || \
	log_failure_msg "can\'t reset config files"
    add_new_serial_if || \
	log_failure_msg "can\'t add serial interfaces"
    sysctl -q -p /opt/vyatta/etc/vyatta-sysctl.conf ||
        log_failure_msg "can\'t configure kernel settings"
    set_ipv6_params
    update_version_info

    ## Clear out apt config file--it will be filled in by rtrmgr
    empty /etc/apt/sources.list
}

case "$ACTION" in
    start) start ;;
    stop|restart|force-reload) true ;; # nothing to stop/restart
    *)	log_failure_msg "action unknown: $ACTION" ;
	false ;;
esac

exit $?

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