#!/bin/sh
# $Id: syncconf.sh,v 1.1 1998/12/30 19:53:17 danw Exp $

# syncconf - Synchronize system configuration files with data in
# /etc/athena/rc.conf.

# Set up variables.  We would like to put rcsync and rcedit in
# /var/athena, but we generally run before /var is mounted.
rcconf=/etc/athena/rc.conf
rcsync=/etc/athena/rc.conf.sync
rcedit=/etc/athena/rc.conf.edit
rcsyncout=$rcsync
all=false
debug=false
echo=echo
vars=
netif=
gateway=

# Set gateway and netif to appropriate values for $ADDR.
netparams()
{
	if [ -z "$gateway" ]; then
		np=`/etc/athena/netparams "$ADDR"`
		if [ $? -ne 0 ]; then
			return 1
		fi
		set -- $np
		gateway=$4
		netif="inet $ADDR netmask $1 broadcast $3"
		if [ -n "$NETMEDIA" ]; then
			netif="$netif media $NETMEDIA"
		fi
	fi
	return 0
}

# Translates true/false to YES/NO.
yesno()
{
	if [ "$1" = true ]; then
		echo YES
	else
		echo NO
	fi
}

handle()
{
	case "$1" in
	HOST)
		vars="$vars hostname $HOST"
		;;
	ADDR)
		netparams
		if [ $? -eq 0 ]; then
			vars="$vars defaultroute $gateway"
			hostsfile=true
		fi
		;;
	NETDEV)
		netparams
		;;
	NETMEDIA)
		netparams
		;;
	NFSSRV)
		vars="$vars nfs_server `yesno $NFSSRV`"
		;;
	NFSCLIENT)
		vars="$vars nfs_client `yesno $NFSCLIENT`"
		;;
	SENDMAIL)
		vars="$vars sendmail `yesno $SENDMAIL`"
		;;
	TIMECLIENT)
		vars="$vars xntpd `yesno $TIMECLIENT`"
		;;
	*)
		echo "syncconf: unknown variable $1" 1>&2
		exit 1
		;;
	esac
}

while getopts anq opt; do
	case "$opt" in
	a)
		all=true
		;;
	n)
		debug=true
		rcsyncout=/tmp/rc.conf.sync
		;;
	q)
		echo=:
		;;
	\?)
		echo "Usage: syncconf [-anq]"
		exit 1
		;;
	esac
done
shift `expr $OPTIND - 1`
if [ "$#" -ne 0 ]; then
	echo "Usage: syncconf [-anq]" 1>&2
	exit 1
fi

$echo -n "Synchronizing configuration... "

. "$rcconf"

if [ "$all" = false -a -f "$rcsync" ]; then
	. "$rcsync"
else
	changes="HOST ADDR NETDEV NETMEDIA NFSSRV NFSCLIENT SENDMAIL"
	changes="$changes TIMECLIENT"
fi

if [ -z "$changes" ]; then
	$echo "No changes to synchronize."
	exit
fi

for i in $changes; do
	$echo -n "$i "
	handle "$i"
done

$echo ""

if [ "$debug" = true ]; then
	echo "vars: $vars"
	echo "netif: $netif"
	exit
fi

if [ -n "$vars" ]; then
	rm -f "$rcedit"
	set -- $vars
	while [ $# != 0 ]; do
		echo "/^$1=/s/=[^ 	]*/=$2/" >> $rcedit
		shift 2
	done
	echo "w" >> $rcedit
	echo "q" >> $rcedit
	ed -s /etc/rc.conf < $rcedit
	rm -f "$rcedit"
fi

if [ -n "$netif" ]; then
	rm -f "/etc/ifconfig.$NETDEV"
	echo "$netif" > /etc/ifconfig.$NETDEV
fi

cat > $rcsyncout << EOF
# This file was generated by /etc/athena/syncconf; do not edit.
if [ "\$HOST" != "$HOST" ]; then changes="\$changes HOST"; fi
if [ "\$ADDR" != "$ADDR" ]; then changes="\$changes ADDR"; fi
if [ "\$NETDEV" != "$NETDEV" ]; then changes="\$changes NETDEV"; fi
if [ "\$NETMEDIA" != "$NETMEDIA" ]; then changes="\$changes NETMEDIA"; fi
if [ "\$NFSSRV" != "$NFSSRV" ]; then changes="\$changes NFSSRV"; fi
if [ "\$NFSCLIENT" != "$NFSCLIENT" ]; then changes="\$changes NFSCLIENT"; fi
if [ "\$SENDMAIL" != "$SENDMAIL" ]; then changes="\$changes SENDMAIL"; fi
if [ "\$TIMECLIENT" != "$TIMECLIENT" ]; then changes="\$changes TIMECLIENT"; fi
EOF
