#!/bin/sh

LOCALE_DIR="/usr/lib/locale"
SYSADMIN_LOCALE_DIR="/usr/snadm/sysadmin/locale"

LIST="	AR argentina\
	AU australia\
	BE belgium\
	BO bolivia\
	BR brazil\
	CA canada\
	CH switzerland\
	CL chile\
	CO colombia\
	CR costarica\
	DK denmark\
	EC ecuador\
	GT guatemala\
	IE ireland\
	MX mexico\
	NI nicaragua\
	NZ newzealand\
	PA panama\
	PE peru\
	PY paraquay\
	SV elsalvador\
	UK england\
	US us\
	UY uruguay\
	VE venezeula\
	da danish\
	de german\
	en english\
	es spanish\
	fr french\
	it italian\
	nl dutch\
	no norwegian\
	pt portuguese\
	su finnish\
	sv swedish"

#
# Run through the list of abbrieviations & get the full name.  This is done by
# checking every other word in the list for a match.  When a match is found, 
# it shifts one more word to get the translation.
#
getname()
{
	cnt=0
	found=0
	TMPLIST=${LIST}
	TMPWORD='XXX'
	while [ ${TMPWORD}x != x ]
	do
		# XXX Three should be 2. See note in shiftit.
		shiftit 3 $TMPLIST
		
		# If this word matches what we're looking for...
		if [ ${TMPWORD}x = ${1}x ]
		then
			# ...shift over to get the translation
			# XXX Two should be 1. See note in shiftit.
			shiftit 2 $TMPLIST
			NAME=$TMPWORD
			found=1
			break
		fi
	done
	
	# If we didn't find anything, set NAME to null
	if [ ${found} = 0 ]
	then
		NAME=""
	fi
}

#
# Since shift only works on positional parameters, this func shifts things and
# stores the remainder in TMPWORD & TMPLIST.  TMPWORD is the first word in the
# new list while TMPLIST is the new list.
# Takes the number of words to shift & the list of words to shift.
#
shiftit()
{
	# Add 1 to the number to shift so we step over the number arg.
#	shift_cnt=`expr $1 + 1`
# XXX Expr turns out to be too slow, so just force calling func to add one to
# XXX what they should be passing.
#
	shift_cnt=$1
	shift $shift_cnt
	TMPWORD=$1
	TMPLIST=$*
}

#
# Takes 3 args: the lang abbreviation to translate, the country abbreviation
# & the codeset.
#
translate()
{
	# If there isn't at least one arg, return immediately
	if [ $# -lt 1 ]
	then
		return 1
	fi

	getname $1
	lang=${NAME}

	# If there is a second arg, translate it, otherwise set it to null
	if [ ${2}"x" != "x" ]
	then
		getname $2
		if [ ${NAME}"x" != "x" ]
		then
			country="_${NAME}"
		else
			country=""
		fi
	else
		country=""
	fi

	# If there is a third arg, copy it, otherwise set it to null
	if [ ${3}"x" != "x" ]
	then
		codeset=".`echo $3 | sed "s/-1//"`"
	else
		codeset=""
	fi

	# If lang is null (wasn't found) and the country is _ (wasn't found)
	# return 1
	if [ ${lang}x = "x" - a ${country}x = "_x" ]
	then
		return 1
	else
		return 0
	fi
}

#
# Parses the passed filename into language,locale & codeset
#
parsefile ()
{
	FILENAME=$1
	# This sed script first checks for a file of format fr_FR.8859.
	# If it's found, the t on the next line says to break out of the script.
	# If it's not found, check for fr_FR then fr.8859, & finally just fr.
	# The following table describes the output
	# FILENAME	LANG	COUNTRY	CODESET
	# fr		fr	(nil)	(nil)
	# fr_CA		fr	CA	(nil)
	# fr.8859	fr	(nil)	8859
	# fr_CA.8859	fr	CA	8859
	eval `echo $FILENAME | \
	sed -e "s/\(.*\)_\(.*\)\.\(.*\)/LANGUAGE=\1 LOCALE=\2 CODESET=\3/
	t
	s/\(.*\)_\(.*\)/LANGUAGE=\1 LOCALE=\2 CODESET=/
	t
	s/\(.*\)\.\(.*\)/LANGUAGE=\1 LOCALE= CODESET=\2/
	t
	s/\(.*\)/LANGUAGE=\1 LOCALE= CODESET=/"`
}

#
# Takes a filename, breaks it up, then translates it & creates a symlink.
#
dofile()
{
	parsefile $1

	translate $LANGUAGE $LOCALE $CODESET
	if [ $? = 0 ]
	then
		# If there was a codeset in the filename, create that link
		if [ ${codeset}"x" != "x" ]
		then
			createlink $1
		else
		# Otherwise, create links for 8859 codeset
			codeset=".8859"
			createlink $1
		fi
	fi
}

createlink()
{
	# If the link exists, remove it & re-link it to
	# ensure it's correct 
	if [ -h ${lang}${country}${codeset} ]
	then
		rm ${lang}${country}${codeset}
	fi

	# If the file doesn't exist, link it.  This check is to avoid
	# overwriting a file that the user has placed there.
	if [ ! -r ${lang}${country}${codeset} ]
	then
		ln -s ${1} ${lang}${country}${codeset}
	fi
}

#
# Takes a list of files & passes them one at a time to the lower functions.
#
dofiles()
{
	# Run through the list of files & create the links as necessary
	for file in $*
	do
		dofile $file
	done
}


#
# Goes to the passed locale directory & passes the list of files on
#
dolocale()
{
	if [ -d $1 ]
	then
		cd $1	# Go to desired directory & get a list of files there
		dofiles `\ls`
	fi
}

#
# main
#

# Remember the directory we started in so we can return there
CURR_DIR=`pwd`

# If no list of files was passed, do the files in the standard locations
if [ $# = 0 ]
then
	dolocale ${LOCALE_DIR}
	# XXX GAG!  Force a link here for english_us.ascii since we normally
	# only create .8859 links.
	lang=english
	country=_us
	codeset=.ascii
	createlink en_US

	dolocale ${SYSADMIN_LOCALE_DIR}
	# XXX GAG!  Force a link here for english_us.ascii since we normally
	# only create .8859 links.
	lang=english
	country=_us
	codeset=.ascii
	createlink en_US
else
	# A list of files was passed, so just do those
	dofiles $*
fi

# Go back whence we came
cd ${CURR_DIR}
