# $Header: /home/users/hurtta/cvsroot/mail/elmme+/ConfTool/GenCharmap,v 2.6 2015/09/21 16:00:10 hurtta Exp $

write_header() {
		cat >bin/elm.mimecharsets <<'EOM'
# Mapping from LC_CTYPE locale to MIME's charset values
# Format is:
#	LC_CTYPE	charset
#
# That format can be used override mapping given by nl_langinfo(CODESET)
#
# If LC_CTYPE is on form
#	LL_cc.encoding
# then also following formats are available:
#	LL_cc		charset
#	LL		charset
#	*.encoding	charset
# These mappings are only used if nl_langinfo(CODESET) is not supported
#
#       -               charset;type=ascii-set;MIME-subset=US-ASCII
#       -               charset;type=xxx;MIME-subset=some-subset-name
#       -               charset;alias=alias-name
EOM
}

	if test ! -d ConfTool ; then
	   $echo "Creating build subdirectory ConfTool"
	   mkdir ConfTool || exit 1

	   cp "$source_dir/ConfTool/Makefile" \
 "$source_dir/ConfTool/validcharset.c" ConfTool/ || exit 1

	else
	   cmp "$source_dir/ConfTool/Makefile" ConfTool/Makefile || \
  cp "$source_dir/ConfTool/Makefile" ConfTool/Makefile
	   cmp "$source_dir/ConfTool/validcharset.c" ConfTool/validcharset.c || \
  cp "$source_dir/ConfTool/validcharset.c" ConfTool/validcharset.c
	fi

	is_validcharset=
	if (CC="$cc"; export CC; cd ConfTool && $make validcharset SOURCE_DIR="$source_dir"); then
		is_validcharset=y
		$echo "ConfTool/validcharset compiled"
	else
		$echo "Failed to generate ConfTool/validcharset ..."
		$echo "bin/elm.mimecharsets will be incorrent or incomplete"
		sleep 20
		is_validcharset=n
	fi

	write_header

	printit='$echo "* Adding map definations from ConfTool/mapfiles"; printit='
	while read file set rest ; do
	    if $test -f "$lib/elm.map.txt/$file" -o \
	       -f "$source_dir/charset/MAPPINGS/ISO8859/$file" -o \
	       -f "$source_dir/charset/MAPPINGS/$file" -o \
	       -f "$source_dir/charset/MAPPINGS/MICSFT/$file" ; then
		eval "$printit"
		if $egrep -i "^-.*$set;alias=[^;]+\$" bin/elm.mimecharsets > /dev/null; then
		    $echo "  Charset $set alias already set"
		    echo "-	$set$rest;map=$file" >> bin/elm.mimecharsets
		    echo "  Adding map=$file for $set"
		elif $egrep -i "^-.*$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 < "$source_dir/ConfTool/mapfiles"

	printit='$echo "* Adding iso646 definations from ConfTool/iso646maps"; printit='
	while read set map ; do
	    eval "$printit"
	    if $egrep -i "^-.*$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 < "$source_dir/ConfTool/iso646maps"

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

	printit='$echo "* Adding charset definations from doc/mime.charsets"; printit='
	while read loc set rest ; do	    
	    set="`echo \"$set\" | $sed 's/$;//'`"
	    case "$loc" in
		"-")
		eval "$printit"
		if $egrep -i "^-.*$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 < "$source_dir/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 -i "^-.*$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 < "$source_dir/ConfTool/charaliases.map"

	IFS="$ifs"

	if $test "$nomap" = y ; then
	    $echo "* map definations for locales are not added"
	    values=
	elif $test "$i_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
		. "$source_dir/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|*.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)"
				;;
				*.ISO_8859-16|*.ISO-8859-16|*.ISO8859-16|*.iso885916)
					echo "$lc_ctype	ISO-8859-16" >> bin/elm.mimecharsets
					echo "(as ISO-8859-16)"
				;;
				*.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)"	
				;;
				*.utf-8|*.utf8|*.UTF-8)
					echo "$lc_ctype	UTF-8" >> bin/elm.mimecharsets
					echo "(as UTF-8)"	
				;;
				*.*)
				    charmap="`echo \"$lc_ctype\"|$sed 's/^.*\.//'`"
				    . "$source_dir/ConfTool/CharMap"
				    if $test "$result" = "(unsupported)" ; then
					   file="$source_dir/doc/mime.charsets"
					   first="$lc_ctype"
				  	   . "$source_dir/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 
				;;
				*)

				. "$source_dir/ConfTool/LocaleMap"
				if $test "$result" != "" ; then
 					echo "$lc_ctype $result" >> bin/elm.mimecharsets
					echo "(as $result$source)"
				else
					file="$source_dir/doc/mime.charsets"
					first="$lc_ctype"
				  	. "$source_dir/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 $test "$nomap" = y ; then
	    $echo "* map definations for locale charmaps are not added"
	    charsetvalues=
	elif [ "$i_locale" = "$define" ] ; then
	    if $test $locale != locale ; then
		charsetvalues="`$locale -m 2>/dev/null`"
	    fi
    	fi

	if $test "$charsetvalues" != "" ; 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
		. "$source_dir/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"
			. "$source_dir/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
	






