#!/usr/bin/wish8.3
#
# Graphical User Interface for EWB ver.3.3
# Copyright 1998-2002 ASCII Corp.
#

set guilib /usr/share/ewb/3.3/guilib
set newbindir /usr/lib/ewb/3.3/bin
set oldbindir /usr/lib/ewb/3.3/bin
set printcap /etc/printcap

set bindir $newbindir

source $guilib/guipastcore.tcl

set rootwd [winfo screenwidth .]
set rootht [winfo screenheight .]
wm geometry . +[expr $rootwd / 2 - 400]+[expr $rootht /2 - 300]
wm title . "guishell for EWB 3.3"

if {![string compare $tcl_version "8.0"]} {
	font delete Mincho:Helvetica-Bold-12
	font create Mincho:Helvetica-Bold-12 -compound {a14 k14}
	font delete Mincho:Helvetica-12
	font create Mincho:Helvetica-12 -compound {a14 k14}
} else {
	option add *Font a14
	option add *Font k14
	catch {tk useinputmethods 1}
}

set newewb 1
set mode 1
set pagemode 1
frame .f0
frame .f1
frame .f3
frame .f7
frame .menuset

# ñʥåν
proc errormsg {msg} {
	global rootwd rootht

	toplevel .errlog
	wm geometry .errlog +[expr $rootwd / 2 - 250]+[expr $rootht /2 - 100]
	message .errlog.m0 -width 500 -text $msg
	button .errlog.b0 -text "OK" -command {
		grab release .errlog
		destroy .errlog
	}
	bind .errlog <Destroy> {
		grab release .errlog
	}
	pack .errlog.m0 -padx 10 -pady 10
	pack .errlog.b0
	grab set .errlog
	wm resizable .errlog 0 0
	tkwait window .errlog
}

# ץ󥿾μ
set prcount 0
if {[catch {set fd [open $printcap r]}]} {
	errormsg "ץ󥿾ե $printcap ɤޤ"
} else {
	while {[gets $fd buff] != -1} {
		if {[string first "#" $buff] != 0 && [string first " " $buff] != 0 && [string first "\t" $buff] != 0 && [string compare "" $buff] != 0} {
			set printers($prcount) $buff
			if {[string first "|" $buff] != -1} {
				set printers($prcount) [string range $buff 0 [expr [string first "|" $buff]-1]]
			}
			set buff $printers($prcount)
			if {[string first ":" $buff] != -1} {
				set printers($prcount) [string range $buff 0 [expr [string first ":" $buff]-1]]
			}
			incr prcount
		}
	}
}

if {$prcount < 1} {
	errormsg "ץ󥿤ϻѤǤޤ"
} else {
	if {[catch {exec printenv PRINTER}]} {
		set printer $printers(0)
	} else {
		catch {exec printenv PRINTER} tmp
		set printer $tmp
	}
}
set papersize A4
set mamehonsize A4
set twopage 0

label .working -text "ߤΥǥ쥯ȥꡧ"
button .workingdir -text "[pwd]" -command {
	selectdir
}

listbox .l0 -height 25 -width 80 -yscrollcommand ".s1 set" -selectmode extended
scrollbar .s1 -orient vertical -command ".l0 yview"
menubutton .system -text "ƥ" -menu .system.m0 -width 10 -relief raised
menu .system.m0 -tearoff false
.system.m0 add command -label "ȥեޤȤ" -command {
	if {[file writable [pwd]]} {
		set cdir [pwd]
		set dirname [string range $cdir [expr [string last "/" $cdir] +1] end]
		ynmsg "ȥեޤȤơߤΥǥ쥯ȥ $dirname.tar ȤեޤǤ"
		if {$answer} {
			toplevel .mkarc
			wm geometry .mkarc +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
			label .mkarc.lb0 -text "ȥե $dirname.tar ˤޤȤƤޤ"
			pack .mkarc.lb0 -padx 50 -pady 50
			grab set .mkarc
			wm resizable .mkarc 0 0
			update

			catch {exec $newbindir/shelf -m cln} tmp
			cd ..
			catch {exec tar chf $cdir/$dirname.tar $dirname} tmp
			cd $cdir
			grab release .mkarc
			destroy .mkarc
			errormsg "ߤκȥǥ쥯ȥ $dirname.tar ޤ"
		} else {
			errormsg "ȥեΤޤȤߤޤ"
		}
	} else {
		errormsg "ߤΥǥ쥯ȥ˽񤭹ߤǤʤΤǺȥեޤȤޤ"
	}
}
.system.m0 add command -label "ȥեŸ" -command {
	selectarc
	if {[string compare $arcfile ""]} {
		toplevel .tarfile
		wm geometry .tarfile +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
		label .tarfile.lb0 -text "$arcfile Ĵ٤Ƥޤ"
		pack .tarfile.lb0 -padx 50 -pady 50
		grab set .tarfile
		wm resizable .tarfile 0 0
		update

		catch {exec tar tvf $arcfile} tartmp

		grab release .tarfile
		destroy .tarfile

		if {[string match "tar: only read*" $tmp]} {
			errormsg "$arcfile ŸǤޤ"
		} else {
			set extworkdir [lindex $tartmp 7]
			if {[string match "*/" $extworkdir]} {} else {
				set extworkdir ""
			}
			showtar $arcfile
			if {[string compare $extdir "-"]} {
				set cdir [pwd]
				set dirname [string range $cdir [expr [string last "/" $cdir] +1] end]
#				set extworkdir [string range [file tail $arcfile] 0 [expr [string last "." [file tail $arcfile]] -1]]
				set extflag 0
				if {[file exists ${extdir}${extworkdir}]} {
					if {[string compare $extdir ""] && [string compare $extworkdir ""]} {
						if {[file isdirectory ${extdir}${extworkdir}]} {
							ynmsg "$extdir$extworkdir ˾񤭤ƥեŸޤǤ"
							if {$answer} {
								set extflag 1
							} else {
								errormsg "եŸߤޤ"
							}
						} else {
							errormsg "ե ${extdir}${extworkdir} ¸ߤΤŸǤޤ"
						}
					} else {
						ynmsg "${extdir}${extworkdir} ˾񤭤ƥեŸޤǤ"
						if {$answer} {
							set extflag 1
						} else {
							errormsg "եŸߤޤ"
						}
					}
				} else {
					if {[string compare $extdir ""]} {
						ynmsg "$extdir  $extworkdir Ȥǥ쥯ȥޤǤ"
					} else {
						ynmsg "$extworkdir Ȥǥ쥯ȥޤǤ"
					}
					if {$answer} {
						set extflag 1
					} else {
						errormsg "եŸߤޤ"
					}
				}
				if {[string compare $extdir ""]} {
					cd $extdir
				}
				if {$extflag} {
					if {[file writable [pwd]]} {
						toplevel .exarc
						wm geometry .exarc +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
						label .exarc.lb0 -text "[file tail $arcfile] ŸƤޤ"
						pack .exarc.lb0 -padx 50 -pady 50
						grab set .exarc
						wm resizable .exarc 0 0
						update

						exec tar xf $arcfile
						grab release .exarc
						destroy .exarc
						if {[string compare $extworkdir ""]} {
							cd $extworkdir
						}
						.workingdir configure -text [pwd]
						getlist
					} else {
						errormsg "Υǥ쥯ȥˤϽ񤭹ߤǤޤŸߤޤ"
					}
				}
			}
		}
	}
}

.system.m0 add command -label "ޥɥ" -command {
	catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] &} tmp
}
.system.m0 add command -label "λ" -command {
	if {$wflag} {} else {
		ynmsg "ɽ¸ޤ"
		if {$answer} {
			writephy
			set wflag 1
		}
	}
	catch {exec /bin/ps | egrep $xempid.*xemacs} tmp
	if {[string compare $xempid [lindex $tmp 0]]} {} else {
		exec kill -9 $xempid
	}
	set flag 1
	exit 0
}

menubutton .config -text "" -menu .config.m0 -width 10 -relief raised
menu .config.m0 -tearoff false
.config.m0 add command -label "ȥǥ쥯ȥ" -command {
	selectdir
}
.config.m0 add command -label "եΥԡ" -command {
	setstyle
}
.config.m0 add cascade -label "ץӥ塼⡼" -menu .config.m0.m1
menu .config.m0.m1 -tearoff false
.config.m0.m1 add radiobutton -label "EPSɽ" -var mode -value 1
.config.m0.m1 add radiobutton -label "EPSɽ" -var mode -value 0

menubutton .filter -text "ե륿" -menu .filter.m0 -width 10 -relief raised
menu .filter.m0 -tearoff false
.filter.m0 add command -label "ʸΰ" -command {
	exec touch ewordtmp.log
	foreach i [.l0 curselection] {
		if {[string compare $body($i) "index"]} {
			exec nkf -e $fname($i) | $bindir/crconv -u | $bindir/eword -k >> ewordtmp.log
		}
	}
	catch {exec sort -f ewordtmp.log | uniq} tmp
	exec /bin/rm -f ewordtmp.log

	toplevel .kword
	wm geometry .kword +[expr $rootwd / 2 - 150]+[expr $rootht /2 - 270]
	listbox .kword.l0 -height 30 -width 40 -yscrollcommand ".kword.s1 set"
	.kword.l0 delete 0 end
	foreach lin $tmp {
		.kword.l0 insert end $lin
	}
	scrollbar .kword.s1 -orient vertical -command ".kword.l0 yview"
	button .kword.ok -text "OK" -command {
		destroy .kword
	}
	
	grid .kword.l0 -row 0 -column 0 -sticky nsew
	grid .kword.s1 -row 0 -column 1 -sticky nsw
	grid .kword.ok -row 1 -column 0
}
.filter.m0 add command -label "ñΰ" -command {
	exec touch ewordtmp.log
	foreach i [.l0 curselection] {
		if {[string compare $body($i) "index"]} {
			exec nkf -e $fname($i) | $bindir/crconv -u | $bindir/eword -e >> ewordtmp.log
		}
	}
	catch {exec sort -f ewordtmp.log | uniq} tmp
	exec /bin/rm -f ewordtmp.log

	toplevel .eword
	wm geometry .eword +[expr $rootwd / 2 - 150]+[expr $rootht /2 - 270]
	listbox .eword.l0 -height 30 -width 40 -yscrollcommand ".eword.s1 set"
	.eword.l0 delete 0 end
	foreach lin $tmp {
		.eword.l0 insert end $lin
	}
	scrollbar .eword.s1 -orient vertical -command ".eword.l0 yview"
	button .eword.ok -text "OK" -command {
		destroy .eword
	}
	
	grid .eword.l0 -row 0 -column 0 -sticky nsew
	grid .eword.s1 -row 0 -column 1 -sticky nsw
	grid .eword.ok -row 1 -column 0
}

menubutton .help -text "إ" -menu .help.m0 -width 10 -relief raised
menu .help.m0 -tearoff false
.help.m0 add command -label "ƥ˥塼" -command {
	showhelp .setteihelp system.txt
}
.help.m0 add command -label "˥塼" -command {
	showhelp .setteihelp settei.txt
}
.help.m0 add command -label "ե륿˥塼" -command {
	showhelp .filterhelp filter.txt
}
.help.m0 add command -label "ʸϿ" -command {
	showhelp .texthelp text.txt
}
.help.m0 add command -label "ե" -command {
	showhelp .stylefilehelp stylefile.txt
}
.help.m0 add command -label "ڡ" -command {
	showhelp .pagehelp page.txt
}
.help.m0 add command -label "ɽϿ" -command {
	showhelp .zuhyouhelp zuhyou.txt
}
.help.m0 add command -label "Ͽ" -command {
	showhelp .indexhelp index.txt
}
.help.m0 add command -label "TeXλ" -command {
	showhelp .texhelp tex.txt
}
.help.m0 add command -label "ʸΥѥ" -command {
	showhelp .compilehelp compile.txt
}
.help.m0 add command -label "ܼ" -command {
	showhelp .contentshelp contents.txt
}
.help.m0 add command -label "ƥեκ" -command {
	showhelp .nyukouhelp nyukou.txt
}
.help.m0 add command -label "ƸΥڡؤ" -command {
	showhelp .pageexchhelp pageexch.txt
}
.help.m0 add command -label "ȥ֥н" -command {
	showhelp .troublehelp trouble.txt
}

