#!/bin/sh
#
# $Id: create-juniper-conf,v 1.4 2001/04/25 18:38:25 mdb Exp $
#
# install state configuration file
#
# Based on original Pewl script /etc/firstinstall by
# Paul Traina, June 1998
#
# mangled into /bin/sh by Bob Manson, September 2000
#
# Copyright (c) 1998-2001, Juniper Networks, Inc.
# All rights reserved.
#

if [ "$#" != 3 ] ; then
    echo "Usage: $0 input-var-file default-config-file output-file"
fi

myself="$0"
input="$1"
default="$2"
output="$3"

#
# Silently exit if a config file is already there
#
[ -f $output ] && exit 0

#
# Silently exit if the input file is missing
#
[ ! -f $input ] && exit 0

> $output || {
    echo "INSTALL: Unable to write initial config in $output." 1>&2
    echo "INSTALL: Not recording system installation config." 1>&2
    exit 1
}

#
# Read the install state dump, which is a set of /bin/sh var assignments.
#
. $input

#
# Record the hostname, remove the domain name, since it makes the
# system prompts shorter and we're not originating e-mail.
#
if [ "$hostname" != "" ] ; then
    hostname=`echo $hostname | sed 's/[.].*$//;'`

    echo "system { host-name $hostname; }" >> $output
fi

#
# Record nameservice information
#
if [ "$domainname" != "" ] ; then
  echo "system { domain-name $domainname; }" >> $output
fi

if [ "$nameserver" != "" ] ; then
  echo "system { name-server { $nameserver } }" >> $output;
fi

#
# Record debugging options
#
if [ "$debugger_on_break" = "yes" ] ; then
    echo "system { debugger-on-break; }"; >> $output
fi

if [ "$debugger_on_panic" = "yes" ] ; then
    echo "system { debugger-on-panic; }"; >> $output
fi

if [ "${dumpdev:-NO}" != "NO" ] ; then
    echo "system { dump-on-panic; }" >> $output;
    if [ "$dumpdev" != "/dev/wd2s1b" ] ; then
        echo "system { dump-device $dumpdev; }" >> $output
    fi
fi

#
# Set interfaces up, at least for our ethernet and FDDI interfaces.
#
# We could conceivably do ATM and serial ports by looking at the
# ifconfig_xxx_extra paramters, but there's limited win for the
# additional complexity, so screw it.
#
# We deliberately don't do the loopback, it's unnecessary, we're a router,
# not a UNIX box.
#

if [ "$network_interfaces" != "" ] ; then
    for iface in $network_interfaces
    do
	case $iface in
	    fxp*|de*|ep*|fpa*|ed*)
		eval addr=\"\${ifconfig_$iface}\";
		echo "interfaces { $iface { unit 0 { family inet { address $addr; } } } }" >> $output
        esac
    done
fi
echo "interfaces { lo0 { unit 0 { family inet { address 127.0.0.1; } } } }" >> $output

#
# Set up the default router from sysinstall as both an emergency
# default router (backup-router) and add a static route in rpd.
#
# We expect this user to ditch the static route on backbone routers,
# but it's nice when you're first setting things up.
#
if [ "$defaultrouter" != "" ] ; then
    echo "system { backup-router $defaultrouter; }" >> $output;
    echo "routing-options { static { route default { nexthop $defaultrouter; retain; no-readvertise; } } }" >> $output
fi

#
# Crud for internal Juniper configs -- never to appear on customers
# production systems.
#
case "$install_script_config" in
    mfg-*|lab-*)
	echo "system { services { finger; ftp; rsh; rlogin; ssh; telnet; } }" >> $output;
	echo "system { time-zone America/Los_Angeles; }" >> $output;
	;;
esac

#
# if /etc/default.conf exists, append it
#
if [ -f $default ] ; then
    dd if=$default >> $output 2>/dev/null
fi

echo "INSTALL: created initial system configuration database in $output.";

#
# remove the now obsolete input file
#
rm -f $input || { echo "INSTALL: Unable to remove $input" 1>&2; exit 1; }

#
# Since this is a first setup, let's setup /altroot too.
#
#echo "INSTALL: archiving to backup media devices if available.";
#/sbin/snapshot

exit 0;
