########################################################################
#
# "main_ini" : Tvi, ¹ԥץ
#
# Copyright (c) 1998,1999  Motomu Nakashima
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option) 
# any later version.
#
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
# GNU General Public License for more details.
#
########################################################################

###########################
# եνɤ߹   
###########################

set current_dir [ pwd ] ; # ȥǥ쥯ȥĴ٤

# İʾξ硤 exit
if { [llength $argv ] > 1 } { 
     return -code error "顼¿뤫ְäƤޤ"

# Ĥξ
} elseif { [llength $argv ] == 1 } { 
     set filename [lindex $argv 0] ; # Ȥꤢfilenameˤ
     set dirlist [split $current_dir / ]  ; # ȥǥ쥯ȥꥹȲ

     # եѥǻꤷƤʳХѥȤߤʤϤ
     if {[string index $filename 0] != "/"} {
        set filelist  [split $filename / ] ; # filename ꥹȲ
        set fullpathlist [concat $dirlist $filelist]  
                                             # dirlist  filelist Ĥʤ

        # ޤ "." õ
        while { [lsearch -exact $fullpathlist "." ] != -1 } {
           set fullpathlist [lreplace $fullpathlist \
                               [lsearch -exact $fullpathlist "." ] \
                               [lsearch -exact $fullpathlist "." ] ] }

        # Ĥ ".." õ(ǤȰ˾äɤ)
        while { [lsearch -exact $fullpathlist ".." ] != -1 } {
           set fullpathlist [lreplace $fullpathlist \
                     [expr [lsearch -exact $fullpathlist ".." ] - 1 ] \
                               [lsearch -exact $fullpathlist ".." ] ] }
        # filename 
        set filename [join $fullpathlist / ]

     } else { }

     # filename ¸ߤ뤫ݤǿե뤫ݤȽ
     if {[file exists $filename] == 1 } {
        set new_filename 0
     } else { 
        set new_filename 1 
     }

     # file_dir 
     set fullpathlist [split $filename / ]
     set file_dir [lrange $fullpathlist 0 [expr [llength $fullpathlist ] - 2 ]]
     set file_dir [join $file_dir / ]

# ʤ
} else {
     set filename ${current_dir}/unnamed.tex
     set new_filename 1 
     set file_dir $current_dir
}

loadfile $filename

# compile_file νͤ
set compile_file "Not TeX file"
#  .tex ä filename  .tex äΤˤ
if {[string match "*.tex" $filename] == 1 } {
   set compile_file [string range $filename 0 \
                       [ expr [string length $filename] - 5 ]]
}

cd $file_dir 

# ɤν
kanji defaultOutputCode $kanji_code

##################################
# LaTeX طν¹ԥץ #
##################################

# colorize_flag Ǽ¹ԤȽǤ
if {$colorize_flag == 1 } { after 10 { colorize 1.0 end } }

# ޥɥꥹȤɤ߹
set f [open $tvi_directory/latex_commands] 
set latex_cmds [read $f 10000000] 
close $f 

# ĶꥹȤɤ߹
set f [open $tvi_directory/latex_environments] 
set latex_envs [read $f 10000000] 
close $f 

# ꥹȤɤ߹
set f [open $tvi_directory/latex_symbols] 
set latex_symbols [read $f 10000000] 
close $f 

