#!/bin/sh

echo "/* Makefile-generated, don't edit! */" >converters.hh

echo -n 'CSETOBJS=' > charsets.mak

echo >charsets.tmp

converter()
{
  declaration="$2$3"

  echo '#ifdef CHARCONV_LIST'            >>converters.hh

  echo '    "'$1'",'                     >>converters.hh

  echo '#endif'                          >>converters.hh
  
  if test ! "$4" = ""; then
    echo '#ifdef INCLUDEDATA'            >>converters.hh
    echo '# include "'$4'"'              >>converters.hh
    echo '#endif'                        >>converters.hh
  fi
  
  echo '#ifdef INCLUDECONV'              >>converters.hh
  echo '# include "../'$2'.hh"'          >>converters.hh
  echo '#endif'                          >>converters.hh

  echo '#ifdef CONVLIST'                 >>converters.hh
  for ss in $converters; do
      echo -n '"'"$ss"'",'               >>converters.hh
  done
  echo                                   >>converters.hh
  echo '#endif'                          >>converters.hh
  echo '#ifdef CASELIST'                 >>converters.hh
  echo -n '   '                          >>converters.hh
  for ss in $converters; do
      echo -n " case $conum:"            >>converters.hh
      conum=$[conum+1]
  done
  echo " return new $declaration;"       >>converters.hh
  echo '#endif'                          >>converters.hh
}

declare -x conum
conum=0
dotable()
{
	# 1=converter class
	# 2=filename
	# 3=classname
	# 4=xx in xx_to_uni and uni_to_xx
	# 5=infile
	
	converter="$1"
	shift
	
	echo -n " $1".o >>charsets.mak
	echo "$1.o: $1.cc" >>charsets.tmp
	echo '	$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $@ -c $<' >>charsets.tmp

	if test ! -z "$converter"; then
	  declare -a A;A=($converters)
	  
	  converter "${A[0]}" "$converter" '(new '$2'_to_'$3', new '$3'_to_'$2')' "$1.h"
	fi
	
	echo "$1.cc $1.h: table2h $infn" >>charsets.tmp
	echo "	tr '\\015' '\\012' <$infn >$1.tmp && ./table2h $* <$1.tmp >$1.h && rm -f $1.tmp" >>charsets.tmp
}

converters="unihtml"
converter  "unihtml    (&#number; codes)"           "unihtml" "()"
converters="utf8linux"
converter  "utf8linux  (with vt100 escape codes)"   "utf8" "(true)"
converters="utf7mod"
converter  "utf7mod    (imap modified)"             "utf7" "(true)"

converters="koi8-r"
infn=VENDORS/MISC/KOI8-R.TXT
dotable easycharset koi8r koi8r uni

infn=EASTASIA/JIS/JIS0212.TXT
dotable "" jis0212      jis212 uni
infn=EASTASIA/JIS/JIS0208.TXT
dotable "" jis0208 sjis jis208 uni

converters="jis-x-0201"
infn=EASTASIA/JIS/JIS0201.TXT
dotable easycharset jis0201 jis201 uni

converters="shift_jis sjis ms_kanji"
infn=EASTASIA/JIS/SHIFTJIS.TXT
dotable shift_jis sjis sjis uni

converters="big5 bigfive"
infn=EASTASIA/OTHER/BIG5.TXT
dotable big5 big5 big5 uni

for s in 1 2 3 4 5 6 7 8 9; do
	converters="iso-8859-$s latin$s iso0$s"
	infn=ISO8859/8859-$s".TXT"
	dotable easycharset iso0$s iso0$s uni
done
for s in 10 13 14 15; do
	converters="iso-8859-$s latin$s iso$s"
	infn=ISO8859/8859-$s".TXT"
	dotable easycharset iso$s iso$s uni
done
for s in 437 737 775 850 852 855 857 860 861 862 863 864 865 866 869 874; do
	converters="cp$s"
	infn=VENDORS/MICSFT/PC/CP$s".TXT"
	dotable easycharset cp$s cp$s uni
done
for s in 1250 1252 1254 1256 1258 1251 1253 1255 1257; do
	converters="cp$s"
	infn=VENDORS/MICSFT/WINDOWS/CP$s".TXT"
	dotable easycharset cp$s cp$s uni
done
# The huge sets - we don't need them, don't we?
#for s in 932 949 936 950; do
#	converters="cp$s"
#	infn=VENDORS/MICSFT/WINDOWS/CP$s".TXT"
#	dotable easycharset cp$s cp$s uni
#done
for s in 856 1006 424; do
	converters="cp$s"
        infn=VENDORS/MISC/CP$s".TXT"
	dotable easycharset cp$s cp$s uni
done

converters="roman hp_mcs apple"
infn=VENDORS/APPLE/ROMAN.TXT
dotable easycharset roman roman uni

#converters="romanian"
#infn=VENDORS/APPLE/ROMANIAN.TXT
#dotable easycharset romanian romanian uni

converters="iso-2022-jp jis7 jis208"
converter  "iso-2022-jp" "jis208" "()"
converters="utf8"
converter  "utf8"        "utf8" "(false)"
converters="utf7"
converter  "utf7"        "utf7" "(false)"
converters="euc-jp ajec"
converter  "euc-jp"      "eucjp" "()"

cat charsets.tmp >> charsets.mak
rm -f charsets.tmp
echo >&2
