# $Header: /data/cvsroot/mail/elmme+/ConfTool/GenCharmap,v 1.8 2000/11/04 09:12:26 hurtta Exp $

	if $test $new = y ; then
		cat >bin/elm.mimecharsets <<'EOM'
# Mapping from LC_CTYPE locale to MIME's charset values
# Format is:
#	LC_CTYPE	charset
# LC_CTYPE is on form
#	LL_cc.encoding
# then also following formats are available:
#	LL_cc		charset
#	LL		charset
#	*.encoding	charset
#
#       -               charset;type=ascii-set;MIME-subset=US-ASCII
#       -               charset;type=xxx;MIME-subset=some-subset-name
#       -               charset;alias=alias-name
EOM
	else
	    printit='$echo "* Removing obsolete names from bin/elm.mimecharsets"; printit='
	    while read killset ; do

		if $egrep "^[^;]+$killset(;|\$)" bin/elm.mimecharsets > /dev/null; 
		then
		    eval "$printit"
		    $echo "  Removing $killset"
		    $egrep -v "^[^;]+$killset(;|\$)" bin/elm.mimecharsets > tmp-$$
		    mv -f tmp-$$ bin/elm.mimecharsets
		fi
	    done < ConfTool/killsets
	fi

	printit='$echo "* Adding map definations from ConfTool/mapfiles"; printit='
	while read file set rest ; do
	    if $test -f $lib/elm.map.txt/$file ; then
		eval "$printit"
		if $egrep "^-.*$set;" bin/elm.mimecharsets > /dev/null; then
		    $echo "  Charset $set already set, will not add map=$file"
		else
		    echo "-	$set$rest;map=$file" >> bin/elm.mimecharsets
		    echo "  Adding map=$file for $set"
		fi
	    fi
	done < ConfTool/mapfiles

	printit='$echo "* Adding iso646 definations from ConfTool/iso646maps"; printit='
	while read set map ; do
	    eval "$printit"
	    if $egrep "^-.*$set;" bin/elm.mimecharsets > /dev/null; then
		$echo "  Charset $set already set, will not add \"$map\""
	    else
		echo "-	$set;MIME-subset=INVARIANT;type=iso646-set;map=\"$map\"" >> bin/elm.mimecharsets
		echo "  Adding \"$map\" for $set"
	    fi
	done < ConfTool/iso646maps

	ifs="$IFS"	
	IFS="$IFS;"

	printit='$echo "* Adding charset definations from doc/mime.charsets"; printit='
	while read loc set rest ; do	    
	    case "$loc" in
		"-")
		eval "$printit"
		if $egrep "^-.*$set;" bin/elm.mimecharsets > /dev/null; then
		    $echo "  Charset $set already set, will not add $rest"
		else
		    echo "-	$set;$rest" >> bin/elm.mimecharsets
		    echo "  Adding $rest for $set"
		fi
		;;
	    esac
	done < doc/mime.charsets

	printit='$echo "* Adding alias definations from ConfTool/charaliases.map"; printit='
	while read loc set rest ; do	    
	    case "$loc" in
		"-")
		eval "$printit"
		if $egrep "^-.*$set;.*alias=" bin/elm.mimecharsets > /dev/null; then
		    $echo "  Charset $set already set, will not add $rest"
		else
		    echo "-	$set;$rest" >> bin/elm.mimecharsets
		    echo "  Adding $rest for $set"
		fi
		;;
	    esac
	done < ConfTool/charaliases.map

	IFS="$ifs"


	if [ "$d_locale" = "$define" ] ; then
	    if $test $locale != locale ; then
		values="`$locale -a`"
	    elif $test "$locale_dir" != none ; then
		values="`ls $locale_dir`"
	    else 
		$echo "Can't generate locale values for elm.mimecharsets"
	    fi
	else
	    values=NONE
        fi

	printit='$echo "* Adding map definations for locales"; printit='
	bad=''
	for lc_ctype in $values ; do
		eval "$printit"
		first="$lc_ctype"
		file=bin/elm.mimecharsets
		. ConfTool/GrepValue
		if $test "$result" = "(unsupported)" ;  then		
			echo "  $lc_ctype (unsupported)"
		elif $test "$result" != "" ;  then			
			echo "  $lc_ctype ok (as $result)"
		else
		     $echo $n "  Adding $lc_ctype $c"
		     readed_charset=
		     if $test "$locale_dir" != none -a -f "$locale_dir/$lc_ctype/charset" ; then
			readed_charset="`$cat $locale_dir/$lc_ctype/charset`"
			case "$readed_charset" in
				iso-*)
					readed_charset="`echo \"$readed_charset\"|$sed 's/^iso-/ISO-/'`" 
				;;
			esac
		     fi
                     if $test "$readed_charset" != "" ; then
			echo "$lc_ctype	$readed_charset" >> bin/elm.mimecharsets
			echo "(as $readed_charset from $locale_dir/$lc_ctype/charset)"
                     else                  
			case "$lc_ctype" in
				NONE)
				    echo "$lc_ctype	$defsystemcharset" >> bin/elm.mimecharsets
				    echo "(default as $defsystemcharset)"
				;;
				C|*.ASCII|ascii|us-ascii|ASCII|US-ASCII|*.US-ASCII)
					echo "$lc_ctype US-ASCII" >> bin/elm.mimecharsets
					echo "(as US-ASCII)"
				;;
				iso88591|*.ISO_8859-1|*.ISO-8859-1|*.ISO8859-1|*.iso88591)
					echo "$lc_ctype ISO-8859-1" >> bin/elm.mimecharsets
					echo "(as ISO-8859-1)"
				;;
				*.ISO_8859-2|*.ISO-8859-2|*.ISO8859-2|*.iso88592)
					echo "$lc_ctype ISO-8859-2" >> bin/elm.mimecharsets
					echo "(as ISO-8859-2)"
				;;
				*.ISO_8859-4|*.ISO-8859-4|*.ISO8859-4|*.iso88594)
					echo "$lc_ctype ISO-8859-4" >> bin/elm.mimecharsets
					echo "(as ISO-8859-4)"
				;;
				*.ISO_8859-5|*.ISO-8859-5|*.ISO8859-5|*.iso88595)
					echo "$lc_ctype ISO-8859-5" >> bin/elm.mimecharsets
					echo "(as ISO-8859-5)"
				;;
				*.ISO_8859-6|*.ISO-8859-6|*.ISO8859-6|*.iso88596)
					echo "$lc_ctype ISO-8859-6" >> bin/elm.mimecharsets
					echo "(as ISO-8859-6)"
				;;
				*.ISO_8859-7|*.ISO-8859-7|*.ISO8859-7|*.iso88597)
					echo "$lc_ctype ISO-8859-7" >> bin/elm.mimecharsets
					echo "(as ISO-8859-7)"
				;;
				*.ISO_8859-8|*.ISO-8859-8|*.ISO8859-8|*.iso88598)
					echo "$lc_ctype ISO-8859-8" >> bin/elm.mimecharsets
					echo "(as ISO-8859-8)"
				;;
				*.ISO_8859-9|*.ISO-8859-9|*.ISO8859-9|*.iso88599)
					echo "$lc_ctype	ISO-8859-9" >> bin/elm.mimecharsets
					echo "(as ISO-8859-9)"
				;;
				*.ISO_8859-13|*.ISO-8859-13|*.ISO8859-13|*.iso885913)
					echo "$lc_ctype	ISO-8859-13" >> bin/elm.mimecharsets
					echo "(as ISO-8859-13)"
				;;
				*.ISO_8859-14|*.ISO-8859-14|*.ISO8859-14|*.iso885914)
					echo "$lc_ctype	ISO-8859-14" >> bin/elm.mimecharsets
					echo "(as ISO-8859-14)"
				;;
				*.ISO_8859-15|*.ISO-8859-15|*.ISO8859-15|*.iso885915)
					echo "$lc_ctype	ISO-8859-15" >> bin/elm.mimecharsets
					echo "(as ISO-8859-15)"
				;;
				*.EUC-JP|*.eucJP)
					echo "$lc_ctype	EUC-JP" >> bin/elm.mimecharsets
					echo "(as EUC-JP)"	
                                ;;
				*.EUC-KR|*.eucKR)
					echo "$lc_ctype	EUC-KR" >> bin/elm.mimecharsets
					echo "(as EUC-KR)"	
				;;
				*.*)
				    charmap="`echo \"$lc_ctype\"|$sed 's/^.*\.//'`"
				    . ConfTool/CharMap
				    if $test "$result" = "(unsupported)" ; then
					   file=doc/mime.charsets
					   first="$lc_ctype"
				  	   . ConfTool/GrepValue   
					   if $test "$result" != "" ; then
						echo "$lc_ctype $result" >> bin/elm.mimecharsets
			    			echo "   (as $result from doc/mime.charsets)"
					    else
					        echo "$lc_ctype	(unsupported)" >> bin/elm.mimecharsets
					        echo "(unsupported)"
                                           fi
				    else 
					echo "$lc_ctype	$result" >> bin/elm.mimecharsets				      
					echo "(as $result)"
				    fi 
				;;
				*)
				. ConfTool/LocaleMap
				if $test "$result" != "" ; then
 					echo "$lc_ctype $result" >> bin/elm.mimecharsets
					echo "(as $result from $locale_dir/locale.alias)"
				else
					file=doc/mime.charsets
					first="$lc_ctype"
				  	. ConfTool/GrepValue   
					if $test "$result" != "" ; then
						echo "$lc_ctype $result" >> bin/elm.mimecharsets
			    			echo "   (as $result from doc/mime.charsets)"
					else
						bad="$bad $lc_ctype"
						echo "#$lc_ctype (unknown)" >> bin/elm.mimecharsets
						echo "(unknown)"
					fi
				fi
				;;
			esac
                     fi
		fi
	done

	# Read GNU localedb charmaps
	# I hope that understand them correctly
	charsetvalues=""
	if [ "$d_locale" = "$define" ] ; then
	    if $test $locale != locale ; then
		charsetvalues="`$locale -m 2>/dev/null`"
	    fi
    	fi

	if $test "$charsetvalue" != "" ; then
	        	$echo "# Mappings for $locale -m" >> bin/elm.mimecharsets
	fi

	printit='$echo "* Adding map definations for locale charmaps"; printit='
	for charmap in $charsetvalues ; do
		eval "$printit"
		first="*.$charmap"
		file=bin/elm.mimecharsets
		. ConfTool/GrepValue
		if $test "$result" = "(unsupported)" ;  then		
			echo "  *.$charmap (unsupported)"
		elif $test "$result" != "" ;  then
			echo "  *.$charmap ok (as $result)"
		else
		    case "$charmap" in
		    *)
			echo $n "  Adding *.$charmap $c"
			. ConfTool/CharMap
			if $test "$result" = "(unsupported)" ; then
			    echo "*.$charmap	(unsupported)" >> bin/elm.mimecharsets
			    echo "(unsupported)"
			else 
			    echo "*.$charmap	$result" >> bin/elm.mimecharsets
			    echo "(as $result)"
			    if $test "$extra" != "" ; then
			        
				echo "*.$extra	$result" >> bin/elm.mimecharsets
				echo "  Adding *.$extra (as $result)"
			    fi
			fi
		    ;;
		    esac
    	       fi
	
	done

	if $test "$locale_dir" != none ; then
	    if $test -f $locale_dir/locale.alias ; then
		aa="# $locale_dir/locale.alias gives:"
		if $fgrep "$aa" bin/elm.mimecharsets ; then
		    $echo "* Assuming that $locale_dir/locale.alias is already copied to elm.mimecharsets as comment"
		else
		    $echo "$aa" >> bin/elm.mimecharsets
		    $sed 's/^/#/g' < $locale_dir/locale.alias >> bin/elm.mimecharsets
		    $echo "* Copying $locale_dir/locale.alias to elm.mimecharsets as comment"
		fi
	    fi
	fi
	






