#!/bin/sed -f
# Erik E. Fair <fair@ucbarpa.berkeley.edu>, March 12, 1987
#
#	Lucky me, 4 BSD allows me to directly exec shell scripts...
#
#	This sed script is used by recnews to canonicalize internet
#	addresses for local address conventions (that is, the local
#	sendmail config knows about certain unofficial domains like
#	BITNET, and will do the right thing with an address of the form
#	user@host.BITNET, so I'm free to throw away the gateway in
#	user%host.bitnet@gateway and use user@host.BITNET). All this in
#	the name of gatewaying stuff to USENET...
#
#	NOTA BENE: since none of the sendmail configuration files that
#	I have seen or used will deliver a local (as in from this host)
#	address fully qualified (that is, with at sign and official
#	hostname), I had to encode the official hostname of the system
#	that this script runs on into the script (sed has no really
#	flexible way of fetching a hostname externally...). There is a
#	big, loud comment in caps where the name is; be sure to change
#	it for local configuration. If any sed wizards come up with a
#	better way to do this, I'm listening...
#
#	With apologies to upas and Dave Presotto
#	(cf. Portland USENIX Proceedings)

# perhaps just a user name all by itself?
/^[^!%@.]*$/{
#
# NOTE: FULLY QUALIFIED HOSTNAME MUST APPEAR IN THE REGULAR EXPRESSION BELOW
#	ON THE RIGHT HAND SIDE OF THE AT SIGN.
#
	s/$/@ucbvax.berkeley.edu/
	b end
}

#
#	Make the domain name upper case (and easy to match)
#
s/[Rr][Ee][Ll][Aa][Yy]\.[Cc][Ss]\.[Nn][Ee][Tt]/RELAY.CS.NET/g
s/\.[Mm][Aa][Ii][Ll][Nn][Ee][Tt]/.MAILNET/g
s/\.[Bb][Ii][Tt][Nn][Ee][Tt]/.BITNET/g
s/\.[Cc][Ss][Nn][Ee][Tt]/.CSNET/g
s/\.[Uu][Uu][Cc][Pp]/.UUCP/g
s/\.[Dd][Ee][Cc]/.DEC/g
s/\.[Cc][Dd][Nn]/.CDN/g
s/\.[Oo][Zz]/.OZ/g
#
#	Official domains hiding behind registered hosts (we can MX)
#
s/\.[Ee][Dd][Uu]/.EDU/g
s/\.[Cc][Oo][Mm]/.COM/g
s/\.[Gg][Oo][Vv]/.GOV/g
s/\.[Mm][Ii][Ll]/.MIL/g
s/\.[Oo][Rr][Gg]/.ORG/g
s/\.[Nn][Ee][Tt]/.NET/g
s/\.[Uu][Kk]/.UK/g
s/\.[Aa][Uu]/.AU/g

:top
# is it the percent syntax?
/.*%.*\.MAILNET@.*$/b pct
/.*%.*\.BITNET@.*$/b pct
/.*%.*\.CSNET@.*$/b pct
/.*%.*\.UUCP@.*$/b pct
/.*%.*\.DEC@.*$/b pct
/.*%.*\.CDN@.*$/b pct
/.*%.*\.OZ@.*$/b pct

/.*%.*\.EDU@.*$/b pct
/.*%.*\.COM@.*$/b pct
/.*%.*\.GOV@.*$/b pct
/.*%.*\.MIL@.*$/b pct
/.*%.*\.ORG@.*$/b pct
/.*%.*\.NET@.*$/b pct
/.*%.*\.UK@.*$/b pct
/.*%.*\.AU@.*$/b pct

# strip route off of route-addrs
/^@.*@/s/^@[^:]*://

# special for CSNET, since they're slowly going domainist
# remove gateway, change percent to at sign, add .CSNET if there's no dot
/@RELAY.CS.NET$/ {
	s/@[^@]*$//
	s/%\([^%]*\)$/@\1/
	s/@[^.]*$/&.CSNET/
}

# mung hybrid addresses
#	a!b!c.d!e@z	-> e@c.d
#	a!b!c@z		-> c@b.UUCP
#
# Thanks to Wendell Baker <wbaker@shadow.berkeley.edu> the sed wizard
/!.*@/s/@.*$//
/!/{
	s/.*!\([^!]*\)!\([^!]*\)$/\2@\1/
	s/\([^!]*\)!\([^!]*\)$/\2@\1/
	s/@[^.]*$/&.UUCP/
}

b end

# mung user%host.unoffdomain@gatewayhost.arpa -> user@host.unoffdomain
:pct
s/@[^@]*$//
s/%\([^%]*\)$/@\1/
/%.*@/b top

# special for DEC E-net
:end
s/@dec-\([^.]*\).UUCP$/@\1.DEC/
s/\.DEC$/&.COM/
s/\.OZ$/&.AU/
