#! /bin/sh
# Copyright (c) 1996-1999 Florian La Roche

cpifchanged() {
	if test -r $2 ; then
		if cmp -s $1 $2 ; then
			return
		fi
	fi
	cp $1 $2
}
M=/etc/mail

echo "Started the jurix configuration tool."

r=$ROOT

echo "Reading $r/etc/rc.config and updating the system..."

test -f $r/etc/rc.config || {
	echo "No $r/etc/rc.config found." 1>&2
	exit 1
}
. $r/etc/rc.config
if test "$r" != ""; then
	PATH=$PATH:$r/bin:$r/usr/bin:$r/sbin:$r/usr/sbin
	export PATH
fi

if test -n "$SEARCHLIST" -o -n "$NAMESERVER"; then
	(echo "# this file is automatically generated by /sbin/jur-config"
	echo "# please edit /etc/rc.config and run /sbin/jur-config"
	test -n "$SEARCHLIST" && echo "search $SEARCHLIST"
	for i in $NAMESERVER; do
		echo "nameserver $i"
	done ) > $r/etc/resolv.conf
else
	rm -f $r/etc/resolv.conf
fi

(echo "# this file is automatically generated by /sbin/jur-config"
echo "# please edit /etc/rc.config and run /sbin/jur-config"
if test -n "$NAMESERVER"; then
	echo "order hosts,bind"
else
	echo "order hosts"
fi
echo "multi on") > $r/etc/host.conf

test -n "$ORGANIZATION" && echo "$ORGANIZATION" > $r/etc/organization

test -n "$NNTPSERVER" && echo "$NNTPSERVER" > $r/etc/nntpserver

if test -w $r/usr/lib/irc; then
	(for i in $IRCSERVER; do
		echo $i
	done) > $r/usr/lib/irc/ircII.servers
# shorter: echo $IRCSERVER | tr ' ' '\012' > $r/usr/lib/irc/ircII.servers
fi

if test -n "$TIMEZONE"; then
	rm -f $r/etc/localtime
	if test -f $r/usr/lib/zoneinfo/$TIMEZONE ; then
		cpifchanged $r/usr/lib/zoneinfo/$TIMEZONE $r/etc/localtime
	else
		cpifchanged $r/usr/share/zoneinfo/$TIMEZONE $r/etc/localtime
	fi
fi

