#!/bin/sh

PROG="${0##*/}" #program name

print_version()
{
	cat <<EOF
$PROG version @VERSION@

Written by Stanislav Ievlev

Copyright (C) 2003-2004 ALT Linux Team
EOF
	exit
}


print_usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
Usage: $PROG [options] 

alterator backend for simple i18n manipulations

Valid options are:
  -h, --help	display help screen
  -v, --version	display version information
  -l, --list	get list of avaliable locales
  -t, --type	check for existance
  -d, --delete	(empty action, required for backend)
  -r, --read	read locale information
  -w, --write	change locale information

Report bugs to <inger@altlinux.org>
EOF
	[ -n "$1" ] && exit "$1" || exit
}

GLOBAL_CONFIG=/etc/sysconfig/i18n
LOCAL_CONFIG=~/.i18n


add_or_subst()
{
	local name=$1
	local value=$2
	local file=$3

	[ -f "$file" ] || touch "$file"
	if grep -qs "^$name" "$file";then
		/bin/sed -r -i "s,^$name.*,$name$value," $file
	else
		echo "$name$value">>$file
	fi
}


update_lang()
{
	#language now autogenerated by system
	[ -f $LOCAL_CONFIG ] && /bin/sed -r -i "/^LANGUAGE=/d" "$LOCAL_CONFIG" 

	if [  -w "$GLOBAL_CONFIG" ] ;then
		CONFIGNAME="$GLOBAL_CONFIG"
	else
		CONFIGNAME="$LOCAL_CONFIG"
	fi

	add_or_subst "LANG=" "$1" "$CONFIGNAME"
}

TEMP=`getopt -n $PROG -o h,v,l:,d:,r:,w:,t: -l help,version,list:,delete:,read:,write:,type: -- "$@"` || print_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		-h|--help) print_usage 0
			;;
		-v|--version) print_version
			;;
		-l|--list)
			shift;dir="$1";
			if [ "$dir" = "/" ] ;then
				echo "available d"
				echo "current r"
			elif [ "$dir" = "available" ] ;then
				#get list of available locales
				for locale_name in $(locale -a|egrep -v 'C|POSIX')
				do
					LC_ALL=$locale_name locale title
				done | sort -u | sed 's,.*,& r,'
			fi
			;;
		-d|--delete)
			#ignore
			shift;dir="$1";
			;;
		-t|--type)
			#ignore
			shift;dir="$1";
			;;
		-r|--read)
			shift;dir="$1";
			if [ "$dir" = "current" ]; then
				#get current locale setup
				unset LANG
				unset LANGUAGE
				[ -f "$GLOBAL_CONFIG" ] && . "$GLOBAL_CONFIG"
				
				if ! [ -w "$GLOBAL_CONFIG" ] ;then
					[ -f "$LOCAL_CONFIG" ] && . "$LOCAL_CONFIG"
				fi
				
				echo "stdname:${LANG%.*}"
				echo -n "title:"
				LC_ALL=$LANG locale title
				echo -n "charmap:"
				LC_ALL=$LANG locale charmap
			elif echo "$dir"|/bin/grep -qs "^available" ; then
				#get information about some avaliable locale
				wanted_name=${dir#*/}
				wanted_locales=$(for locale_name in $(locale -a|egrep -v 'C|POSIX')
						do
							name=$(LC_ALL=$locale_name locale title)
							stdname=${locale_name%%.*}
							charmap=$(LC_ALL=$locale_name locale charmap)
							echo "$name	$locale_name"
							done|grep "^$wanted_name	"|cut -f2)
				echo -n "stdname:"
				echo "$wanted_locales"|head -n1|sed -r 's,\..*,,'
				echo -n "charmaps:"
				for i in $wanted_locales;do
					LC_ALL=$i locale charmap
				done|sort -u|tr '\n' ','|sed s/,$//
				echo ""
			fi
			;;
		-w|--write)
			#ignore
			shift;dir="$1";
			if [ "$dir" = "current" ] ;then
				while read l; do
					option=${l%%:*}
					value=${l#*:}
					[ "$option" = "lang" ] && update_lang "$value"
				done
			fi
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
done