button .idxed -text "ʸԽ" -width 18 -command {
	if {[file writable [pwd]]} {
		set idxlist ""
		foreach i [.l0 curselection] {
			if {[string compare $body($i) "index"]} {
				lappend idxlist $fname($i)
			}
		}
		if {[llength $idxlist]} {
			catch {exec /bin/ps | egrep $xempid.*xemacs} tmp
			if {$xempid == 0 || [string compare $xempid [lindex $tmp 0]]} {
				set xempid [exec xemacs -q -l /usr/share/ewb/3.3/xemacs/lisp/ewb-load.el -unmapped >& /dev/null &]
#				after 3000
			} else {
				if {[string match "Z*" [lindex $tmp 2]]} {
					exec kill -9 $xempid
					set xempid [exec xemacs -q -l /usr/share/ewb/3.3/xemacs/lisp/ewb-load.el -unmapped >& /dev/null &]
#					after 3000
				}
			}
			while {1} {
				catch {exec /bin/ps xwww | egrep gnuserv} tmp
				if {[string match "*xemacs*gnuserv*" $tmp]} {
					break
				}
			}

			exec $bindir/xewbindex-client $idxlist &
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .phyed -text "ɽԽ" -width 18 -command {
	if {[file writable [pwd]]} {
		set phylist ""
		foreach i [.l0 curselection] {
			if {[string compare $body($i) "index"]} {
				lappend phylist $fname($i)
			}
		}
		if {[llength $phylist]} {
			gettriger
			getfilelist
			set acount 0
			for {set i 0} {$i < $count} {incr i} {
				if {[string compare $body($i) "index"]} {
					getphylist $fname($i)
				}
			}
			getforallphy
			makephylist $phylist
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .pagepreview -text "ڡץӥ塼" -width 18 -command {
	if {[file writable [pwd]]} {
		set flag 1
		if {[llength [.l0 curselection]]} {
			if {[llength [.l0 curselection]] == 1} {
				if {[file exists "triger.def"] || [file exists "Styles/triger.def"]} {
					set i [.l0 curselection]
					if {[file exists $stylef($i)] || [file exists Styles/$stylef($i)]} {
						set pagenum ""
						toplevel .pagepre
						wm geometry .pagepre +[expr $rootwd / 2 - 100]+[expr $rootht /2 - 50]
						frame .pagepre.f0
						label .pagepre.lb0 -text "ڡֹꤷƤ"
						entry .pagepre.e0 -textvariable pagenum -width 8 
						button .pagepre.ok -text "OK" -command {
							set flag 0
							destroy .pagepre
						}
						button .pagepre.cancel -text "Cancel" -command {
							set flag 1
							set pagenum ""
							destroy .pagepre
						}

						bind .pagepre <Destroy> {
							if {$flag} {
								set pagenum ""
							}
						}

						focus .pagepre.e0
						pack .pagepre.ok .pagepre.cancel -in .pagepre.f0 -side left
						pack .pagepre.lb0 .pagepre.e0 .pagepre.f0 -side top
						wm resizable .pagepre 0 0
						tkwait window .pagepre
						
						if {[string compare $pagenum ""]} {
							if {[regexp {[0-9][0-9]*} $pagenum tmp]} {
								tounixeps
								if {[file exists Work/$body($i).LOG]} {
									exec /bin/rm -f Work/$body($i).LOG
								}
								if {$mode} {
									exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -a $pagenum -m $body($i).PRE
								} else {
									exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -a $pagenum -m $body($i).pre
								}
								if {[file exists Work/$body($i).LOG]} {
									errormsg [exec cat Work/$body($i).LOG]
								}
								getlist
							} else {
								errormsg "ڡֹϿͤǻꤷƤ"
							}
						}
					} else {
						errormsg "ʸ $fname($i) Υե$stylef($i)ˤޤ"
					}
				} else {
					errormsg "ȥꥬեtriger.defˤޤ"
				}
			} else {
				errormsg "ʣΥեǥڡΥץӥ塼ϤǤޤ"
			}
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .preview -text "ץӥ塼" -width 18 -command {
	if {[file writable [pwd]]} {
		if {[llength [.l0 curselection]]} {
			if {[file exists "triger.def"] || [file exists "Styles/triger.def"]} {
				tounixeps
				foreach i [.l0 curselection] {
					if {[file exists $stylef($i)] || [file exists Styles/$stylef($i)]} {
						if {[file exists Work/$body($i).LOG]} {
							exec /bin/rm -f Work/$body($i).LOG
						}
						if {$mode} {
							exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m $body($i).PRE
						} else {
							exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m $body($i).pre
						}
						if {[file exists Work/$body($i).LOG]} {
							errormsg [exec cat Work/$body($i).LOG]
						}
						getlist
					} else {
						errormsg "ʸ $fname($i) Υե$stylef($i)ˤޤ"
					}
				}
			} else {
				errormsg "ȥꥬեtriger.defˤޤ"
			}
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .compile -text "ѥΤ" -width 18 -command {
	if {[file writable [pwd]]} {
		if {[llength [.l0 curselection]]} {
			if {[file exists "triger.def"] || [file exists "Styles/triger.def"]} {
				tounixeps
				foreach i [.l0 curselection] {
					if {[file exists $stylef($i)] || [file exists Styles/$stylef($i)]} {
						if {[file exists Work/$body($i).LOG]} {
							exec /bin/rm -f Work/$body($i).LOG
						}
						if {$mode} {
							exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m $body($i).Dvi
						} else {
							exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m $body($i).dvi
						}
						if {[file exists Work/$body($i).LOG]} {
							errormsg [exec cat Work/$body($i).LOG]
						}
						getlist
					} else {
						errormsg "ʸ $fname($i) Υե$stylef($i)ˤޤ"
					}
				}
			} else {
				errormsg "ȥꥬեtriger.defˤޤ"
			}
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .print -text "" -width 18 -command {
	if {$prcount > 0} {
		if {[file writable [pwd]]} {
			if {[llength [.l0 curselection]]} {
				set all 1
				toplevel .pp
				wm geometry .pp +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
				frame .pp.f0
				frame .pp.f1
				frame .pp.f2
				frame .pp.f3
				label .pp.l0 -text "ץ:"
				menubutton .pp.printer -text $printer -menu .pp.printer.m0 -width 20 -relief raised
				menu .pp.printer.m0 -tearoff false
				for {set i 0} {$i < $prcount} {incr i} {
					.pp.printer.m0 add command -label $printers($i) -command "
						set printer $printers($i)
						.pp.printer configure -text $printers($i)
					"
				}
				label .pp.l1 -text "ѻ極:"
				menubutton .pp.papersize -text $papersize -menu .pp.papersize.m0 -width 14 -relief raised
				menu .pp.papersize.m0 -tearoff false
				.pp.papersize.m0 add command -label "B5" -command "
					set papersize B5
					.pp.papersize configure -text B5
				"
				.pp.papersize.m0 add command -label "A4" -command "
					set papersize A4
					.pp.papersize configure -text A4
				"
				.pp.papersize.m0 add command -label "B4" -command "
					set papersize B4
					.pp.papersize configure -text B4
				"
				.pp.papersize.m0 add command -label "A3" -command "
					set papersize A3
					.pp.papersize configure -text A3
				"
				checkbutton .pp.c1 -text "" -variable twopage
				label .pp.l2 -text "ڡϰϤϤƤ"
				label .pp.l3 -text ""
				label .pp.l4 -text "ϰ"
				entry .pp.e0 -textvariable bpage -width 5
				entry .pp.e1 -textvariable epage -width 5
				checkbutton .pp.c0 -text "" -variable all -command {
					focus .pp
				}
				button .pp.b0 -text "OK" -command {
					grab release .pp
					destroy .pp
					if {[file exists "triger.def"] || [file exists "Styles/triger.def"]} {
						tounixeps
						foreach i [.l0 curselection] {
							if {[file exists $stylef($i)] || [file exists Styles/$stylef($i)]} {
								if {[file exists Work/$body($i).LOG]} {
									exec /bin/rm -f Work/$body($i).LOG
								}
								if {$all} {
									exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e env EWBSHELF_PRINTERFORM=$papersize[expr $twopage?"W":""] $newbindir/shelf -a $printer -m $body($i).OUT
								} else {
									exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e env EWBSHELF_PRINTERFORM=$papersize[expr $twopage?"W":""] $newbindir/shelf -a $bpage-$epage $printer -m $body($i).OUT
								}
								if {[file exists Work/$body($i).LOG]} {
									errormsg [exec cat Work/$body($i).LOG]
								}
								getlist
							} else {
								errormsg "ʸ $fname(i) Υե$stylef($i)ˤޤ"
							}
						}
					} else {
						errormsg "ȥꥬեtriger.defˤޤ"
					}
				}
				button .pp.b1 -text "Cancel" -command {
					grab release .pp
					destroy .pp
				}
				bind .pp.e0 <ButtonPress> {
					set all 0
				}
				bind .pp.e1 <ButtonPress> {
					set all 0
				}
				pack .pp.l4 .pp.e0 .pp.l3 .pp.e1 -in .pp.f0 -side left
				pack .pp.b0 .pp.b1 -in .pp.f1 -side left
				pack .pp.l0 .pp.printer -in .pp.f2 -side left
				if {$newewb} {
					pack .pp.l1 .pp.papersize .pp.c1 -in .pp.f3 -side left
				} else {
					pack .pp.l1 .pp.papersize -in .pp.f3 -side left
				}
				pack .pp.f2 .pp.f3 .pp.l2 .pp.c0 .pp.f0 .pp.f1 -side top -padx 5 -pady 5
				grab set .pp
				wm resizable .pp 0 0
				tkwait window .pp
			}
		} else {
			errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
		}
	} else {
		errormsg "ץ󥿤ѤǤʤΤǰǤޤ"
	}
}

button .maketoc -text "ܼե" -width 18 -command {
	if {[file writable [pwd]]} {
		if {[file exists "triger.def"] || [file exists "Styles/triger.def"]} {
			if {[file exists ".shelf2"]} {
				set message ""
				set fmessage ""
				toplevel .gettoc
				wm geometry .gettoc +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
				label .gettoc.lb0 -text "ܼեƤޤ"
				pack .gettoc.lb0 -padx 50 -pady 50
				wm resizable .gettoc 0 0
				update

				for {set i 0} {$i<$count} {incr i} {
					if {[string compare $body($i) "contents"] && [string compare $body($i) "index"]} {
						if {[file exists $fname($i)]} {} else {
							append fmessage "$fname($i) "
						}
						if {[file exists "Work/$body($i).rog"]} {} else {
							append message "$fname($i) "
						}
					}
				}
				if {[string length $fmessage] > 0 || [string length $message] > 0} {
					destroy .gettoc
					if {[string length $fmessage] > 0} {
						errormsg "ʸե ${fmessage}Ĥޤ"
					}
					if {[string length $message] > 0} {
						errormsg "${message}ѥ뤵Ƥޤ󡣤򥳥ѥ뤷ƤܼƤ"
					}
				} else {
					catch {exec $bindir/gettoc} tmp
					if {[string length $tmp] > 0} {
						destroy .gettoc
						if {[string first "This is not EWB" $tmp] == 0} {
							errormsg "EWBΥС󤬰㤤ޤ"
						} else {
							if {[string first "Cannot find " $tmp] == 0} {
								errormsg "[string range $tmp 12 [expr [string length $tmp] - 2]] Ĥޤ"
							} else {
								errormsg $tmp
							}
						}
					} else {
						destroy .gettoc
						errormsg "ܼե contents.ewb ޤ\nǧԽơʸեץܥϿƤ"
					}
				}
			} else {
				errormsg "ʸϿƤʤΤܼǤޤ"
			}
		} else {
			errormsg "ȥꥬե뤬ߤĤʤΤܼǤޤ"
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .clean -text "̥եõ" -width 18 -command {
	if {[file writable [pwd]]} {
		toplevel .cln
		wm geometry .cln +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
		label .cln.lb0 -text "̥եõƤޤ"
		pack .cln.lb0 -padx 50 -pady 50
		grab set .cln
		wm resizable .cln 0 0
		update

		foreach i [.l0 curselection] {
			catch {exec $newbindir/shelf -m $body($i).cln} tmp
		}
		catch {exec $newbindir/shelf -m ALL.cln} tmp

		grab release .cln
		destroy .cln
		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .link -text "߻Ȥγ" -width 18 -command {
	if {[file writable [pwd]]} {
		ynmsg "٤ƤΥե򥳥ѥ뤷ʤޤǤ"
		if {$answer} {
			toplevel .execcln
			wm geometry .execcln +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
			label .execcln.lb0 -text "̥եõƤޤ"
			pack .execcln.lb0 -padx 50 -pady 50
			update
			catch {exec $newbindir/shelf -m cln} tmp
			destroy .execcln
			update

			catch {exec $newbindir/shelf -m ALL.cln} tmp
			catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m Dvi} tmp
			for {set i 0} {$i < $count} {incr i} {
				exec /bin/rm -f Targets/$body($i).Dvi
			}
			exec /bin/rm -f Targets/ALL.Dvi
			catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m Dvi} tmp
			for {set i 0} {$i < $count} {incr i} {
				exec /bin/rm -f Targets/$body($i).Dvi
			}
			exec /bin/rm -f Targets/ALL.Dvi
			catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m Dvi} tmp
		}

		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .upbunsho -text "" -padx 0 -pady 0 -command {
	if {[file writable [pwd]]} {
		if {[llength [.l0 curselection]] == 1} { 
			set selected [.l0 curselection]
			if {$selected !=0} {
				catch  {exec $newbindir/shelf move [expr $selected + 1] $selected} tmp
				getlist
				.l0 selection clear 0 end
				.l0 selection set [expr $selected -1]
			}
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .downbunsho -text "" -padx 0 -pady 0 -command {
	if {[file writable [pwd]]} {
		if {[llength [.l0 curselection]] == 1} { 
			set selected [.l0 curselection]
			if {$selected < [expr $count - 1]} {
				catch  {exec $newbindir/shelf move [expr $selected + 1] [expr $selected + 2]} tmp
				getlist
				.l0 selection clear 0 end
				.l0 selection set [expr $selected + 1]
			}
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

menubutton .bunsho -text "ʸե" -pady 5 -menu .bunsho.m0 -relief raised
menu .bunsho.m0 -tearoff false
.bunsho.m0 add command -label "ʸɲ" -command {
	if {[file writable [pwd]]} {
		selectdoc
		if {![string compare $tcl_version "8.0"]} {
			set kcheck [string compare [kanji code $addname] "ANY"]
		} else {
			set kcheck [string compare $addname [encoding convertto "utf-8" $addname]]
		}
		if {$kcheck} {
			errormsg "ʤΥե̾ϻѤǤޤ"
		} else {
			if {[string length $addname]} {
				if {[file exists "Work"] && [file exists "Targets"]} {} else {
					toplevel .mkdir
					wm geometry .mkdir +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
					frame .mkdir.f0
					label .mkdir.lb0 -wraplength 200 -text "[pwd]  Work ǥ쥯ȥ Targets ǥ쥯ȥޤǤ"
					button .mkdir.b0 -text "OK" -command {
						if {[file exists "Work"]} {} else {
							exec mkdir Work
						}
						if {[file exists "Targets"]} {} else {
							destroy .mkdir
							exec mkdir Targets
						}
						grab release .mkdir 
						destroy .mkdir
					}
					button .mkdir.b1 -text "Cancel" -command {
						grab release .mkdir 
						destroy .mkdir
					}
					pack .mkdir.b0 .mkdir.b1 -in .mkdir.f0 -side left
					pack .mkdir.lb0 .mkdir.f0 -side top
					grab set .mkdir 
					wm resizable .mkdir 0 0
					tkwait window .mkdir
				}
				if {[file exists "Work"] && [file exists "Targets"]} {
					foreach adname $addname {
						for {set i 0} {$i < $count} {incr i} {
							if {[string compare $fname($i) $adname]} {} else {
								errormsg "$adname ϤǤϿƤޤ"
								break
							}
							set dot [string last "." $adname]
							if {$dot == 0} {
								errormsg "ե̾ `.' ǻϤޤեϿǤޤ"
								break
							} else {
								if {$dot > 0} {
									set tmpname [string range $adname 0 [expr $dot - 1]]
								} else {
									set tmpname $adname
								}
							}
							if {[string compare $body($i) $tmpname]} {} else {
								errormsg "ե̾γĥҰʳʬƱե뤬ǤϿƤޤ\
		ե̾ѹƤ"
								break
							}
						}
						if {$i == $count} {
							catch {exec $newbindir/shelf add $adname} tmp
							getlist
	#						setbuttons
						}
					}
				} else {
					errormsg "Workǥ쥯ȥTargetsǥ쥯ȥ꤬ʤΤʸɲäǤޤ"
				}
			}
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

.bunsho.m0 add command -label "ʸ" -command {
	if {[file writable [pwd]]} {
		set delcount 0
		foreach i [.l0 curselection] {
			catch  {exec $newbindir/shelf $body($i).cln} tmp
			catch  {exec $newbindir/shelf del [expr $i + 1 - $delcount]} tmp
			incr delcount
		}
		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}
.bunsho.m0 add command -label "ʸ" -command {
	if {[file writable [pwd]]} {
		set addnum [.l0 curselection]
		if {[llength $addnum]} {
			set addnum [expr [lindex $addnum 0] + 1]
			selectdoc
			if {![string compare $tcl_version "8.0"]} {
				set kcheck [string compare [kanji code $addname] "ANY"]
			} else {
				set kcheck [string compare $addname [encoding convertto "utf-8" $addname]]
			}
			if {$kcheck} {
				errormsg "ʤΥե̾ϻѤǤޤ"
			} else {
				if {[file exists "Work"] && [file exists "Targets"]} {} else {
					ynmsg "[pwd]  Work ǥ쥯ȥ Targets ǥ쥯ȥޤǤ"
					if {$answer} {
						if {[file exists "Work"]} {} else {
							exec mkdir Work
						}
						if {[file exists "Targets"]} {} else {
							exec mkdir Targets
						}
					}
				}
				if {[string length $addname]} {
					if {[file exists "Work"] && [file exists "Targets"]} {
						foreach adname $addname {
							for {set i 0} {$i < $count} {incr i} {
								if {[string compare $fname($i) $adname]} {} else {
									errormsg "$adname ϤǤϿƤޤ"
									break
								}
								set dot [string last "." $adname]
								if {$dot == 0} {
									errormsg "ե̾ `.' ǻϤޤեϿǤޤ"
									break
								} else {
									if {$dot > 0} {
										set tmpname [string range $adname 0 [expr $dot - 1]]
									} else {
										set tmpname $adname
									}
								}
								if {[string compare $body($i) $tmpname]} {} else {
									errormsg "ե̾γĥҰʳʬƱե뤬ǤϿƤޤ\
			ե̾ѹƤ"
									break
								}
							}
							if {$i == $count} {
								catch {exec $newbindir/shelf $addnum ins $adname} tmp
								getlist
							}
						}
					} else {
						errormsg "Workǥ쥯ȥTargetsǥ쥯ȥ꤬ʤΤʸɲäǤޤ"
					}
				}
			}
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

.bunsho.m0 add command -label "ʸư" -command {
	if {[file writable [pwd]]} {
		if {$count} {
			set srcnum ""
			set distnum ""
			toplevel .mv
			wm geometry .mv +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
			frame .mv.f0
			frame .mv.f1
			label .mv.lb0 -text "ưʸֹȰưֹϤƤ"
			entry .mv.e0 -width 5 -textvariable srcnum
			entry .mv.e1 -width 5  -textvariable distnum
			label .mv.lb1 -text ""
			label .mv.lb2 -text ""
			button .mv.b0 -text "OK" -command {
				if {($count >= $srcnum) && ($count >= $distnum) && ($srcnum != $distnum)} {
					grab release .mv
					destroy .mv
					catch {exec $newbindir/shelf move $srcnum $distnum} tmp
					getlist
	#				setbuttons
				} else {
					errormsg "ʸֹ椬ޤ"
				}
			}
			button .mv.b1 -text "Cancel" -command {
				grab release .mv
				destroy .mv
			}
			pack .mv.e0 .mv.lb1 .mv.e1 .mv.lb2 -in .mv.f0 -side left
			pack .mv.b0 .mv.b1 -in .mv.f1 -side left
			pack .mv.lb0 .mv.f0 .mv.f1 -side top
			raise .mv
			grab set .mv
			wm resizable .mv 0 0
			tkwait window .mv
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}
.bunsho.m0 add command -label "Ͽ" -command {
	if {[file writable [pwd]]} {
		for {set i 0} {$i < $count} {incr i} {
			if {[string compare $body($i) "index"]} {} else  {
				break
			}
		}
		if {$i == $count} {
			catch {exec $newbindir/shelf add index} tmp
			getlist
		} else {
			errormsg "ϤǤϿƤޤ"
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .page -text "ڡ" -pady 4 -relief raised -command {
	if {[file writable [pwd]]} {
		if {[llength [.l0 curselection]] == 1} { 
			set i [.l0 curselection]
			toplevel .pi
			wm geometry .pi +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
			label .pi.l0 -text "ϥڡֹϤƤ"
			entry .pi.e0 -textvariable pagenum
			button .pi.b0 -text "ڡʤ椭" -command {
				catch  {exec $newbindir/shelf [expr $i + 1] page -} tmp
				grab release .pi
				destroy .pi
				getlist
			}
			button .pi.b1 -text "OK" -command {
				catch  {exec $newbindir/shelf [expr $i + 1] page $pagenum} tmp
				grab release .pi
				destroy .pi
				getlist
			}
			button .pi.b2 -text "Cancel" -command {
				grab release .pi
				destroy .pi
			}
			pack .pi.l0 .pi.e0 -side top
			pack .pi.b0 .pi.b1 .pi.b2 -side left
			grab set .pi
			wm resizable .pi 0 0
			tkwait window .pi
		} else {
			if {[llength [.l0 curselection]]} { 
				errormsg "ʣʸ˥ڡϤǤޤ"
			}
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .style -text "ե" -pady 4 -command {
	if {[file writable [pwd]]} {
		if {[file exists ".shelf2"]} {
			set list [.l0 curselection]
			selectstyle
			if {[string compare "" $filename]} {
				foreach i $list {
					catch  {exec $newbindir/shelf [expr $i + 1] style $filename} tmp
				}
				getlist
			}
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

menubutton .phy -text "ɽ" -pady 5 -menu .phy.m0 -relief raised
menu .phy.m0 -tearoff false
.phy.m0 add command -label "ON" -command {
	if {[file writable [pwd]]} {
		foreach i [.l0 curselection] {
			catch  {exec $newbindir/shelf [expr $i + 1] usephy} tmp
		}
		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}
.phy.m0 add command -label "OFF" -command {
	if {[file writable [pwd]]} {
		foreach i [.l0 curselection] {
			catch  {exec $newbindir/shelf [expr $i + 1] nophy} tmp
		}
		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

menubutton .index -text "" -pady 5 -menu .index.m0 -relief raised
menu .index.m0 -tearoff false
.index.m0 add command -label "ON" -command {
	if {[file writable [pwd]]} {
		foreach i [.l0 curselection] {
			catch  {exec $newbindir/shelf [expr $i + 1] useindex} tmp
		}
		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}
.index.m0 add command -label "OFF" -command {
	if {[file writable [pwd]]} {
		foreach i [.l0 curselection] {
			catch  {exec $newbindir/shelf [expr $i + 1] noindex} tmp
		}
		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

menubutton .tex -text "TeX" -pady 5 -menu .tex.m0 -relief raised
menu .tex.m0 -tearoff false
.tex.m0 add command -label "1" -command {
	if {[file writable [pwd]]} {
		foreach i [.l0 curselection] {
			catch  {exec $newbindir/shelf [expr $i + 1] tex1} tmp
		}
		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}
.tex.m0 add command -label "2" -command {
	if {[file writable [pwd]]} {
		foreach i [.l0 curselection] {
			catch  {exec $newbindir/shelf [expr $i + 1] tex2} tmp
		}
		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}
.tex.m0 add command -label "3" -command {
	if {[file writable [pwd]]} {
		foreach i [.l0 curselection] {
			catch  {exec $newbindir/shelf [expr $i + 1] tex3} tmp
		}
		getlist
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .all -text "ž夬ե" -width 18 -command {
	toplevel .allps
	wm geometry .allps +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 150]

	button .allps.mentuke -text "եPSե" -command {
		grab release .allps
		destroy .allps
		mentuke
	}
	label .allps.l0 -text "  եѤդPostScriptե"
	label .allps.l1 -text "  ޤ"
	button .allps.pdf1 -text "PDFPSե" -command {
		grab release .allps
		destroy .allps
		set pdfflag 1
		allpdf
	}
	label .allps.l2 -text "  PDFѤξղäڡPostScriptե"
	label .allps.l3 -text "  ޤ"
	button .allps.pdf2 -text "PDFPSե(̵)" -command {
		grab release .allps
		destroy .allps
		set pdfflag 0
		allpdf
	}
	label .allps.l4 -text "  PDFѤΥ󥯰ʳξղäPostScriptե"
	label .allps.l5 -text "  ޤڡǤޤ"
	button .allps.cancel -text "Cancel" -command {
		grab release .allps
		destroy .allps
	}
	pack .allps.mentuke .allps.l0 .allps.l1 .allps.pdf1 .allps.l2 .allps.l3 .allps.pdf2 .allps.l4 .allps.l5 -anchor w -padx 5 -pady 3
	pack .allps.cancel -padx 5 -pady 5
	grab set .allps
	wm resizable .allps 0 0
	tkwait window .allps
}

proc mentuke {} {
	global rootwd rootht
	global dir answer ori men musen hosei sep pmode title pagesize
	global bindir newbindir newewb

	if {[file writable [pwd]]} {
		if {[file exists ".shelf2"]} {
			set dir ""
			set ori "16"
			set men "4"
			set musen 0
			set hosei 0
			set sep "ޤ"
			set pmode "Хڡ"
			set title ""
			set pagesize "ȥܥ"

			if {[file exists "MENTUKEinfo"]} {
				set fd [open "MENTUKEinfo" r]
				while {[gets $fd buff] != -1} {
					if {[string first "#" $buff] != -1} {
						set buff1 [string range $buff 0 [string first "#" $buff]]
					} else {
						set buff1 $buff
					}
					set cc [string first "" $buff1]
					if {$cc != -1} {
						if {[string match "**" [string range $buff1 [expr $cc + 6] end]]} {
							set dir ""
						}
						continue
					}
					set cc [string first "ޤñ" $buff1]
					if {$cc != -1} {
						if {[string match "*8*" [string range $buff1 [expr $cc + 8] end]]} {
							set ori "8"
						}
						continue
					}
					set cc [string first "դñ" $buff1]
					if {$cc != -1} {
						if {[string match "*8*" [string range $buff1 [expr $cc + 10] end]]} {
							set men "8"
						}
						continue
					}
					set cc [string first "̵֤" $buff1]
					if {$cc != -1} {
						if {[string match "*ͭ*" [string range $buff1 [expr $cc + 8] end]]} {
							set musen 1
						}
						continue
					}
					set cc [string first "ޤ" $buff1]
					if {$cc != -1} {
						if {[string match "*ͭ*" [string range $buff1 [expr $cc + 12] end]]} {
							set hosei 1
						}
						continue
					}
					set cc [string first "եʬ" $buff1]
					if {$cc != -1} {
						if {[string match "**" [string range $buff1 [expr $cc + 12] end]]} {
							set sep ""
						}
						continue
					}
					set cc [string first "ڡ⡼" $buff1]
					if {$cc != -1} {
						if {[string match "*Υ֥*" [string range $buff1 [expr $cc + 12] end]]} {
							set pmode "Υ֥"
						}
						continue
					}
					set cc [string first "ȥ" $buff1]
					if {$cc != -1} {
						regexp {\"(.*)\"} [string range $buff1 [expr $cc + 8] end] tmp title
					}
					set cc [string first "դ" $buff1]
					if {[expr ($cc != -1)]} {
						set pagesize [lindex [split $buff1 "\t"] 1]
						if {![string compare $pagesize ""]} {
							set pagesize "ȥܥ"
						}
					}
				}
				close $fd
			}

			toplevel .men
			wm geometry .men +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 200]
			frame .men.f0
			frame .men.f1
			frame .men.f2
			frame .men.f3
			frame .men.f4
			frame .men.f5
			frame .men.f6
			frame .men.f7
			frame .men.f8
			frame .men.f9
			frame .men.f10
			label .men.lb0 -text "դ"
			label .men.lb1 -text ""
			radiobutton .men.rb0 -text "" -variable dir -value ""
			radiobutton .men.rb1 -text "" -variable dir -value ""
			label .men.lb2 -text "ޤñ"
			radiobutton .men.rb2 -text "8" -variable ori -value "8" -command {
				if {[string compare $men "8"]} {} else {
					set men "4"
				}
			}	
			radiobutton .men.rb3 -text "16" -variable ori -value "16"
			label .men.lb3 -text "դñ"
			radiobutton .men.rb4 -text "4" -variable men -value "4"
			radiobutton .men.rb5 -text "8" -variable men -value "8" -command {
				if {[string compare $ori "8"]} {} else {
					set ori "16"
				}
			}
			checkbutton .men.cb0 -text "̵֤" -variable musen
			checkbutton .men.cb1 -text "ޤ" -variable hosei
			label .men.lb4 -text "եʬ"
			radiobutton .men.rb6 -text "ޤ" -variable sep -value "ޤ"
			radiobutton .men.rb7 -text "" -variable sep -value ""
			label .men.lb5 -text "ڡ⡼"
			radiobutton .men.rb8 -text "Хڡ" -variable pmode -value "Хڡ"
			radiobutton .men.rb9 -text "Υ֥" -variable pmode -value "Υ֥"
			label .men.lb6 -text "ȥ"
			entry .men.e0 -textvariable title -width 40
			label .men.lb7 -text "դ"
			menubutton .men.pagesize -text $pagesize -menu .men.pagesize.m0 -width 14 -relief raised
			menu .men.pagesize.m0 -tearoff false
			.men.pagesize.m0 add command -label "ȥܥ" -command {
				set pagesize "ȥܥ"
				.men.pagesize configure -text ȥܥ
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			.men.pagesize.m0 add command -label "̵" -command {
				set pagesize "̵"
				.men.pagesize configure -text "̵"
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			.men.pagesize.m0 add command -label "x(mm)" -command {
				set pagesize ""
				.men.pagesize configure -text "x(mm)"
				pack .men.lb7 .men.pagesize .men.pagesize2 -in .men.f7 -side left
			}
			.men.pagesize.m0 add command -label "A4" -command {
				set pagesize A4
				.men.pagesize configure -text A4
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			.men.pagesize.m0 add command -label "B4" -command {
				set pagesize B4
				.men.pagesize configure -text B4
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			.men.pagesize.m0 add command -label "A3" -command {
				set pagesize A3
				.men.pagesize configure -text A3
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			.men.pagesize.m0 add command -label "B3" -command {
				set pagesize B3
				.men.pagesize configure -text B3
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			.men.pagesize.m0 add command -label "A2" -command {
				set pagesize A2
				.men.pagesize configure -text A2
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			.men.pagesize.m0 add command -label "B2" -command {
				set pagesize B2
				.men.pagesize configure -text B2
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			.men.pagesize.m0 add command -label "A1" -command {
				set pagesize A1
				.men.pagesize configure -text A1
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			.men.pagesize.m0 add command -label "B1" -command {
				set pagesize B1
				.men.pagesize configure -text B1
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			entry .men.pagesize2 -textvariable pagesize -width 15
			button .men.b0 -text "Τ" -command {
				if {[string length $title]==0} {
					errormsg "ȥϤƤ"
				} else {
					set fd [open "MENTUKEinfo" w]
					puts $fd "ȥ\t\"$title\""
					puts $fd "\t$dir\t# /"
					puts $fd "ޤñ\t$ori\t# 16/8"
					puts $fd "դñ\t$men\t# 8/4"
					puts $fd "ޤֹ\t1"
					if {$musen} {
						puts $fd "̵֤\tͭ\t# ͭ/̵"
					} else {
						puts $fd "̵֤\t̵\t# ͭ/̵"
					}
					if {$hosei} {
						puts $fd "ޤ\tͭ\t# ͭ/̵"
					} else {
						puts $fd "ޤ\t̵\t# ͭ/̵"
					}
					puts $fd "եʬ\t$sep\t# ޤ/"
					puts $fd "ڡ⡼\t$pmode\t# Хڡ/Υ֥"
					if {[string length $pagesize] > 0} {
						puts $fd "դ\t$pagesize\t# ̵/A4/B4/A3/B3/A2/B2/A1/B1/ȥܥ/x(mm)"
					}
					close $fd
					grab release .men
					destroy .men
				}
			}
			button .men.b1 -text "դ¹" -command {
				if {[string length $title]==0} {
					errormsg "ȥ뤬̵դǤޤ"
				} else {
					set fd [open "MENTUKEinfo" w]
					puts $fd "ȥ\t\"$title\""
					puts $fd "\t$dir\t# /"
					puts $fd "ޤñ\t$ori\t# 16/8"
					puts $fd "դñ\t$men\t# 8/4"
					puts $fd "ޤֹ\t1"
					if {$musen} {
						puts $fd "̵֤\tͭ\t# ͭ/̵"
					} else {
						puts $fd "̵֤\t̵\t# ͭ/̵"
					}
					if {$hosei} {
						puts $fd "ޤ\tͭ\t# ͭ/̵"
					} else {
						puts $fd "ޤ\t̵\t# ͭ/̵"
					}
					puts $fd "եʬ\t$sep\t# ޤ/"
					puts $fd "ڡ⡼\t$pmode\t# Хڡ/Υ֥"
					if {[string length $pagesize] > 0} {
						puts $fd "դ\t$pagesize\t# ̵/A4/B4/A3/B3/A2/B2/A1/B1/ȥܥ/x(mm)"
					}
					close $fd
					grab release .men
					destroy .men

					ynmsg "ޤǤη̥եõޤǤ"
					if {$answer} {
						toplevel .execcln
						wm geometry .execcln +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
						label .execcln.lb0 -text "̥եõƤޤ"
						pack .execcln.lb0 -padx 50 -pady 50
						update
						catch {exec $newbindir/shelf -m cln} tmp
						destroy .execcln
						update
					}

					catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m ALL.DVI} tmp
					for {set i 0} {$i < $count} {incr i} {
						exec /bin/rm -f Targets/$body($i).DVI
					}
					exec /bin/rm -f Targets/ALL.DVI
					catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m ALL.DVI} tmp
					for {set i 0} {$i < $count} {incr i} {
						exec /bin/rm -f Targets/$body($i).DVI
					}
					exec /bin/rm -f Targets/ALL.DVI
					catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m ALL.DVI} tmp
					catch {exec $bindir/alldvicheck Targets/ALL.DVI} tmp
					if {[string compare $tmp ""]} {
						ynmsg "$tmp\n³ޤ"
					} else {
						set answer 1
					}
					if {$answer} {
						exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m ALL.PS

						toplevel .fontscan
						wm geometry .fontscan +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
						label .fontscan.lb0 -text "ѥեȤĴ٤Ƥޤ"
						pack .fontscan.lb0 -padx 50 -pady 50
						update

						set allps ""
						exec /bin/rm -f tmpFONTS.lst FONTS.lst
						for {set i 1} {1} {incr i} {
							if {$i < 10} {
								set tmpnum [format "00%d" $i]
							} else { 
								if {$i < 100} {
									set tmpnum [format "0%d" $i]
								} else {
									set tmpnum [format "%d" $i]
								}
							}

							if {[file exists "Targets/ALL.PS.$tmpnum"]} {
								set allps "Targets/ALL.PS.$tmpnum"
								catch {exec $bindir/fontscan $allps >> tmpFONTS.lst} tmp
							} else {
								break
							}
						}
						exec sort tmpFONTS.lst | sed -e "s/-83pv-RKSJ-H\$//" | sed -e "s/-83pv-RKSJ-V\$//" | sed -e "s/-RKSJ-H\$//" | sed -e "s/-RKSJ-V\$//" | sed -e "s/-H\$//" | sed -e "s/-V\$//" | uniq > FONTS.lst
						catch {exec cat FONTS.lst} tmp
						exec /bin/rm -f tmpFONTS.lst

						destroy .fontscan
						update

						errormsg "ʲΥեȤѤƤޤFONTS.lst ˵Ͽޤ\n$tmp"
						getlist
					}
				}
			}
			button .men.b2 -text "Ʀܰ" -command {
				if {$prcount > 0} {
					if {[string length $title]==0} {
						errormsg "ȥ뤬̵դǤޤ"
					} else {
						set all 1
						set mamenum1 ""
						set mamenum2 ""
						toplevel .mamehon
						wm geometry .mamehon +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
						frame .mamehon.f0
						frame .mamehon.f1
						frame .mamehon.f2
						frame .mamehon.f3
						label .mamehon.l0 -text "ץ:"
						menubutton .mamehon.printer -text $printer -menu .mamehon.printer.m0 -width 20 -relief raised
						menu .mamehon.printer.m0 -tearoff false
						for {set i 0} {$i < $prcount} {incr i} {
							.mamehon.printer.m0 add command -label $printers($i) -command "
								set printer $printers($i)
								.mamehon.printer configure -text $printers($i)
							"
						}
						label .mamehon.l1 -text "ѻ極:"
						menubutton .mamehon.mamehonsize -text $mamehonsize -menu .mamehon.mamehonsize.m0 -width 20 -relief raised
						menu .mamehon.mamehonsize.m0 -tearoff false
						.mamehon.mamehonsize.m0 add command -label "A4" -command "
							set mamehonsize A4
							.mamehon.mamehonsize configure -text A4
						"
						.mamehon.mamehonsize.m0 add command -label "B4" -command "
							set mamehonsize B4
							.mamehon.mamehonsize configure -text B4
						"
						.mamehon.mamehonsize.m0 add command -label "A3" -command "
							set mamehonsize A3
							.mamehon.mamehonsize configure -text A3
						"
						label .mamehon.l2 -text "ֹϰϤϤƤ"
						label .mamehon.l3 -text ""
						label .mamehon.l4 -text "ϰ"
						entry .mamehon.e0 -textvariable mamenum1 -width 5
						entry .mamehon.e1 -textvariable mamenum2 -width 5
						checkbutton .mamehon.c0 -text "" -variable all -command {
							focus .mamehon
						}
						button .mamehon.b0 -text "OK" -command {
							grab release .mamehon
							destroy .mamehon
							grab release .men
							destroy .men
							if {[file exists Targets/ALL.PS] && [file exists MENTUKEinfo]} {
								if {[file mtime Targets/ALL.PS] > [file mtime MENTUKEinfo]} {
									if {$all} {
										catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e env MAMEHONSIZE=$mamehonsize $newbindir/shelf -a $printer -m ALL.out} tmp
									} else {
										if {[string compare $sep "ޤ"]} {
											if {[file exists "MENTUKE.LOG"]} {
												set fd [open "MENTUKE.LOG" r]
												if {[string length $mamenum1] > 0} {
													while {[gets $fd tmp] != -1} {
														if {[string first " : ${mamenum1}ޤ" $tmp] != -1} {
															set num1 [string range $tmp 0 [expr [string first " : " $tmp] - 1]]
															break
														}
													}
												} else {
													set num1 1
												}
												if {[string length $mamenum2] > 0} {
													while {[gets $fd tmp] != -1} {
														if {[string first " : ${mamenum2}ޤ" $tmp] != -1} {
															set num2 [string range $tmp 0 [expr [string first " : " $tmp] - 1]]
														}
													}
												} else {
													set num2 ""
												}
												close $fd
											} else {
												set num1 [expr ($ori/$men)*($mamenum1-1)+1]
												set num2 [expr ($ori/$men)*($mamenum2)]
											}
										} else {
											set num1 $mamenum1
											set num2 $mamenum2
										}
										catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e env MAMEHONSIZE=$mamehonsize $newbindir/shelf -a $num1-$num2 $printer -m ALL.out} tmp
									}
									exec touch Targets/ALL.PS
								} else {
									errormsg "դ¹ԤƤƦܰƤ"
								}
							} else {
								errormsg "դ¹ԤƤƦܰƤ"
							}
						}
						button .mamehon.b1 -text "Cancel" -command {
							grab release .mamehon
							destroy .mamehon
						}
						pack .mamehon.l0 .mamehon.printer -in .mamehon.f2 -side left
						pack .mamehon.l1 .mamehon.mamehonsize -in .mamehon.f3 -side left
						pack .mamehon.l4 .mamehon.e0 .mamehon.l3 .mamehon.e1 -in .mamehon.f0 -side left
						pack .mamehon.b0 .mamehon.b1 -in .mamehon.f1 -side left
						pack .mamehon.f2 .mamehon.f3 .mamehon.l2 .mamehon.c0 .mamehon.f0 .mamehon.f1 -side top -padx 5 -pady 5

						bind .mamehon.e0 <ButtonPress> {
							set all 0
						}
						bind .mamehon.e1 <ButtonPress> {
							set all 0
						}
						grab set .mamehon
						wm resizable .mamehon 0 0
						tkwait window .mamehon
					}
				} else {
					errormsg "ץ󥿤ѤǤʤΤǰǤޤ"
				}
			}
			button .men.b3 -text "Cancel" -command {
				grab release .men
				destroy .men
			}
			pack .men.lb1 .men.rb0 .men.rb1 -in .men.f0 -side left
			pack .men.lb2 .men.rb2 .men.rb3 -in .men.f1 -side left
			pack .men.lb3 .men.rb4 .men.rb5 -in .men.f2 -side left
			pack .men.lb4 .men.rb6 .men.rb7 -in .men.f3 -side left
			pack .men.lb5 .men.rb8 .men.rb9 -in .men.f4 -side left
			pack .men.lb6 .men.e0 -in .men.f5 -side left
			pack .men.lb7 .men.pagesize .men.pagesize2 -in .men.f7 -side left
			if {![string match "*x*" $pagesize]} {
				place .men.pagesize2 -x 1000 -y 1000 -in .men
			}
			pack .men.b0 .men.b1 .men.b2 .men.b3 -in .men.f6 -side left
			pack .men.lb0 -side top
			pack .men.f7 -in .men.f9 -side top -anchor w
			pack .men.lb7 .men.f9 -in .men.f10 -side left -anchor n
			pack .men.f5 .men.f0 .men.f1 .men.f2 .men.cb0 .men.cb1 .men.f3 .men.f4 .men.f7 .men.f10 -side top -anchor w -padx 5 -pady 3
			pack .men.f6 -side top -padx 5 -pady 5
			grab set .men
			wm resizable .men 0 0
			tkwait window .men
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

button .pageexch -text "ڡڤФ" -width 18 -command {
	global rootwd rootht

	if {[file writable [pwd]]} {
		if {[llength [.l0 curselection]]} {
			errormsg ":νϥڡꤷƳǧ˹ԤʤäƤ"

			toplevel .pex
			wm geometry .pex +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
			frame .pex.f0
			frame .pex.f1
			label .pex.l0 -text "ؤڡϤƤ"
			entry .pex.e0 -textvariable pages -width 32
			button .pex.b0 -text "OK" -command {
				grab release .pex
				destroy .pex
				if {[file exists "triger.def"] || [file exists "Styles/triger.def"]} {
					tounixeps
					foreach i [.l0 curselection] {
						if {[file exists $stylef($i)] || [file exists Styles/$stylef($i)]} {
							if {[file exists Work/$body($i).LOG]} {
								exec /bin/rm -f Work/$body($i).LOG
							}
							if {[file exists Targets/$body($i).PS]} {
								exec /bin/rm -f Targets/$body($i).PS
							}
							exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -a $pages -m $body($i).PS
							if {[file exists Work/$body($i).LOG]} {
								errormsg [exec cat Work/$body($i).LOG]
							} else {
								errormsg "Targets ǥ쥯ȥ $body($i).PS.cut Ȥ̾ǥեޤ"
							}
							getlist
						} else {
							errormsg "ʸ $fname(i) Υե$stylef($i)ˤޤ"
						}
					}
				} else {
					errormsg "ȥꥬեtriger.defˤޤ"
				}
			}
			button .pex.b1 -text "Cancel" -command {
				grab release .pex
				destroy .pex
			}
			pack .pex.b0 .pex.b1 -in .pex.f1 -side left
			pack .pex.l0 .pex.e0 .pex.f1 -side top
			grab set .pex
			wm resizable .pex 0 0

		} else {
			errormsg "ʸ򤷤Ƥ"
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

proc allpdf {} {
	global rootwd rootht
	global pdfpages author title subject pagemode
	global pdfflag pdf dvi
	global bindir newbindir newewb

	if {$pdfflag} {
		if {$newewb} {
			set dvi "DVi"
		} else {
			set dvi "DVI"
		}
		set pdf "PDF"
	} else {
		if {$newewb} {
			set dvi "Dvi"
		} else {
			set dvi "DVI"
		}
		set pdf "pdf"
	}

	if {[file writable [pwd]]} {
		if {[file exists ".shelf2"]} {
			if {[file exists "PDFstyle"] || [file exists "Styles/PDFstyle"] || !$newewb || !$pdfflag} {
				toplevel .pdfpage
				if {$pdfflag} {
					wm geometry .pdfpage +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
				} else {
					wm geometry .pdfpage +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 250]
				}
				frame .pdfpage.f0
				frame .pdfpage.f1
				frame .pdfpage.f2

				set pdfpages ""
				set author ""
				set title ""
				set subject ""
				set pagemode "1"

				if {[file exists "PDFinfo"] || [file exists "Styles/PDFinfo"]} {
					if {[file exists "PDFinfo"]} {
						set fd [open "PDFinfo" r]
					} else {
						set fd [open "Styles/PDFinfo" r]
					}
					while {[gets $fd buff] != -1} {
						if {[string first "#" $buff] != -1} {
							set buff1 [string range $buff 0 [string first "#" $buff]]
						} else {
							set buff1 $buff
						}
						set cc [string first "author" $buff1]
						if {$cc != -1} {
							regexp {\"(.*)\"} [string range $buff1 [expr $cc + 6] end] tmp author
						}
						set cc [string first "title" $buff1]
						if {$cc != -1} {
							regexp {\"(.*)\"} [string range $buff1 [expr $cc + 5] end] tmp title
						}
						set cc [string first "subject" $buff1]
						if {$cc != -1} {
							regexp {\"(.*)\"} [string range $buff1 [expr $cc + 7] end] tmp subject
						}
						set cc [string first "pagemode" $buff1]
						if {$cc != -1} {
							if {[string match "*1*" [string range $buff1 [expr $cc + 8] end]]} {
								set pagemode 1
							} else {
								if {[string match "*2*" [string range $buff1 [expr $cc + 8] end]]} {
									set pagemode 2
								} else {
									if {[string match "*3*" [string range $buff1 [expr $cc + 8] end]]} {
										set pagemode 3
									} else {
										if {[string match "*4*" [string range $buff1 [expr $cc + 8] end]]} {
											set pagemode 4
										} else {
											set pagemode 1
										}
									}
								}
							}
						}
					}
					close $fd
				}
				label .pdfpage.lb0 -text ""
				entry .pdfpage.e0 -textvariable author -width 40
				label .pdfpage.lb1 -text "ȥ"
				entry .pdfpage.e1 -textvariable title -width 40
				label .pdfpage.lb2 -text "֥ȥ"
				entry .pdfpage.e2 -textvariable subject -width 40
				radiobutton .pdfpage.rb0 -text "/͡ɽʤ" -variable pagemode -value "1"
				radiobutton .pdfpage.rb1 -text "ɽ" -variable pagemode -value "2"
				radiobutton .pdfpage.rb2 -text "͡ɽ" -variable pagemode -value "3"
				radiobutton .pdfpage.rb3 -text "ե륹꡼⡼ɤɽ" -variable pagemode -value "4"
				button .pdfpage.b0 -text "Τ" -command {
					set fd [open "PDFinfo" w]
					puts $fd "<general>"
					puts $fd "author = \"$author\""
					puts $fd "title = \"$title\""
					puts $fd "subject = \"$subject\""
					puts $fd "pagemode = $pagemode"
					puts $fd "</general>"
					close $fd
					grab release .pdfpage
					destroy .pdfpage
				}

				if {!$pdfflag} {
					label .pdfpage.l0 -text "ڡֹϤƤ"
					label .pdfpage.l1 -text "ڡֹƬΥڡǤΤ"
					label .pdfpage.l2 -text "  Υֹ֥ȤϰפʤȤޤ"
					label .pdfpage.l3 -text "ڡֹϥڡǶڤäϤޤ"
					label .pdfpage.l4 -text "ϰϻ'-'ǤĤʤƵҤޤ"
					label .pdfpage.l5 -text "꤬ʤڡPSեޤ"
					label .pdfpage.l6 -text ": 15ڡ8ڡ10ڡ򤹤"
					label .pdfpage.l7 -text "    1-5 8 10"
					entry .pdfpage.e4 -textvariable pdfpages -width 50
				} else {
					checkbutton .pdfpage.cb0 -text "եľ" -variable sioriflag -onvalue 1 -offvalue 0
				}

				button .pdfpage.b1 -text "PSե" -command {
					if {[file exists "PDFinfo"]} {
						set fd [open "PDFinfo" w]
					} else {
						if {[file exists "Styles/PDFinfo"]} {
							set fd [open "Styles/PDFinfo" w]
						} else {
							set fd [open "PDFinfo" w]
						}
					}
					puts $fd "<general>"
					puts $fd "author = \"$author\""
					puts $fd "title = \"$title\""
					puts $fd "subject = \"$subject\""
					puts $fd "pagemode = $pagemode"
					puts $fd "</general>"
					close $fd

					grab release .pdfpage
					destroy .pdfpage

					toplevel .execcln
					wm geometry .execcln +[expr $rootwd / 2 - 200]+[expr $rootht/2 - 100]
					label .execcln.lb0 -text "̥եõƤޤ"
					pack .execcln.lb0 -padx 50 -pady 50
					update
					catch {exec $newbindir/shelf -m cln} tmp
					destroy .execcln
					update

					
					if {!$pdfflag} {
						if {$newewb} {
							exec /bin/rm -f Targets/pdf.ps
						} else {
							exec /bin/rm -f Targets/samplepdf.ps
						}
					} else {
						exec /bin/rm -f Targets/PDF.PS
					}
					catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m ALL.$dvi} tmp
					for {set i 0} {$i < $count} {incr i} {
						exec /bin/rm -f Targets/$body($i).$dvi
					}
					exec /bin/rm -f Targets/ALL.$dvi
					catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m ALL.$dvi} tmp
					for {set i 0} {$i < $count} {incr i} {
						exec /bin/rm -f Targets/$body($i).$dvi
					}
					exec /bin/rm -f Targets/ALL.$dvi
					catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m ALL.$dvi} tmp
					catch {exec $bindir/alldvicheck Targets/ALL.$dvi} tmp
					if {[string compare $tmp ""]} {
						ynmsg "$tmp\n³ޤ"
					} else {
						set answer 1
					}
					if {$answer} {
						if {$newewb} {
							catch {exec $newbindir/mksiori} tmp
						}
						if {[string compare $pdfpages ""]} {
							regsub -all " " $pdfpages " =" pdfpages
							catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -a $pdfpages -m ALL.$pdf} tmp
						} else {
							catch {exec kterm -km euc -geometry +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 250] -e $newbindir/shelf -m ALL.$pdf} tmp
						}
						if {!$pdfflag} {
							if {$newewb} {
								if {[file exists "Targets/pdf.ps"]} {
									errormsg "Targets ǥ쥯ȥ pdf.ps ޤ"
								} else {
									errormsg "PSեκ˼Ԥޤ\nե뤬˥ѥǤ뤫ǧƤ"
								}
							} else {
								if {[file exists "Targets/samplepdf.ps"]} {
									errormsg "Targets ǥ쥯ȥ samplepdf.ps ޤ"
								} else {
									errormsg "PSեκ˼Ԥޤ\nե뤬˥ѥǤ뤫ǧƤ"
								}
							}
						} else {
							if {[file exists "Targets/PDF.PS"]} {
								errormsg "Targets ǥ쥯ȥ PDF.PS ޤ"
							} else {
								errormsg "PSեκ˼Ԥޤ\nե뤬˥ѥǤ뤫ǧƤ"
							}
						}
					}
				}
				button .pdfpage.b2 -text "Cancel" -command {
					grab release .pdfpage
					destroy .pdfpage
				}
				pack .pdfpage.lb0 .pdfpage.e0 .pdfpage.lb1 .pdfpage.e1 .pdfpage.lb2 .pdfpage.e2 .pdfpage.rb0 .pdfpage.rb1 .pdfpage.rb2 .pdfpage.rb3 -in .pdfpage.f0 -anchor w
				if {!$pdfflag} {
					pack .pdfpage.l0 .pdfpage.l1 .pdfpage.l2 .pdfpage.l3 .pdfpage.l4 .pdfpage.l5 .pdfpage.l6 .pdfpage.l7 .pdfpage.e4 -in .pdfpage.f1 -anchor w
				} else {
					pack .pdfpage.cb0 -in .pdfpage.f1 -anchor w
				}
				pack .pdfpage.b0 .pdfpage.b1 .pdfpage.b2 -in .pdfpage.f2 -side left -padx 5
				pack .pdfpage.f0 .pdfpage.f1 .pdfpage.f2 -padx 5 -pady 5 -anchor w
				grab set .pdfpage
				wm resizable .pdfpage 0 0
			} else {
				errormsg "PDFstyleե뤬ʤΤPDFѴPostScriptեǤޤ\nեԤ̤Ƥ"
			}
		} else {
			errormsg "ʸϿƤޤ"
		}
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

bind . <Control-Key-a> {
	.l0 selection set 0 end
}

bind . <Destroy> {
	if {$wflag} {} else {
		ynmsg "ɽ¸ޤ"
		if {$answer} {
			writephy
			set wflag 1
		}
	}
	catch {exec /bin/ps | egrep $xempid.*xemacs} tmp
	if {[string compare $xempid [lindex $tmp 0]]} {} else {
		exec kill -9 $xempid
	}
	exit 0
}
#bind .l0 <ButtonRelease> {
#	if {[llength [.l0 curselection]] == 1} { 
#		setbuttons
#	} else {
#		unsetbuttons
#	}
#}

pack .system .config .filter .help -in .f0 -side left
#pack .system .config -in .f0 -side left
.menuset configure -width 400 -height 70 
place .working -x 0 -y 5 -in .menuset
place .workingdir -x 145 -y 0 -in .menuset
place .upbunsho -x 0 -y 30 -in .menuset
place .downbunsho -x 0 -y 50 -in .menuset
place .bunsho -x 30 -y 38 -in .menuset
place .style -x 143 -y 37 -in .menuset
place .page -x 329 -y 37 -in .menuset
place .phy -x 414 -y 38 -in .menuset
place .index -x 457 -y 38 -in .menuset
place .tex -x 500 -y 38 -in .menuset
grid .l0 -in .f1 -row 2 -column 0 -sticky nsew
grid .s1 -in .f1 -row 2 -column 1 -sticky nsw
grid columnconfigure .f1 0 -weight 100
grid rowconfigure .f1 0 -weight 100
.f3 configure -width 160 -height 430 
place .idxed -x 5 -y 0 -in .f3
place .phyed -x 5 -y 35 -in .f3
place .compile -x 5 -y 70 -in .f3
place .preview -x 5 -y 105 -in .f3
place .pagepreview -x 5 -y 140 -in .f3
place .print -x 5 -y 175 -in .f3
place .maketoc -x 5 -y 230 -in .f3
place .link -x 5 -y 265 -in .f3
place .clean -x 5 -y 300 -in .f3
place .all -x 5 -y 365 -in .f3
place .pageexch -x 5 -y 400 -in .f3
pack .f1 .f3 -in .f7 -side left
pack .f0 .menuset .f7 -side top -fill x

# .shelf2Ƥꥹɽ
proc getlist {} {
	global tcl_version
	global body fname page lpage stylef opt count
	global bindir newbindir oldbindir newewb

	set cline [.l0 curselection]
	if {[string compare $cline ""]} {} else {set cline 0}
	.l0 delete 0 end
	set count 0
	if {[file exists ".shelf2"]} {
		set fd [open ".shelf2" r]
		while {[gets $fd line] >= 0} {
			if {[string compare $line "ewb3.1"] && [string compare $line "ewb3.2"] && [string compare $line "ewb3.3"]} {
				scan $line "%s %s %s %s %s" \
					body($count) fname($count) page($count) stylef($count) opt($count)
				if {[string compare $body($count) "index"]} {} else {
					set fname($count) ""
				}
				if {[string compare $stylef($count) "-"]} {} else {
					set stylef($count) "ewb2latex.cls"
				}
				if {[string compare $page($count) "-"]} {} else {
					if {[file exists Work/$body($count).fpg]} {
						set page($count) *[exec cat Work/$body($count).fpg]
					} else {
						if {$count} {
							if {[file exists Work/$body([expr $count - 1]).lpg]} {
								set page($count) ?[expr [exec cat Work/$body([expr $count - 1]).lpg] +1]
							} else {
								set page($count) "?0"
							}
						} else {
							set page($count) "?0"
						}
					}
				}
				if {[file exists Work/$body($count).lpg]} {
					set lpage($count) *[exec cat Work/$body($count).lpg]
				} else {
					set lpage($count) "?"
				}
				if {[string match "p*" $opt($count)]} {
					set phy "on"
				} else {
					set phy "off"
				}
				if {[string match "*i*" $opt($count)]} {
					set index "on"
				} else {
					set index "off"
				}
				set tex [string trimleft $opt($count) pi]
				if {![string compare $fname($count) ""] && [string compare $tcl_version "8.0"]} {
						set lin [format "%3d             %-25s %5s - %-5s  %-3s   %-3s     %d" \
						[expr $count + 1] $stylef($count) \
						$page($count) $lpage($count) $phy $index $tex]
				} else {
					set lin [format "%3d %-15s %-25s %5s - %-5s  %-3s   %-3s     %d" \
						[expr $count + 1] $fname($count) $stylef($count) \
						$page($count) $lpage($count) $phy $index $tex]
				}
				.l0 insert end $lin
				incr count
			} else {
				if {![string compare $line "ewb3.2"] || ![string compare $line "ewb3.3"]} {
					set bindir $newbindir
					set newewb 1
				} else {
					set bindir $oldbindir
					set newewb 0
				}
			}
		}
		close $fd

		foreach i $cline {
			.l0 selection set $i
		}
		set tmplist [exec /bin/ls /tmp/]
		foreach tmp $tmplist {
			if {[string first "shelf2" $tmp] == 0 && [file owned /tmp/$tmp]} {
				catch {exec /bin/rm -f /tmp/$tmp} tmp2
			}
		}
	}
}

# ʸܥ򥻥å
proc setbuttons {} {
	global opt

	if {[llength [.l0 curselection]] == 1} {
		if {[string match "p*" $opt([.l0 curselection])]} {
			.c0 select
		} else {
			.c0 deselect
		}
		if {[string match "*i*" $opt([.l0 curselection])]} {
			.c1 select
		} else {
			.c1 deselect
		}
		if {[string match "*1" $opt([.l0 curselection])]} {
			.rb0 select
		} elseif {[string match "*2" $opt([.l0 curselection])]} {
			.rb1 select
		} elseif {[string match "*3" $opt([.l0 curselection])]} {
			.rb2 select
		}
	}
}

# ʸܥΥꥻå
proc unsetbuttons {} {
	.c0 deselect
	.c1 deselect
	.rb0 deselect
	.rb1 deselect
	.rb2 deselect
}

# ȥǥ쥯ȥ򡢤ӥեΥ󥯤䤤碌
proc selectdir {} {
	global cdir rootwd rootht
	global answer
	global flag
	global bindir newbindir newewb

	set flag 1
	set cdir [pwd]
	toplevel .dir
	wm geometry .dir +[expr $rootwd / 2 - 250]+[expr $rootht /2 - 200]
	frame .dir.f0
	frame .dir.f1
	frame .dir.f2
	frame .dir.f3
	frame .dir.f4
	listbox .dir.l0 -width 30 -xscrollcommand ".dir.s2 set" -yscrollcommand ".dir.s1 set" -selectmode single
	scrollbar .dir.s1 -orient vertical -command ".dir.l0 yview"
	scrollbar .dir.s2 -orient horizontal -command ".dir.l0 xview"
	label .dir.lb0 -text "ǥ쥯ȥꡧ" -width 60 -anchor w
	label .dir.lb1 -text [pwd] -width 60 -anchor w
	button .dir.b1 -text "OK" -command {
		if {[llength [.dir.l0 curselection]]} {
			cd [.dir.l0 get [.dir.l0 curselection]]
		}
		if {[file writable [pwd]]} {
			if {[file exists "Work"] && [file exists "Targets"]} {} else {
				ynmsg "[pwd]  Work ǥ쥯ȥ Targets ǥ쥯ȥޤǤ"
				if {$answer} {
					if {[file exists "Work"]} {} else {
						exec mkdir Work
					}
					if {[file exists "Targets"]} {} else {
						exec mkdir Targets
					}
				} else {
					errormsg "Workǥ쥯ȥTargetsǥ쥯ȥ꤬ʤΤǺȤ³ޤ\
	ȥǥ쥯ȥľƤ"
				}
			}
			if {[file exists "Working"] && [file exists "Targets"]} {
				if {[file exists "triger.def"] || [file exists "Styles/triger.def"]} {} else {
					setstyle
				}
			}
			grab release .dir
			set flag 0
			destroy .dir
			.workingdir configure -text "[pwd]"
			if {[file exists ".shelf2"]} {
				getlist
			} else {
				.l0 delete 0 end
			}
		} else {
			errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
			cd $cdir
			grab release .dir
			set flag 1
			destroy .dir
			.workingdir configure -text "[pwd]"
		}
	}
	button .dir.b2 -text "Cancel" -command {
		cd $cdir
		grab release .dir
		set flag 1
		destroy .dir
		.workingdir configure -text "[pwd]"
	}

	bind .dir <Destroy> {
		if {$flag} {
			cd $cdir
			.workingdir configure -text "[pwd]"
		}
	}

	button .dir.b3 -text "ǥ쥯ȥ" -command {
		toplevel .md
		wm geometry .md +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
		frame .md.f0
		label .md.lb0 -text "ǥ쥯ȥ̾ϤƤ"
		entry .md.e0 -textvariable dirname
		button .md.b0 -text "OK" -command {
			if {[string length $dirname]} {
				if {[file exists $dirname]} {
					errormsg "Ǥ¸ߤƤ̾Ǥ"
				} else {
					exec mkdir $dirname
					cd $dirname
					grab release .md
					destroy .md
					set ls [exec /bin/ls -Fa | sed -e "s/\\(.*\\)/\\\"\\1\\\"/"]
					.dir.l0 delete 0 end
					foreach file $ls {
						set tmpfile $file
						set lastchar [string index $tmpfile [expr [string length $tmpfile] -1]]
						if {[string compare $lastchar "@"] && [string compare $lastchar "*"]} {} else {
							set tmpfile [string range $tmpfile 0 [expr [string length $tmpfile] - 2]]
						}
						if {[string compare [file type $tmpfile] "link"]} {} else {
							set tmpfile [file readlink $tmpfile]
						}
						if {[file isdirectory $tmpfile]} {
							.dir.l0 insert end $file
						}
					}
					.dir.lb1 configure -text [pwd]
				}
			} else {
				errormsg "ǥ쥯ȥ̾ϤƤ"
			}
		}
		button .md.b1 -text "Cancel" -command {
			grab release .md
			destroy .md
		}
		pack .md.b0 .md.b1 -in .md.f0 -side left
		pack .md.lb0 .md.e0 .md.f0 -side top
		grab set .md
		wm resizable .md 0 0
		tkwait window .md
	}
	.dir.l0 delete 0 end
	set ls [exec /bin/ls -Fa | sed -e "s/\\(.*\\)/\\\"\\1\\\"/"]
	foreach file $ls {
		if {[string compare $file "./"] && ([string compare $file "../"]||[string compare [pwd] "/"])} {
			set tmpfile $file
			set lastchar [string index $tmpfile [expr [string length $tmpfile] -1]]
			if {[string compare $lastchar "@"] && [string compare $lastchar "*"]} {} else {
				set tmpfile [string range $tmpfile 0 [expr [string length $tmpfile] - 2]]
			}
			if {[string compare [file type $tmpfile] "link"]} {} else {
				set tmpfile [file readlink $tmpfile]
			}
			if {[file isdirectory $tmpfile]} {
				.dir.l0 insert end $file
			}
		}
	}
	bind .dir.l0 <Double-ButtonRelease> {
		if {[llength [.dir.l0 curselection]]} {
			set dname [pwd]/[.dir.l0 get [.dir.l0 curselection]]
			set tmpfile $dname
			set lastchar [string index $tmpfile [expr [string length $tmpfile] -1]]
			if {[string compare $lastchar "@"] && [string compare $lastchar "*"]} {} else {
				set tmpfile [string range $tmpfile 0 [expr [string length $tmpfile] - 2]]
			}
			if {[string compare [file type $tmpfile] "link"]} {} else {
				set tmpfile [file readlink $tmpfile]
			}
			if {[file isdirectory $tmpfile]} {
				cd $tmpfile
				set ls [exec /bin/ls -Fa | sed -e "s/\\(.*\\)/\\\"\\1\\\"/"]
				.dir.l0 delete 0 end
				foreach file $ls {
					if {[string compare $file "./"] && ([string compare $file "../"]||[string compare [pwd] "/"])} {
						set tmpfile $file
						set lastchar [string index $tmpfile [expr [string length $tmpfile] -1]]
						if {[string compare $lastchar "@"] && [string compare $lastchar "*"]} {} else {
							set tmpfile [string range $tmpfile 0 [expr [string length $tmpfile] - 2]]
						}
						if {[string compare [file type $tmpfile] "link"]} {} else {
							set tmpfile [file readlink $tmpfile]
						}
						if {[file isdirectory $tmpfile]} {
							.dir.l0 insert end $file
						}
					}
				}
				.dir.lb1 configure -text [pwd]
			} else {
				if {[string compare $tmpfile "//.."]} {
					errormsg "ǥ쥯ȥ򤷤Ƥ"
				}
			}
		}
	}

	pack .dir.lb0 .dir.lb1 -in .dir.f0 -side top
	grid .dir.l0 -in .dir.f1 -row 1 -column 0 -sticky nsew
	grid .dir.s1 -in .dir.f1 -row 1 -column 1 -sticky nsw
	grid .dir.s2 -in .dir.f1 -row 2 -column 0 -sticky new
	pack .dir.b1 .dir.b2 -in .dir.f2 -side left
	pack .dir.f0 .dir.f1 .dir.f2 -side top
	grab set .dir
	wm resizable .dir 0 0
	tkwait window .dir
}

# EPSեưŪUNIXѴ
proc tounixeps {} {
	global rootwd rootht
	global bindir newbindir newewb

	toplevel .tounix
	wm geometry .tounix +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
	label .tounix.lb0 -text "EPSեUNIXѴƤޤ"
	pack .tounix.lb0 -padx 50 -pady 50
	wm resizable .tounix 0 0
	update

	exec $bindir/tounixeps

	after 1000
	destroy .tounix
	update
}

# epsեμưå̤ѡ
proc epscheck {} {
	global rootwd rootht
	global bindir newbindir newewb

	toplevel .epscheck
	wm geometry .epscheck +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
	label .epscheck.lb0 -text "EPSեåƤޤ"
	pack .epscheck.lb0 -padx 50 -pady 50
	wm resizable .epscheck 0 0
	update

	set ls [exec /bin/ls | sed -e "s/ /\\*/g"]
	set j 0
	for {set i 0} {$i < [llength $ls]} {incr i} {
		regsub -all "!" [lindex $ls $i] " " fn
		if {[file isdirectory $fn]} {
			set ls [concat $ls [exec /bin/ls/ $fn | sed -e "s/ /!/g" | sed -e "s/\\(.*\\)/$fn\\/\\1/"]]
		} else {
			set fext [string tolower [file extension $fn]]
			if {![string compare ".eps" $fext]} {
				set fnm($j) $fn
				set date($j) [file mtime $fn]
				incr j
			}
		}
	}
	set count $j

	if {[file exists ".epschecklist"]} {
		set fd [open ".epschecklist" r]
		set i 0
		while {[gets $fd buff] != -1} {
			scan $buff "%s %s" ofn odate($i)
			regsub -all "!" $ofn " " ofnm($i)
			incr i
		}
		close $fd
		set ocount $i

		for {set i 0} {$i < $count} {incr i} {
			for {set j 0} {$j < $ocount} {incr j} {
				if {[string compare $fnm($i) $ofnm($j)]} {} else {
					if {$date($j) > $odate($j)} {
						break
					}
				}
				if {$j == $ocount} {
					catch {[exec $bindir/tounix $fnm($i)]} tmp
					if {[string match "*ꤢޤ*" $tmp]} {} else {
						errormsg $tmp
					}
				}
			}
		}
	} else {
		for {set i 0} {$i < $count} {incr i} {
			catch {[exec $bindir/epscheck $fnm($i)]} tmp
			if {[string match "*ꤢޤ*" $tmp]} {} else {
				errormsg $tmp
			}
		}
	}

	set fd [open ".epschecklist" w]
	for {set i 0} {$i < $count} {incr i} {
		regsub -all " " $fnm($i) "!" fn
		puts $fd "$fn $date($i)"
	}
	close $fd

	destroy .epscheck
	update
}

# ե֤Ƥǥ쥯ȥ򤷡
# ե򥫥ȥǥ쥯ȥإԡ
proc setstyle {} {
	global sdir rootwd rootht
	global answer
	global bindir newbindir newewb

	if {[file writable [pwd]]} {
		toplevel .stydir
		wm geometry .stydir +[expr $rootwd / 2 - 300]+[expr $rootht /2 - 150]
		set sdir [pwd]
		frame .stydir.f0
		frame .stydir.f1
		frame .stydir.f2
		label .stydir.lb0 -wraplength 200 -text "եΤǥ쥯ȥꤷƤ"
		label .stydir.lb1 -text [pwd] -width 60 -anchor w
		listbox .stydir.l0 -width 30 -xscrollcommand ".stydir.s2 set" -yscrollcommand ".stydir.s1 set" -selectmode single
		scrollbar .stydir.s1 -orient vertical -command ".stydir.l0 yview"
		scrollbar .stydir.s2 -orient horizontal -command ".stydir.l0 xview"
		button .stydir.b0 -text "OK" -command {
			if {[llength [.stydir.l0 curselection]]} {
				event generate .stydir.l0 <<select>>
			}
			ynmsg "$sdir ˥ե򥳥ԡޤǤ"
			if {$answer} {
				set stydir [pwd]
				set ls [exec /bin/ls]
				cd $sdir
				set list [exec /bin/ls]
				foreach file $list {
					set ext [file extension $file]
					if {[string compare ".sty" $ext] && [string compare ".cls" $ext] && [string compare "triger.def" $file] && [string compare "triger.diff" $file] && [string compare "yakumono.sed" $file] && [string compare "index.sed" $file] && [string compare "Rulefile" $file]} {} else {
						exec /bin/rm -f $file
					}
				}
				foreach file $ls {
					set ext [file extension $file]
					if {[string compare ".sty" $ext] && [string compare ".cls" $ext] && [string compare "triger.def" $file] && [string compare "triger.diff" $file] && [string compare "yakumono.sed" $file] && [string compare "index.sed" $file] && [string compare "Rulefile" $file]} {} else {
						exec /bin/cp $stydir/$file $file
					}
				}
				destroy .stydir
			} else {
				cd $sdir
			}
		}
		button .stydir.b1 -text "Cancel" -command {
			cd $sdir
			grab release .stydir
			destroy .stydir
		}
		bind .stydir <Destroy> {
			cd $sdir
		}

		.stydir.l0 delete 0 end
		set ls [exec /bin/ls -Fa | sed -e "s/\\(.*\\)/\\\"\\1\\\"/"]
		foreach file $ls {
			.stydir.l0 insert end $file
		}

		event add <<select>> <Double-ButtonRelease>

		bind .stydir.l0 <<select>> {
			if {[llength [.stydir.l0 curselection]]} {
				set dname [pwd]/[.stydir.l0 get [.stydir.l0 curselection]]
				set tmpfile $dname
				set lastchar [string index $tmpfile [expr [string length $tmpfile] -1]]
				if {[string compare $lastchar "@"] && [string compare $lastchar "*"]} {} else {
					set tmpfile [string range $tmpfile 0 [expr [string length $tmpfile] - 2]]
				}
				if {[string compare [file type $tmpfile] "link"]} {} else {
					set tmpfile [file readlink $tmpfile]
				}
				if {[file isdirectory $tmpfile]} { 
					cd $tmpfile
					set ls [exec /bin/ls -Fa | sed -e "s/\\(.*\\)/\\\"\\1\\\"/"]
					.stydir.l0 delete 0 end
					foreach file $ls {
						.stydir.l0 insert end $file
					}
					.stydir.lb1 configure -text [pwd]
				} else {
					if {[string compare $tmpfile "//.."]} {
						errormsg "ǥ쥯ȥ򤷤Ƥ"
					}
				}
			}
		}
		pack .stydir.lb0 .stydir.lb1 -in .stydir.f0 -side top
		grid .stydir.l0 -in .stydir.f1 -row 1 -column 0 -sticky nsew
		grid .stydir.s1 -in .stydir.f1 -row 1 -column 1 -sticky nsw
		grid .stydir.s2 -in .stydir.f1 -row 2 -column 0 -sticky new
		pack .stydir.b0 .stydir.b1 -in .stydir.f2 -side left
		pack .stydir.f0 .stydir.f1 .stydir.f2 -side top
		grab set .stydir
		wm resizable .stydir 0 0
		tkwait window .stydir
	} else {
		errormsg "Υǥ쥯ȥϽ񤭹ߤǤʤΤǺȤǤޤ"
	}
}

# ʸե
proc selectdoc {} {
	global rootwd rootht
	global flag
	global bindir newbindir newewb

	set flag 1
	toplevel .seldoc
	wm geometry .seldoc +[expr $rootwd / 2 - 250]+[expr $rootht /2 - 150]
	frame .seldoc.f0
	frame .seldoc.f1
	frame .seldoc.f2
	label .seldoc.lb0 -wraplength 250 -text "ʸեꤷƤ"
	listbox .seldoc.l0 -width 30 -xscrollcommand ".seldoc.s2 set" -yscrollcommand ".seldoc.s1 set" -selectmode extended
	scrollbar .seldoc.s1 -orient vertical -command ".seldoc.l0 yview"
	scrollbar .seldoc.s2 -orient horizontal -command ".seldoc.l0 xview"
	button .seldoc.b0 -text "OK" -command {
		set addname ""
		foreach i [.seldoc.l0 curselection] {
			set addname [lappend addname [.seldoc.l0 get $i]]
		}
		grab release .seldoc
		set flag 0
		destroy .seldoc
	}
	button .seldoc.b1 -text "Cancel" -command {
		set addname ""
		grab release .seldoc
		set flag 1
		destroy .seldoc
	}

	bind .seldoc <Destroy> {
		if {$flag} {
			set addname ""
		}
	}

	.seldoc.l0 delete 0 end
	set ls [exec /bin/ls]
	foreach file $ls {
		if {![file isdirectory $file]} {
			set ext [string tolower [file extension $file]]
			if {[string compare ".doc" $ext] && [string compare ".txt" $ext] && [string compare ".ewb" $ext]} {} else {
				.seldoc.l0 insert end $file
			}
		}
	}
#	bind .seldoc.l0 <Double-ButtonRelease> {
#		set addname [.seldoc.l0 get [.seldoc.l0 curselection]]
#		grab release .seldoc
#		destroy .seldoc
#	}
	pack .seldoc.lb0 -in .seldoc.f0 -side top
	grid .seldoc.l0 -in .seldoc.f1 -row 1 -column 0 -sticky nsew
	grid .seldoc.s1 -in .seldoc.f1 -row 1 -column 1 -sticky nsw
	grid .seldoc.s2 -in .seldoc.f1 -row 2 -column 0 -sticky new
	pack .seldoc.b0 .seldoc.b1 -in .seldoc.f2 -side left
	pack .seldoc.f0 .seldoc.f1 .seldoc.f2 -side top
	grab set .seldoc
	wm resizable .seldoc 0 0
	tkwait window .seldoc
}

# ե
proc selectstyle {} {
	global rootwd rootht
	global curdir filename
	global flag

	set flag 0
	set curdir ""
	set filename ""
	toplevel .selfile -width 300 -height 370 
	wm title .selfile "ե򳫤"
	wm geometry .selfile +[expr $rootwd / 2 - 150]+[expr $rootht /2 - 185]
	frame .selfile.f0
	frame .selfile.f1
	frame .selfile.f2
	label .selfile.lb0 -wraplength 250 -text "եꤷƤ"
	entry .selfile.filename -width 40 -textvariable filename
	label .selfile.lb1 -wraplength 250 -text "ǥ쥯ȥ:"
	label .selfile.lb2 -wraplength 250 -text $curdir
	listbox .selfile.l0 -width 36 -height 12 -xscrollcommand ".selfile.s2 set" -yscrollcommand ".selfile.s1 set" -selectmode single
	scrollbar .selfile.s1 -orient vertical -command ".selfile.l0 yview"
	scrollbar .selfile.s2 -orient horizontal -command ".selfile.l0 xview"
	button .selfile.ok -text "OK" -command {
		if {[llength [.selfile.l0 curselection]]} {
			set filename ${curdir}[.selfile.l0 get [.selfile.l0 curselection]]
		}
		grab release .selfile
		set flag 1
		destroy .selfile
	}
	button .selfile.cancel -text "Cancel" -command {
		set filename ""
		grab release .selfile
		set flag 0
		destroy .selfile
	}

	bind .selfile <Destroy> {
		if {!$flag} {
			set filename ""
		}
	}

	bind .selfile.l0 <Double-ButtonRelease> {
		if {[llength [.selfile.l0 curselection]]} {
			set file [.selfile.l0 get [.selfile.l0 curselection]]
			if {[string compare $file ""]} {
				if {[expr [string match "*\\*" $file] || [string match "*@" $file]]} {
					set file [string range $file 0 [expr [string length $file] -2]] 
				}
				if {[string compare [file type ${curdir}${file}] "link"]} {
					set dfile ${curdir}${file}
				} else {
					set dfile [file readlink ${curdir}${file}]
				}
				if {[file isdirectory $dfile]} {
					if {[string compare $file "./"]} {
						if {[string compare $file "../"]} {
							set file [.selfile.l0 get [.selfile.l0 curselection]]
							set curdir ${curdir}${file}
							.selfile.lb2 configure -text $curdir
							showfilelist2
						} else {
							if {[string compare $curdir ""]} {
								if {[expr [string last "/" [string range $curdir 0 [expr [string length $curdir] -2]]] == -1]} {
									set curdir ""
									.selfile.lb2 configure -text $curdir
								} else {
									set curdir [string range $curdir 0 [string last "/" [string range $curdir 0 [expr [string length $curdir] -2]]]]
									.selfile.lb2 configure -text $curdir
								}
								showfilelist2
							}
						}
					}
				}
			}
		}
	}

	showfilelist2

	grid .selfile.l0 -in .selfile.f1 -row 1 -column 0 -sticky nsew
	grid .selfile.s1 -in .selfile.f1 -row 1 -column 1 -sticky nsw
	grid .selfile.s2 -in .selfile.f1 -row 2 -column 0 -sticky new
	place .selfile.lb0 -x 10 -y 10
	place .selfile.filename -x 10 -y 40
	place .selfile.lb1 -x 10 -y 70
	place .selfile.lb2 -x 110 -y 70
	place .selfile.f1 -x 10 -y 90
	place .selfile.ok -x 70 -y 330
	place .selfile.cancel -x 120 -y 330

	grab set .selfile
	wm resizable .selfile 0 0
	tkwait window .selfile
}

# selfileɥ˥եɽ
proc showfilelist2 {} {
	global curdir filename

	.selfile.l0 delete 0 end
	if {[string compare $curdir ""]} {
		set ls [exec /bin/ls -Fa $curdir]
	} else {
		set ls [exec /bin/ls -Fa]
	}
	foreach file $ls {
		if {[string match "*\\*" $file] || [string match "*@" $file]} {
			set file [string range $file 0 [expr [string length $file] -2]] 
		}
		if {[string compare [file type ${curdir}${file}] "link"]} {
			set dfile ${curdir}${file}
		} else {
			set dfile [file readlink ${curdir}${file}]
		}
		if {[file isdirectory $dfile]} {
			if {[expr ![string compare $curdir ""] && ![string compare $file "Styles/"]] || [expr [string compare $curdir ""] && ![string compare $file "../"]]} {
				.selfile.l0 insert end $file
			}
		} else {
			if {![string match ".*" $file] && [string match "*.cls" $file]} {
				.selfile.l0 insert end $file
			}
		}
	}
	set filename $curdir
}

# tarե
proc selectarc {} {
	global rootwd rootht
	global curdir arcfile
	global flag
	global bindir newbindir newewb

	set flag 1
	set curdir [pwd]/
	toplevel .selarc -width 300 -height 350
	wm geometry .selarc +[expr $rootwd / 2 - 150]+[expr $rootht /2 - 175]
	frame .selarc.f0
	frame .selarc.f1
	frame .selarc.f2
	label .selarc.lb0 -wraplength 280 -text "ŸեꤷƤ"
    label .selarc.lb1 -wraplength 280 -text "ǥ쥯ȥ:"
    label .selarc.lb2 -wraplength 280 -text $curdir
	listbox .selarc.l0 -width 36 -height 12 -xscrollcommand ".selarc.s2 set" -yscrollcommand ".selarc.s1 set" -selectmode single
	scrollbar .selarc.s1 -orient vertical -command ".selarc.l0 yview"
	scrollbar .selarc.s2 -orient horizontal -command ".selarc.l0 xview"
	button .selarc.ok -text "OK" -command {
		if {[llength [.selarc.l0 curselection]]} {
			set arcfile ${curdir}[.selarc.l0 get [.selarc.l0 curselection]]
		} else {
			set arcfile ""
		}
		grab release .selarc
		set flag 0
		destroy .selarc
	}
	button .selarc.cancel -text "Cancel" -command {
		set arcfile ""
		grab release .selarc
		set flag 1
		destroy .selarc
	}

	bind .selarc <Destroy> {
		if {$flag} {
			set arcfile ""
		}
	}

	bind .selarc.l0 <Double-ButtonRelease> {
		if {[llength [.selarc.l0 curselection]]} {
			set file [.selarc.l0 get [.selarc.l0 curselection]]
			if {[string compare $file ""]} {
				if {[string match "*\\*" $file] || [string match "*@" $file]} {
					set $file [string range $file 0 [expr [string length $file] -2]] 
				}
				if {[string compare [file type ${curdir}${file}] "link"]} {
					set dfile ${curdir}${file}
				} else {
					set dfile [file readlink ${curdir}${file}]
				}
				if {[file isdirectory $dfile]} {
					if {[string compare $file "./"]} {
						if {[string compare $file "../"]} {
							set file [.selarc.l0 get [.selarc.l0 curselection]]
							set curdir $curdir$file
							.selarc.lb2 configure -text $curdir
						} else {
							if {[string compare $curdir ""]} {
								if {[string match  "*../" $curdir]} {
									set curdir ${curdir}${file}
									.selarc.lb2 configure -text $curdir
								} else {
									if {[string last "/" [string range $curdir 0 [expr [string length $curdir] -2]]] == -1} {
										set curdir ""
										.selarc.lb2 configure -text $curdir
									} else {
										set curdir [string range $curdir 0 [string last "/" [string range $curdir 0 [expr [string length $curdir] -2]]]]
										.selarc.lb2 configure -text $curdir
									}
								}
							} else {
								set curdir "../"
								.selarc.lb2 configure -text $curdir
							}
						}
						showarclist
					}
				}
			}
		}
	}

	showarclist

	grid .selarc.l0 -in .selarc.f1 -row 1 -column 0 -sticky nsew
	grid .selarc.s1 -in .selarc.f1 -row 1 -column 1 -sticky nsw
	grid .selarc.s2 -in .selarc.f1 -row 2 -column 0 -sticky new
	place .selarc.lb0 -x 10 -y 10
	place .selarc.lb1 -x 10 -y 30
	place .selarc.lb2 -x 10 -y 50
	place .selarc.f1 -x 10 -y 70
	place .selarc.ok -x 70 -y 320
	place .selarc.cancel -x 120 -y 320

	grab set .selarc
	wm resizable .selarc 0 0
	tkwait window .selarc
}

# selectarcɥ˥եɽ
proc showarclist {} {
	global curdir
	global bindir newbindir newewb

	.selarc.l0 delete 0 end
	if {[string compare $curdir ""]} {
		set ls [exec /bin/ls -Fa $curdir | sed -e "s/\\(.*\\)/\\\"\\1\\\"/"]
	} else {
		set ls [exec /bin/ls -Fa | sed -e "s/\\(.*\\)/\\\"\\1\\\"/"]
	}
	foreach file $ls {
		if {[string match "*\\*" $file] || [string match "*@" $file] || [string match "*/" $file]} {
			set file [string range $file 0 [expr [string length $file] -2]]
		}
		if {[string compare [file type ${curdir}${file}] "link"]} {
			set dfile ${curdir}${file}
		} else {
			set dfile [file readlink ${curdir}${file}]
		}
		if {[file isdirectory $dfile]} {
			if {[string compare $file "."]} {
				if {[string compare $curdir "/"] || [string compare $file ".."]} {
					.selarc.l0 insert end ${file}/
				}
			}
		} else {
			set ext [string tolower [file extension $dfile]]
			if {[string compare ".tar" $ext]} {} else {
				.selarc.l0 insert end $file
			}
		}
	}
}

# tarŸ֤ɽ
proc showtar {tarfile} {
	global rootwd rootht
	global curdir extdir extworkdir
	global tartmp
	global flag
	global bindir newbindir newewb

	set flag 1
	set curdir [pwd]/
	set extdir $curdir
	toplevel .showtar -width 400 -height 400
	wm geometry .showtar +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 200]
	frame .showtar.f0
	frame .showtar.f1
	frame .showtar.f2
	label .showtar.lb0 -wraplength 380 -text "$tarfile ϰʲΤ褦Ÿޤ"
	label .showtar.lb1 -text $tarfile
	listbox .showtar.l0 -width 49 -height 17 -xscrollcommand ".showtar.s2 set" -yscrollcommand ".showtar.s1 set" -selectmode single
	scrollbar .showtar.s1 -orient vertical -command ".showtar.l0 yview"
	scrollbar .showtar.s2 -orient horizontal -command ".showtar.l0 xview"
	button .showtar.ok -text "OK" -command {
		set extdir $curdir
		grab release .showtar
		set flag 0
		destroy .showtar
	}
	button .showtar.chdir -text "ǥ쥯ȥѹ" -command {
		set tarfile [.showtar.lb1 cget -text]
		selectextdir $tarfile
		set filelist ""
		for {set i 7} {1} {incr i 8} {
			set file [lindex $tartmp $i]
			if {[string compare $file ""]} {
				lappend filelist ${extdir}${file}
			} else {
				break
			}
		}

		.showtar.l0 delete 0 end
		foreach file $filelist {
			.showtar.l0 insert end $file
		}
	}
	button .showtar.cancel -text "Cancel" -command {
		set extdir "-"
		grab release .showtar
		set flag 1
		destroy .showtar
	}

	bind .showtar <Destroy> {
		if {$flag} {
			set extdir "-"
		}
	}

#	catch {exec tar tvf $tarfile} tartmp
	set filelist ""
	for {set i 7} {1} {incr i 8} {
		set file [lindex $tartmp $i]
		if {[string compare $file ""]} {
			lappend filelist ${extdir}${file}
		} else {
			break
		}
	}

	.showtar.l0 delete 0 end
	foreach file $filelist {
		.showtar.l0 insert end $file
	}

	grid .showtar.l0 -in .showtar.f1 -row 1 -column 0 -sticky nsew
	grid .showtar.s1 -in .showtar.f1 -row 1 -column 1 -sticky nsw
	grid .showtar.s2 -in .showtar.f1 -row 2 -column 0 -sticky new
	place .showtar.lb0 -x 10 -y 10
	place .showtar.f1 -x 10 -y 50
	place .showtar.ok -x 50 -y 370
	place .showtar.chdir -x 100 -y 370
	place .showtar.cancel -x 250 -y 370

	grab set .showtar
	wm resizable .showtar 0 0
	tkwait window .showtar
}

# tarŸ
proc selectextdir {tarfile} {
	global rootwd rootht
	global curdir extdir extworkdir
	global bindir newbindir newewb

	set tarfile [file tail $tarfile]
	toplevel .selext -width 300 -height 400
	wm geometry .selext +[expr $rootwd / 2 - 150]+[expr $rootht /2 - 200]
	frame .selext.f0
	frame .selext.f1
	frame .selext.f2
	label .selext.lb0 -wraplength 280
	if {[string compare $extworkdir ""]} {
		.selext.lb0 configure -text "$tarfile Ÿ\nǥ쥯ȥꤷƤ\n򤵤줿ǥ쥯ȥβ\n$extworkdir ǥ쥯ȥŸޤ"
	} else {
		.selext.lb0 configure -text "$tarfile Ÿ\nǥ쥯ȥꤷƤ\n򤵤줿ǥ쥯ȥľܥեŸޤ"
	}
    label .selext.lb1 -wraplength 280 -text "򤷤Ƥǥ쥯ȥ:"
    label .selext.lb2 -wraplength 280 -text $curdir
	listbox .selext.l0 -width 36 -height 12 -xscrollcommand ".selext.s2 set" -yscrollcommand ".selext.s1 set" -selectmode single
	scrollbar .selext.s1 -orient vertical -command ".selext.l0 yview"
	scrollbar .selext.s2 -orient horizontal -command ".selext.l0 xview"
	button .selext.ok -text "OK" -command {
		if {[llength [.selext.l0 curselection]]} {
			event generate .selext.l0 <<select>>
		}
		set extdir $curdir
		grab release .selext
		destroy .selext
	}
	button .selext.cancel -text "Cancel" -command {
		grab release .selext
		destroy .selext
	}

	event add <<select>> <Double-ButtonRelease>

	bind .selext.l0 <<select>> {
		if {[llength [.selext.l0 curselection]]} {
			set file [.selext.l0 get [.selext.l0 curselection]]
			if {[string compare $file ""]} {
				if {[string match "*\\*" $file] || [string match "*@" $file]} {
					set $file [string range $file 0 [expr [string length $file] -2]]
				}
				if {[string compare [file type ${curdir}${file}] "link"]} {
					set dfile ${curdir}${file}
				} else {
					set dfile [file readlink ${curdir}${file}]
				}
				if {[file isdirectory $dfile]} {
					if {[string compare $file "./"]} {
						if {[string compare $file "../"]} {
							set file [.selext.l0 get [.selext.l0 curselection]]
							set curdir $curdir$file
							.selext.lb2 configure -text $curdir
						} else {
							if {[string compare $curdir ""]} {
								if {[string match  "*../" $curdir]} {
									set curdir ${curdir}${file}
									.selext.lb2 configure -text $curdir
								} else {
									if {[string last "/" [string range $curdir 0 [expr [string length $curdir] -2]]] == -1} {
										set curdir ""
										.selext.lb2 configure -text $curdir
									} else {
										set curdir [string range $curdir 0 [string last "/" [string range $curdir 0 [expr [string length $curdir] -2]]]]
										.selext.lb2 configure -text $curdir
									}
								}
							} else {
								set curdir "../"
								.selext.lb2 configure -text $curdir
							}
						}
						showdirlist
					}
				}
			}
		}
	}

	showdirlist

	grid .selext.l0 -in .selext.f1 -row 1 -column 0 -sticky nsew
	grid .selext.s1 -in .selext.f1 -row 1 -column 1 -sticky nsw
	grid .selext.s2 -in .selext.f1 -row 2 -column 0 -sticky new
	place .selext.lb0 -x 10 -y 10
	place .selext.lb1 -x 10 -y 80
	place .selext.lb2 -x 10 -y 100
	place .selext.f1 -x 10 -y 120
	place .selext.ok -x 70 -y 370
	place .selext.cancel -x 120 -y 370

	grab set .selext
	wm resizable .selext 0 0
	tkwait window .selext
}

# selectextdirɥ˥ǥ쥯ȥɽ
proc showdirlist {} {
	global curdir type
	global bindir newbindir newewb

	.selext.l0 delete 0 end
	if {[string compare $curdir ""]} {
		set ls [exec /bin/ls -Fa $curdir | sed -e "s/\\(.*\\)/\\\"\\1\\\"/"]
	} else {
		set ls [exec /bin/ls -Fa | sed -e "s/\\(.*\\)/\\\"\\1\\\"/"]
	}
	foreach file $ls {
		if {[string match "*\\*" $file] || [string match "*@" $file] || [string match "*/" $file]} {
			set file [string range $file 0 [expr [string length $file] -2]]
		}
		if {[string compare [file type ${curdir}${file}] "link"]} {
			set dfile ${curdir}${file}
		} else {
			set dfile [file readlink ${curdir}${file}]
		}
		if {[file isdirectory $dfile]} {
			if {[string compare $file "."]} {
				if {[string compare $curdir "/"] || [string compare $file ".."]} {
					.selext.l0 insert end ${file}/
				}
			}
		}
	}
}

# 䤤碌Υå
proc ynmsg {msg} {
	global rootwd rootht
	global answer

	toplevel .yn
	wm geometry .yn +[expr $rootwd / 2 - 200]+[expr $rootht /2 - 100]
	frame .yn.f0
	label .yn.lb0 -wraplength 300 -text $msg
	button .yn.b0 -text "OK" -command {
		grab release .yn
		set answer 1
		destroy .yn
	}
	button .yn.b1 -text "Cancel" -command {
		grab release .yn
		set answer 0
		destroy .yn
	}
	pack .yn.b0 .yn.b1 -in .yn.f0 -side left
	pack .yn.lb0 .yn.f0 -padx 10 -pady 10 -side top
	grab set .yn
	wm resizable .yn 0 0
	tkwait window .yn
}

# ʪ°ꤵƤ븶ƥե
proc getfilelist {} {
	global phyfname phycount
	global bindir newbindir newewb

	set phycount 0

	if {[file exists ".shelf2"]} {
		set fd [open ".shelf2" r]
		while {[gets $fd line] >= 0} {
			if {[string compare $line "ewb3.1"] && [string compare $line "ewb3.2"] && [string compare $line "ewb3.3"]} {
				scan $line "%s %s %s %s %s" \
				body($phycount) phyfname($phycount) page($phycount) stylef($phycount) opt($phycount)
				if {[string compare $body($phycount) "index"]} {} else {
					continue
				}
#				if {[string match "p*" $opt($phycount)]} {} else {
#					continue
#				}
				incr phycount
			}
		}
		close $fd
	}
}

# triger.defɽȥꥬ
proc gettriger {} {
	global triger tcount
	global bindir newbindir newewb

	set tcount 0
	if {[file exists "triger.def"] || [file exists "Styles/triger.def"]} {
		if {[file exists "triger.def"]} {
			set fd [open "triger.def" r]
		} else  {
			set fd [open "Styles/triger.def" r]
		}
		while {[gets $fd buff] != -1} {
			regsub -all "\[\t \]+" $buff " " buff1
			regsub -all "+" $buff1 " " buff
			regsub -all " +" $buff " " buff1
			if {[string first "deffigure" $buff1] == 0} {
				for {set i 9} {$i < [string length $buff1]} {incr i} {
					if {[string compare [string index $buff1 $i] " "]} {
						for {set j $i} {$j < [string length $buff1]} {incr j} {
							if {[string compare [string index $buff1 $j] " "]} {} else {
								set triger($tcount) "//[string range $buff1 $i [expr $j - 1]]"
								incr tcount
								break
							}
						}
						break
					}
				}
			}
		}
	}
}

proc showhelp {win filename} {
	global rootwd rootht guilib
	global bindir newbindir newewb

	if {[winfo exists $win]} {} else {
		if {[file exists $guilib/$filename]} {} else {
			errormsg "ιܤΥإץե뤬Ĥޤ"
			return
		}

		toplevel $win -width 640 -height 600
		wm geometry $win +[expr $rootwd / 2 - 320]+[expr $rootht /2 - 300]
		text $win.text -width 80 -height 30 -yscrollcommand "$win.y set"
#		scrollbar $win.x -orient horizontal -command "$win.text xview"
		scrollbar $win.y -orient vertical -command "$win.text yview"
		frame $win.frame

		button $win.ok -text "OK" -command "
			destroy $win
		"

		$win.text delete 0.0 end
		set fd [open $guilib/$filename r]
		while {[gets $fd line] >= 0} {$win.text insert end "$line\n"}

		close $fd

		grid $win.text -row 0 -column 0 -sticky nsew
#		grid $win.x -row 1 -column 0 -sticky ew
		grid $win.y -row 0 -column 1 -sticky ns
		grid $win.ok -row 2 -column 0
	}
}

# xemacsư
set xempid [exec xemacs -q -l /usr/share/ewb/3.3/xemacs/lisp/ewb-load.el -unmapped >& /dev/null &]
#after 5000
#catch {exec /bin/ps | egrep $xempid.*xemacs} tmp
#while {[string compare $xempid [lindex $tmp 0]]} {
#	catch {exec /bin/ps | egrep $xempid.*xemacs} tmp
#}

# ǽΥꥹɽ
getlist
wm resizable . 0 0
set wflag 1