gen_sendmail_m4(){
	cat <<EOF
include(\`/usr/share/sendmail/m4/cf.m4')
VERSIONID(\`linux setup for jurix')dnl
OSTYPE(\`linux')dnl
define(\`STATUS_FILE', \`/var/log/sendmail.st')dnl
define(\`confDEF_USER_ID', \`daemon:daemon')dnl
define(\`PROCMAIL_MAILER_PATH', \`/usr/bin/procmail')dnl
define(\`confCOPY_ERRORS_TO', \`Postmaster')dnl
define(\`UUCP_MAILER_MAX', \`2000000')dnl
define(\`confQUEUE_LA', \`12')dnl
define(\`confREFUSE_LA', \`18')dnl
define(\`confPRIVACY_FLAGS', \`authwarnings,novrfy,noexpn')dnl
define(\`confUSERDB_SPEC', \`/etc/mail/userdb.db')dnl
define(\`confTRUSTED_USERS', \`mdom wwwrun')dnl
EOF
	if test -n "$SENDMAIL_SMARTHOST" ; then
		echo "define(\`SMART_HOST', \`$SENDMAIL_SMARTHOST')dnl"
	fi
	if test -n "$SENDMAIL_RELAY" ; then
		echo "define(\`LOCAL_RELAY', \`$SENDMAIL_RELAY')dnl"
		echo "define(\`MAIL_HUB', \`$SENDMAIL_RELAY')dnl"
	fi
	echo "FEATURE(\`accept_unqualified_senders')dnl"
	if test "$SENDMAIL_EXPENSIVE" = yes ; then
		echo "define(\`confCON_EXPENSIVE', \`True')dnl"
		echo "define(\`SMTP_MAILER_FLAGS',\`e')dnl"
	fi
	if test "$SENDMAIL_NOCANONIFY" = yes ; then
		echo "FEATURE(\`nocanonify')dnl"
	fi
	if test "$SENDMAIL_ANTI_SPAM" = yes ; then
		echo "FEATURE(\`rbl')dnl"
	else
		echo "FEATURE(\`accept_unresolvable_domains')dnl"
	fi
	if test -n "$FROM_HEADER" ; then
		echo "MASQUERADE_AS(\`$FROM_HEADER')dnl"
	fi
	echo "FEATURE(\`masquerade_envelope')dnl"
	if test -n "$SENDMAIL_GENERICS_DOMAIN" ; then
		echo "GENERICS_DOMAIN(\`$SENDMAIL_GENERICS_DOMAIN')dnl"
	fi

	cat <<EOF
FEATURE(\`always_add_domain')dnl
FEATURE(\`local_procmail')dnl
FEATURE(\`mailertable', \`hash -o /etc/mail/mailertable.db')dnl
FEATURE(\`genericstable', \`hash -o /etc/mail/genericstable.db')dnl
FEATURE(\`virtusertable', \`hash -o /etc/mail/virtusertable.db')dnl
FEATURE(\`access_db', \`hash -o /etc/mail/access.db')dnl
FEATURE(\`blacklist_recipients')dnl
MAILER(\`local')dnl
MAILER(\`procmail')dnl
MAILER(\`smtp')dnl
MAILER(\`uucp')dnl
MAILER(\`bsmtp')dnl
MAILER(\`fido')dnl
EOF
	if test -n "$SENDMAIL_LOCALHOST" ; then
		echo "Cw $SENDMAIL_LOCALHOST"
	fi
}
if test "$SENDMAIL_TYPE" != no; then
	gen_sendmail_m4 | m4 - > $r/etc/sendmail.cf
fi
if test -z "$r" ; then
	if test /etc/aliases -nt /etc/aliases.db ; then
		echo "Rebuilding /etc/aliases.db."
		sendmail -bi
	fi
	if test $M/mailertable -nt $M/mailertable.db ; then
		echo "Rebuilding $M/mailertable.db."
		makemap hash $M/mailertable.db < $M/mailertable
	fi
	if test $M/genericstable -nt $M/genericstable.db ; then
		echo "Rebuilding $M/genericstable.db."
		makemap hash $M/genericstable.db < $M/genericstable
	fi
	if test $M/virtusertable -nt $M/virtusertable.db ; then
		echo "Rebuilding $M/virtusertable.db."
		makemap hash $M/virtusertable.db < $M/virtusertable
	fi
	if test $M/access -nt $M/access.db ; then
		echo "Rebuilding $M/access.db."
		makemap hash $M/access.db < $M/access
	fi
	if test $M/userdb -nt $M/userdb.db ; then
		echo "Rebuilding $M/userdb.db."
		makemap btree $M/userdb.db < $M/userdb
	fi
fi
if test -n "$FROM_HEADER"; then
	echo "$FROM_HEADER" > /etc/inews_mail_gateway
fi
if test -n "$KEYTABLE" -a -d $r/usr/share/kbd/keymaps ; then
	loadkeys $KEYTABLE
	dumpkeys > $r/etc/default.keytab
fi

test -z "$r" -a -x /sbin/ldconfig && /sbin/ldconfig 2>/dev/null

if test -z "$r" -a -n "$LONGCHECK" ; then
	echo "Updating X11 font directories..."
	for i in 100dpi Type1 75dpi cyrillic PEX misc Speedo ; do
		FDIR=/usr/X11R6/lib/X11/fonts/$i
		test -d $FDIR && test ! -f $FDIR/fonts.dir -o $FDIR -nt \
			$FDIR/fonts.dir && \
			/usr/X11R6/bin/mkfontdir $FDIR
	done
	if test -x /usr/bin/perl && test ! -f /usr/info/dir -o \
		/usr/info/dir -ot /usr/info ; then
		echo "Creating /usr/info/dir..."
		/usr/bin/mkinfodir > /usr/info/dir
	fi
	if test -x /usr/bin/texhash ; then
		echo "Creating teTeX database."
		/usr/bin/texhash
	fi
	echo "Creating an index file of all manpages..."
	/usr/bin/mandb -q
fi

echo "Finished."
exit 0
