#!/usr/bin/tclsh

#----------------------------------#
# this skript create the           #
# file section needed for          #
# the SaX start configuration      #
#----------------------------------#


proc GetOptions {iOptionList} {
#####--GetOptions--#######
# This function read all options
# possible are:
# --lang    id
#
upvar $iOptionList option
global argv

set    argcount     "0"
set    arg          ""
set    option(--lang)     ""

while { 1 } {

 set arg [ lindex $argv $argcount ]
 incr argcount
 switch -glob -- "$arg" {
            -l             -
            --lang       {

               set arg "--lang"
            }


            -*           {

                puts "PrepFileSection: $arg is not a valid option."
                exit "0"
            }

            default {

                break

            }
 }
 set option($arg) [ lindex $argv $argcount ]
 incr argcount

 if { $option($arg) == "" } {

  puts "PrepFileSection: missing parameter to option $arg"
  exit "0"
 }
}
}


#---------------------#
# available languages #
#---------------------#
set type(de) "german"
set type(en) "english"
set type(fr) "french"
set type(it) "italian"
set type(es) "spanish"
set type(br) "brazilian"
set type(bz) "breton" 
set type(nl) "dutch"
set type(hu) "hungarian"
set type(pl) "polish"
set type(pt) "portuguese"
set type(sk) "slovak"
set type(ro) "romanian"
set type(el) "greek"
set type(id) "indonesian"
set type(ru) "russian"
set type(tr) "turkish"
set type(sv) "swedish"
set type(cs) "czech"
set type(ja) "japanese"
set type(ko) "korean"

#------------------#
# get options      #
#------------------#
global OptionList
GetOptions OptionList
set language "invalid"
if { $OptionList(--lang) != "" } {

 set id [ string tolower $OptionList(--lang) ]
 set id [ string trim $id ]

 set found "no"
 foreach i [ array names type ] {
  if { $i == $id } {
   set found "yes"
   break
  }
 }

 if { $found == "yes" } { 
  set language $type($id)
 } else {
  set language "invalid"
 }
}


#-----------------#
# available types #
#-----------------#
set speachtype(german)        "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
set speachtype(english)       "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
set speachtype(french)        "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
set speachtype(italian)       "/usr/X11R6/lib/X11/fonts/latin1/75dpi"
set speachtype(spanish)       "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
set speachtype(brazilian)     "/usr/X11R6/lib/X11/fonts/latin1/75dpi"
set speachtype(breton)        "/usr/X11R6/lib/X11/fonts/latin1/75dpi"
set speachtype(dutch)         "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
set speachtype(hungarian)     "/usr/X11R6/lib/X11/fonts/latin2/75dpi"
set speachtype(polish)        "/usr/X11R6/lib/X11/fonts/latin2/75dpi"
set speachtype(portuguese)    "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
set speachtype(slovak)        "/usr/X11R6/lib/X11/fonts/latin2/75dpi"
set speachtype(romanian)      "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
set speachtype(greek)         "/usr/X11R6/lib/X11/fonts/latin7/75dpi"
set speachtype(indonesian)    "/usr/X11R6/lib/X11/fonts/latin1/75dpi"
set speachtype(russian)       "/usr/X11R6/lib/X11/fonts/cyrillic"
set speachtype(turkish)       "/usr/X11R6/lib/X11/fonts/latin2/75dpi"
set speachtype(swedish)       "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
set speachtype(czech)         "/usr/X11R6/lib/X11/fonts/latin2/75dpi /usr/X11R6/lib/X11/fonts/latin2/misc"
set speachtype(japanese)        "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"
set speachtype(korean)        "/usr/X11R6/lib/X11/fonts/75dpi:unscaled"

