#!/bin/sh
# -*- tcl -*- \
exec tclsh "$0" ${1+"$@"}

# Convert the escape sequences and UTF-8 to ABC for abcm2ps
#	
# Copyright (C) 2007 Jean-François Moine

# translation table from ABC draft version 2 (accents first)
#	` grave
#	' acute
#	^ circumflex
#	, cedilla
#	" umlaut
#	~ tilde
#	o ring
#	= macron or stroke
#	'/' slash
#	; ogonek
#	v caron
#	u breve
#	: long Hungarian umlaut
#	. dot / dotless
# else, ligatures
#	ae ss ng
set seq_lst [list \
	\\`A	"\xc3\x80"	\
	\\`E	"\xc3\x88"	\
	\\`I	"\xc3\x8c"	\
	\\`O	"\xc3\x92"	\
	\\`U	"\xc3\x99"	\
	\\`a	"\xc3\xa0"	\
	\\`e	"\xc3\xa8"	\
	\\`i	"\xc3\xac"	\
	\\`o	"\xc3\xb2"	\
	\\`u	"\xc3\xb9"	\
	\\\'A	"\xc3\x81"	\
	\\\'E	"\xc3\x89"	\
	\\\'I	"\xc3\x8d"	\
	\\\'O	"\xc3\x93"	\
	\\\'U	"\xc3\x9a"	\
	\\\'Y	"\xc3\x9d"	\
	\\\'a	"\xc3\xa1"	\
	\\\'e	"\xc3\xa9"	\
	\\\'i	"\xc3\xad"	\
	\\\'o	"\xc3\xb3"	\
	\\\'u	"\xc3\xba"	\
	\\\'y	"\xc3\xbd"	\
	\\\'S	"\xc5\x9a"	\
	\\\'Z	"\xc5\xb9"	\
	\\\'s	"\xc5\x9b"	\
	\\\'z	"\xc5\xba"	\
	\\\'R	"\xc5\x94"	\
	\\\'L	"\xc4\xb9"	\
	\\\'C	"\xc4\x86"	\
	\\\'N	"\xc5\x83"	\
	\\\'r	"\xc5\x95"	\
	\\\'l	"\xc4\xba"	\
	\\\'c	"\xc4\x87"	\
	\\\'n	"\xc5\x84"	\
	\\^A	"\xc3\x82"	\
	\\^E	"\xc3\x8a"	\
	\\^I	"\xc3\x8e"	\
	\\^O	"\xc3\x94"	\
	\\^U	"\xc3\x9b"	\
	\\^a	"\xc3\xa2"	\
	\\^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"	\
	"\\U;"	"\xc5\xb2"	\
	\\U?	"\xc5\xa8"	\
	\\U-	"\xc5\xaa"	\
	\\a-	"\xc4\x81"	\
	"\\i;"	"\xc4\xaf"	\
	\\e.	"\xc4\x97"	\
	\\i-	"\xc4\xab"	\
	\\n,	"\xc5\x86"	\
	\\o-	"\xc5\x8d"	\
	\\k,	"\xc4\xb7"	\
	"\\u;"	"\xc5\xb3"	\
	\\u?	"\xc5\xa9"	\
	\\u-	"\xc5\xab"


# glyph names
# (Dbar/dbar --> Dcroat/dcroat)
array set glyph {
	0xa0	/space
	0xa1	/exclamdown
	0xa2	/cent
	0xa3	/sterling
	0xa4	/currency
	0xa5	/yen
	0xa6	/brokenbar
	0xa7	/section
	0xa8	/dieresis
	0xa9	/copyright
	0xaa	/ordfeminine
	0xab	/guillemotleft
	0xac	/logicalnot
	0xad	/hyphen
	0xae	/registered
	0xaf	/macron
	0xb0	/degree
	0xb1	/plusminus
	0xb2	/twosuperior
	0xb3	/threesuperior
	0xb4	/acute
	0xb5	/mu
	0xb6	/paragraph
	0xb7	/periodcentered
	0xb8	/cedilla
	0xb9	/onesuperior
	0xba	/ordmasculine
	0xbb	/guillemotright
	0xbc	/onequarter
	0xbd	/onehalf
	0xbe	/threequarters
	0xbf	/questiondown
	0xc0	/Agrave
	0xc1	/Aacute
	0xc2	/Acircumflex
	0xc3	/Atilde
	0xc4	/Adieresis
	0xc5	/Aring
	0xc6	/AE
	0xc7	/Ccedilla
	0xc8	/Egrave
	0xc9	/Eacute
	0xca	/Ecircumflex
	0xcb	/Edieresis
	0xcc	/Igrave
	0xcd	/Iacute
	0xce	/Icircumflex
	0xcf	/Idieresis
	0xd0	/Eth
	0xd1	/Ntilde
	0xd2	/Ograve
	0xd3	/Oacute
	0xd4	/Ocircumflex
	0xd5	/Otilde
	0xd6	/Odieresis
	0xd7	/multiply
	0xd8	/Oslash
	0xd9	/Ugrave
	0xda	/Uacute
	0xdb	/Ucircumflex
	0xdc	/Udieresis
	0xdd	/Yacute
	0xde	/Thorn
	0xdf	/germandbls
	0xe0	/agrave
	0xe1	/aacute
	0xe2	/acircumflex
	0xe3	/atilde
	0xe4	/adieresis
	0xe5	/aring
	0xe6	/ae
	0xe7	/ccedilla
	0xe8	/egrave
	0xe9	/eacute
	0xea	/ecircumflex
	0xeb	/edieresis
	0xec	/igrave
	0xed	/iacute
	0xee	/icircumflex
	0xef	/idieresis
	0xf0	/eth
	0xf1	/ntilde
	0xf2	/ograve
	0xf3	/oacute
	0xf4	/ocircumflex
	0xf5	/otilde
	0xf6	/odieresis
	0xf7	/divide
	0xf8	/oslash
	0xf9	/ugrave
	0xfa	/uacute
	0xfb	/ucircumflex
	0xfc	/udieresis
	0xfd	/yacute
	0xfe	/thorn
	0xff	/ydieresis
	0x100	/Amacron
	0x101	/amacron
	0x102	/Abreve
	0x103	/abreve
	0x104	/Aogonek
	0x105	/aogonek
	0x106	/Cacute
	0x107	/cacute
	0x108	/Ccircumflex
	0x109	/ccircumflex
	0x10a	/Cdotaccent
	0x10b	/cdotaccent
	0x10c	/Ccaron
	0x10d	/ccaron
	0x10e	/Dcaron
	0x10f	/dcaron
	0x110	/Dcroat
	0x111	/dcroat
	0x112	/Emacron
	0x113	/emacron
	0x116	/Edotaccent
	0x117	/edotaccent
	0x118	/Eogonek
	0x119	/eogonek
	0x11a	/Ecaron
	0x11b	/ecaron
	0x11c	/Gcircumflex
	0x11d	/gcircumflex
	0x11e	/Gbreve
	0x11f	/gbreve
	0x120	/Gdotaccent
	0x121	/gdotaccent
	0x122	/Gcedilla
	0x123	/gcedilla
	0x124	/Hcircumflex
	0x125	/hcircumflex
	0x126	/Hstroke
	0x127	/hstroke
	0x128	/Itilde
	0x129	/itilde
	0x12a	/Imacron
	0x12b	/imacron
	0x12e	/Iogonek
	0x12f	/iogonek
	0x130	/Idotaccent
	0x131	/dotlessi
	0x134	/Jcircumflex
	0x135	/jcircumflex
	0x136	/Kcedilla
	0x137	/kcedilla
	0x138	/kra
	0x139	/Lacute
	0x13a	/lacute
	0x13b	/Lcedilla
	0x13c	/lcedilla
	0x13d	/Lcaron
	0x13e	/lcaron
	0x141	/Lslash
	0x142	/lslash
	0x143	/Nacute
	0x144	/nacute
	0x145	/Ncedilla
	0x146	/ncedilla
	0x147	/Ncaron
	0x148	/ncaron
	0x14a	/Eng
	0x14b	/eng
	0x14c	/Omacron
	0x14d	/omacron
	0x150	/Ohungarumlaut
	0x151	/ohungarumlaut
	0x154	/Racute
	0x155	/racute
	0x156	/Rcedilla
	0x157	/rcedilla
	0x158	/Rcaron
	0x159	/rcaron
	0x15a	/Sacute
	0x15b	/sacute
	0x15c	/Scircumflex
	0x15d	/scircumflex
	0x15e	/Scedilla
	0x15f	/scedilla
	0x160	/Scaron
	0x161	/scaron
	0x162	/Tcedilla
	0x163	/tcedilla
	0x164	/Tcaron
	0x165	/tcaron
	0x166	/Tbar
	0x167	/tbar
	0x168	/Utilde
	0x169	/utilde
	0x16a	/Umacron
	0x16b	/umacron
	0x16c	/Ubreve
	0x16d	/ubreve
	0x16e	/Uring
	0x16f	/uring
	0x170	/Uhungarumlaut
	0x171	/uhungarumlaut
	0x172	/Uogonek
	0x173	/uogonek
	0x179	/Zacute
	0x17a	/zacute
	0x17b	/Zdotaccent
	0x17c	/zdotaccent
	0x17d	/Zcaron
	0x17e	/zcaron
	0x2c7	/caron
	0x2d8	/breve
	0x2d9	/dotaccent
	0x2db	/ogonek
	0x2dd	/hungarumlaut
}

proc which-enc {c} {
# search the new encoding for a character
    global enc_tb enc_nb
    set c [format 0x%x $c]
    if {![info exists enc_tb($c)]} {
	if {$enc_nb >= 0xff} {
	    puts stderr "Too many utf-8 characters"
	    return 0x80
	}
	set enc_tb($c) [format 0x%x $enc_nb]
	incr enc_nb
    }
#puts "c: $c enc: $enc_tb($c)"
    return $enc_tb($c)
}

proc main {fnin fnout} {
# main proc
    global enc_tb enc_nb seq_lst glyph
    if {[catch {open $fnin r} in]} {
	puts "Cannot read file '$fnin'"
	exit 1
    }
    if {$fnout == "-"} {
	set out stdout
    } else {
	if {[catch {open $fnout w} out]} {
	    puts "Cannot create file '$fnout'"
	    exit 1
	}
    }
    fconfigure $in -encoding binary
    set file [read $in [file size $fnin]]
    close $in
    # convert the escape sequences to utf-8
    if {[string first "\\" $file] >= 0} {
      set file [string map $seq_lst $file]
    }
    # scan the file for utf-8 characters
    set result {}
#    set enc_tb {}
    set enc_nb 0xa0
    for {set i 0} {$i < [string length $file]} {incr i} {
	set c [string index $file $i]
	binary scan $c H* b1
	set bh1 0x$b1
	if {$bh1 < 0x80} {
	    append result $c
	    continue
	}
	incr i
	set c [string index $file $i]
	binary scan $c H* b2
	set bh2 0x$b2
	set b [expr {(($bh1 & 0x0f) << 6) + ($bh2 & 0x3f)}]
	set c [which-enc $b]
	append result [binary format c $c]
    }
    if {[info exists enc_tb]} {
	puts $out {%%beginps
ISOLatin0Encoding}
	foreach v [array names enc_tb] {
#puts "enc: $enc_tb($v) v: $v"
	    set i [format %02x $enc_tb($v)]
	    puts $out "dup 16#$i $glyph($v) put"
	}
	puts $out {pop
%%endps}
    }
    puts $out $result
}

if {$argc != 2} {
    puts {Convert escape sequences and UTF-8 to ABC for abcm2ps
Usage: ./utf2abcm.tcl <input ABC file> <output file>
  <output file> may be '-' for stdout}
	exit 1
}

main [lindex $argv 0] [lindex $argv 1]
