#! C:\Tcl\wish84
encoding system utf-8

set ::files {	Z_confg.cfg
                  convert.exe
		      logo.ico
		      Main.exe
                  Const.txd
		      images
                  images_
                  data 
                  data_0 
                  Msg_en.tcl
                  Msg_ru.tcl 
                  Msg_pl.tcl
                  Msg_lt.tcl 
                  Doc_en
                  Doc_ru 
                  Doc_pl
                  Doc_lt 
                  "PL-2303 Driver Installer.exe"      
}

option add *font {Verdana 12}
set font_OK "-family Times             -size 16 -weight bold " 
option add *Button.font {Verdana 12 bold}
option add *l.background white
option add *padY 4
option add *padX 4


package require msgcat
interp alias {} _ {} msgcat::mc

proc confirm {} {
	if {[tk_messageBox -icon question -type yesno -title [_ "Confirmation"]\
		-message [_ "Do you really want to cancel installation and exit?"]]=="yes"} {
			exit
	}
}

proc install_files {srcdir destdir files} {
	foreach x $files {
		set dest [file nativename [file join $destdir $x]]
		while {[catch {
			file delete -force $dest
			file copy -force [file nativename [file join $srcdir $x]] $dest
		} reason]} {
			if {[string match *.exe $x ]} {
				append reason "\n\n" "Make sure $x is not runinng."
			}
			set answer [tk_messageBox -icon error -type abortretryignore\
				-title [_ "Installation error"]\
				-message "[_ {Error copying file}] $x: $reason"]
			if { $answer=="abort"  } exit
			if { $answer=="ignore" } break
		}
		.l configure -text $dest
		update
	}
      set dest [file nativename [file join $destdir Msg.tcl]]
      catch { file delete -force $dest 
              file copy -force [file nativename [file join $srcdir Msg_$::language.tcl]] $dest
      }
      set dest [file nativename [file join $destdir Doc]]
      catch { file delete -force $dest 
              file copy -force [file nativename [file join $srcdir Doc_$::language]] $dest
      }

       exec cmd /c attrib -r  [file nativename [file join $destdir/data  *.dt ] ]   /s 
       exec cmd /c attrib -r  [file nativename [file join $destdir/data_0 *.dt ] ]  /s
       exec cmd /c attrib -r  [file nativename [file join $destdir/images_ *.*  ] ] /s
       exec cmd /c attrib -r  [file nativename [file join $destdir Z_confg.cfg  ] ]
	return 1
}

proc set_language {} {
	msgcat::mclocale $::language
	catch { source $::language.msg }
	if {[info commands textlabels]!=""} textlabels
}

proc language_selector {name} {
	frame $name
	
	foreach f [glob [file join images lang_*.gif]] {
		set part [string range [file root [file tail $f]] 5 end]
		set img [image create photo -file $f]
		pack [radiobutton $name.b$part -text $part -variable ::language\
			-value $part -image $img -indicatoron 0 -compound top -command set_language ]\
			 -fill y -side left
		if {[lsearch [msgcat::mcpreferences] $part]!=-1} {
			$name.b$part invoke
		}
	}
	if {$::language == "" && [info exists part]} {
		$name.b$part invoke
	}
	return $name
}

proc Install {} {
  global font_OK  Uptitle

	 set files_to_install $::files

	.l configure -text [_ "Installation in progress"]
	.f.btn configure -state disabled
	.f.vol configure -state disabled
	wm protocol . WM_DELETE_WINDOW {}

	set destdir [file nativename [file join "$::targetVolume/" Z_Up-x8-06]]
	if {![file isdirectory $destdir]} {
		if {[catch { file mkdir $destdir } reason]} {
			.l configure -text "[_ "Can not create target directory"] $destdir:\n$reason"
			return
		}
	}
	if {[file exists $destdir]} { 
		.f.vol configure -foreground red 
	}
   
	install_files . $destdir $files_to_install

		
	if {[ catch {
		     set desktop [::freewrap::getSpecialDir DESKTOP]
                 append Uptitle .lnk
		     set shk [file nativename $Uptitle]
		     set shk_dir [file join $desktop $shk]

		     set obj [file nativename $destdir/Main.exe]

		     ::freewrap::shortcut $shk_dir -objectPath $obj -ico [file nativename $destdir/logo.ico] 0 -workingDirectory  $destdir
                 
	           } reason ]
      }      { .l configure -text [_ "An error occured during installation "]\n$reason
	} else { .l configure -text [_ "Installation is completed.\nNow You can install USB PL2303 driver."] -font $font_OK
                pack  .fdown.installPL -side left -expand 1 -padx 20
	}

	.fdown.close configure -text [_ "Exit"]
}

#--------------------------- Start -------------------
  set fd [open Const.txd r]
  set Uptitle [gets $fd]
  close $fd   ;   unset fd
  set Uptitle [string range $Uptitle 11 end-8]

wm title . "$Uptitle [_ "Installer"]"
wm attributes . -topmost 1
wm protocol . WM_DELETE_WINDOW confirm

set scrW05 [expr int([winfo screenwidth .]/2)]
set scrH05 [expr int([winfo screenheight .]/2)]
set start_panel_x [expr int(0.2*$scrW05)]
set start_panel_y $scrH05
wm geometry . +$start_panel_x+$start_panel_y

pack [label .l] -expand yes -fill both

pack [language_selector .lang] -expand yes -fill both
pack [frame .f]                -expand yes -fill both
pack [frame .fdown]            -expand yes -fill both

pack [button .fdown.close -command {destroy .}] -side left -expand 1
button .fdown.installPL -text [_ "install usb Driver" ] \
     -command { wm withdraw .  ;  after 10 exit  ;  exec "PL-2303 Driver Installer.exe" }
             

set volumes [list]
foreach volume [file volumes] {
	if {[string match {[C-Z]:/} $volume]} {
		lappend volumes [string range $volume 0 1]
	}
}

label .f.lb  -anchor e
eval [list tk_optionMenu .f.vol targetVolume] $volumes
button .f.btn -command Install -anchor w
pack .f.lb -side left -expand yes -fill both
pack .f.vol .f.btn -side left -expand yes -fill y


proc textlabels {} {  global Uptitle
	.f.lb configure  -text [_ "Select drive to install:"]
	.f.btn configure -text [_ Install]
	.l configure     -text "[_ "This program will install"]  $Uptitle \n [_ "on your computer"]"
 	.fdown.close configure -text [_ "Cancel installation"]
}

textlabels
        