#-------------------#
# standard FontPath #
#-------------------#
set var(FileFontPath) "{/usr/X11R6/lib/X11/fonts/local} \
                       {/usr/X11R6/lib/X11/fonts/misc:unscaled} \
                       {/usr/X11R6/lib/X11/fonts/75dpi:unscaled} \
                       {/usr/X11R6/lib/X11/fonts/100dpi:unscaled} \
                       {/usr/X11R6/lib/X11/fonts/Type1} \
                       {/usr/X11R6/lib/X11/fonts/URW} \
                       {/usr/X11R6/lib/X11/fonts/Speedo} \
                       {/usr/X11R6/lib/X11/fonts/misc} \
                       {/usr/X11R6/lib/X11/fonts/75dpi} \
                       {/usr/X11R6/lib/X11/fonts/100dpi} \
                       {/usr/X11R6/lib/X11/fonts/PEX} \
                       {/usr/X11R6/lib/X11/fonts/cyrillic} \
                       {/usr/X11R6/lib/X11/fonts/latin2/misc} \
                       {/usr/X11R6/lib/X11/fonts/latin2/75dpi} \
                       {/usr/X11R6/lib/X11/fonts/latin2/100dpi} \
                       {/usr/X11R6/lib/X11/fonts/latin2/Type1} \
                       {/usr/X11R6/lib/X11/fonts/latin7/75dpi} \
                       {/usr/X11R6/lib/X11/fonts/kwintv} \
                       {/usr/X11R6/lib/X11/fonts/uni} \
                       {/usr/X11R6/lib/X11/fonts/baekmuk} \
		               {/usr/X11R6/lib/X11/fonts/ucs/misc} \
                       {/usr/X11R6/lib/X11/fonts/ucs/75dpi} \
                       {/usr/X11R6/lib/X11/fonts/ucs/100dpi} \
                       {/usr/X11R6/lib/X11/fonts/hellas/misc} \
                       {/usr/X11R6/lib/X11/fonts/hellas/75dpi} \
                       {/usr/X11R6/lib/X11/fonts/hellas/100dpi} \
                       {/usr/X11R6/lib/X11/fonts/hellas/Type1} \
                       {/usr/X11R6/lib/X11/fonts/xtest}"


set var(TmpConfig)     "/tmp/startconfig"
set var(StartConfig)   "/var/X11R6/sax/config/StartConfig"
set var(OrigConfig)    "/var/X11R6/sax/config/StartConfig.orig" 

#-------------------------------------------#
# get language from rc.config or as option  #
#-------------------------------------------#
if { $language == "invalid" } {
 catch { set speach  [ eval exec { cat /etc/rc.config | grep ^LANGUAGE= } ] }
} else {
 set speach "LANGUAGE=\"$language\""
}

#-----------------------------------#
# search for first FontPath setting #
#-----------------------------------#
set speach [ string trim $speach ]
set dummy ""
set left  ""
set right ""
regexp -- {(.*)=(.*)} $speach dummy left right
set right [ string trim $right {"} ]

set found "no"
set first  $speachtype(english);
foreach i [ array names speachtype ] {
 if { $i == $right } {
   set found "yes"
   set first $speachtype($i);
   break
 }
}

  
#------------------------------------#
# create full section and add it to  #
# /var/X11R6/sax/config/StartConfig  #
#------------------------------------#
set handle [ open $var(TmpConfig) "w" ]

puts $handle "#-----------------------------#"
puts $handle "# sample start configuration  #"
puts $handle "#-----------------------------#"
puts $handle "\n"
puts $handle "Section \"Files\""
puts $handle "  RgbPath\t\"/usr/X11R6/lib/X11/rgb\""
foreach i $first {
 puts $handle "  FontPath\t\"$i\""
} 

foreach i $var(FileFontPath) {
 if { $first != $i } {
  puts  $handle "  FontPath\t\"$i\""
 }
}

puts $handle "EndSection\n"

set config [ open $var(StartConfig) "r" ]
while { [ eof $config ] == 0 } {
 set line [ gets $config ]
 puts $handle $line
}

close $handle
close $config

catch { exec cp $var(TmpConfig) $var(StartConfig) }
catch { exec rm -f $var(TmpConfig) }
