#!/usr/local/bin/wish8.0jp
kanji defaultInputCode EUC

# ѡ桼ʳμ¹Ԥ
if { $env(HOME) != "/root" } then {
	tk_messageBox -type ok -icon error -message "ѡ桼Ǽ¹ԤƲ"
	exit
}
	
#########################################
## ƥɥ.ˤδ
#########################################
wm title . "Tk Kamiyama PacKaGe"

#########################################
## եؤΥѥ
#########################################
set icon_path "/usr/local/lib/icons"
set KPKG_conf "/usr/local/etc/kpkg.conf"
set INST_LOCK "/tmp/inst.lock"

#########################################
## Ͽ
#########################################
image create photo kamiyama -file $icon_path/kamiyama.gif
image create photo pkg -file $icon_path/package.gif
image create photo trash -file $icon_path/trash.gif
image create photo unpkg -file $icon_path/unpackage.gif
image create photo pkginfo -file $icon_path/info2.gif
image create photo pkgclose -file $icon_path/close.gif

#########################################
## ƥɥ.ѤΥ٥κ
#########################################
label .l0 -text "Pkg&Inst" -font { helvetica 9 }
label .l1 -text "Delete" -font { helvetica 9 }
label .l2 -text "Install" -font { helvetica 9 }
label .l3 -text "PkgInfo" -font { helvetica 9 }
label .l4 -text "Exit" -font { helvetica 9 }

#########################################
## ƥɥ.ѤΥܥκ
#########################################
#########################################
## 1.ե᡼ܥ
#########################################
button .b0 -image kamiyama -command {

	## ҥɥ.w0ˤδ
	toplevel .w0
	wm title .w0 "TKpkg Information"

	## ҥɥ.w0ѤΥ٥κ
	label .w0.l0 -image kamiyama
	label .w0.l1 -justify left -relief sunken -wraplength 200 -padx 10 -pady 10 -text \
"δʰץѥåޥ͡㡼makeɤοͤΰ٤˺äġǤ
make_installtkpkg¹ԤСñ˥ѥååȤȥ󥹥ȡեΰǤޤΰǥ󥤥󥹥ȡڡǤޤ顢ˤޤ̵ǤȤ⤪"

	## ҥɥ.w0ѤΥܥκ
	button .w0.b0 -text "Exit" -command "destroy .w0"

	## ҥɥ.w0ˤʤ
	pack .w0.l0 .w0.l1 .w0.b0
}

#########################################
## 2.Pkg & Instܥ
#########################################
button .b1 -image pkg -command {

	## ҥɥ.w1ˤδ
	toplevel .w1
	wm title .w1 "Packaging & Install"
	.w1 configure -width 560 -height 450

	## ҥɥ.w1ѤΥ٥κ
	label .w1.l0 -text "ץꥱ̾ :" -anchor w
	label .w1.l1 -text " AfterStep1.4.5beta2 ʤ as145b2 ƤʴǤ " -anchor w
	label .w1.l2 -text "MakefileΤǥ쥯ȥ :"  -anchor w
	label .w1.l3 -text "ѹ Makefile ǲ" -anchor w
	label .w1.l4 -text "make install.man ɬס :" -anchor w
	label .w1.l5 -text "¾Υ󥹥ȡ륪ץ :" -anchor w
	label .w1.l6 -text " makeθΥץǤǤ " -anchor w
	label .w1.l7 -text "ϥǥ쥯ȥ :" -anchor w
	label .w1.l8 -text "оݳ|򶴤󵭡:" -anchor w
	label .w1.l9 -text "ѥååȤޤ :" -anchor w

	## Ϸˤ٥Ǥ
	label .w1.line0 -relief sunken
	label .w1.line1 -relief sunken
	label .w1.line2 -relief sunken
	label .w1.line3 -relief sunken

	## ҥɥ.w1ѤΥȥ꡼κȽͤ
	entry .w1.e0 -textvariable App_name
	entry .w1.e1 -textvariable Mf_path
	entry .w1.e2 -textvariable Mk_op
	entry .w1.e3 -textvariable S_path
	entry .w1.e4 -textvariable Rm_path
	set Mf_path $env(PWD)
	set S_path "/usr"
	set Rm_path [ exec cat $KPKG_conf ]

	## ҥɥ.w1ѤΥ饸ܥκȽͤ
	radiobutton .w1.rb0 -text "YES" -value YES -var Man_op
	radiobutton .w1.rb1 -text "NO" -value NO -var Man_op
	radiobutton .w1.rb2 -text "YES" -value YES -var Pkg_op
	radiobutton .w1.rb3 -text "NO" -value NO -var Pkg_op
	set Man_op "NO"
	set Pkg_op "YES"

	## ҥɥ.w1ѤΥƥȤκ
	text .w1.t0 -yscrollcommand ".w1.sl0 set"
	scrollbar .w1.sl0 -orient vertical -command ".w1.t0 yview"
	.w1.t0 insert end \
"󥹥ȡ򳫻ϤȤФ餯ߤޤäΤ褦ˤʤޤȤƤޤΤǤ¿
դȤơ٥󥹥ȡ˼Ԥmake24ְ˺ƤӼ¹ԤȤޤޤ󡣹¤ΤʤʤΤǡƲʤԤ˽褿/var/log/kpkg/PLIST.ץ̾פȤեȥ롼Ȥ˺ѥåեλĳ褦Ǥ顢ưǺƤʤȥǤʤˤʤޤΤǤꤤޤ"

	## ҥɥ.w1ѤΥܥκ
	button .w1.b0 -text "ѹ" -command {
		set Mf_path [ tk_getOpenFile -initialdir $env(PWD) -filetypes { { "Makefile" Makefile } {"ƤΥե" *} } ]

		## Ϳ줿ѥMakefileפƥǥ쥯ȥؤΥѥˤ
		set Mf_path [ exec base_tkpkg Mfpath $Mf_path ]
	}

	button .w1.b1 -text "Exit " -command "destroy .w1"
	button .w1.b2 -text "Start" -command {

		## åեγǧ
		if { [ exec base_tkpkg LockCheck ] == "YES" } then {
			set Lock_delete [ tk_messageBox -type okcancel -icon error -message \
"ΥץȤï¹Ԥ
ƤǽޤOK
ܥ򲡤ȥåե
ޤ
CancelߤƲ" ]
			if { $Lock_delete == "ok" } then {
				exec rm $INST_LOCK
			}
		} else {

			## ְκեθФȥåեκ
			exec base_tkpkg Findfiles $S_path

			## make installμ¹
			exec base_tkpkg Installfile $Mf_path $Man_op $Mk_op

			## 󥹥ȡեθФȥåեκ
			exec base_tkpkg FindNewfiles $App_name $S_path $Rm_path

			## ѥå"YES"ʤѥå
			if { $Pkg_op == "YES" } then {
				exec base_tkpkg Mkpkg $App_name
			}

			## Ƚλ
			bell
			.w1.t0 delete 1.0 end
			.w1.t0 insert end \
"٤ƽޤ줵ޡ ^_^ 
----------------------------------------------------------
ѥå : /var/log/kpkg/PLIST.$App_name
ѥå줿 /ʥ롼ȡˤˤޤ
----------------------------------------------------------
󥹥ȡեꥹȡ
[ exec base_tkpkg InfoList $App_name]
----------------------------------------------------------
󥹥ȡե륵
[ exec base_tkpkg InfoSize $App_name]"
		}
	}

	## ҥɥ.w1Ѥʤ
	place .w1.l0 -x 8 -y 8 -width 210
	place .w1.e0 -x 218 -y 8 -width 334
	place .w1.l1 -x 218 -y 34 -width 334

	place .w1.line0 -x 8 -y 61 -width 544 -height 2

	place .w1.l2 -x 8 -y 66 -width 210
	place .w1.b0 -x 218 -y 66 -width 48  -height 23
	place .w1.l3 -x 266 -y 66 -width 286
	place .w1.e1 -x 8 -y 92 -width 544
	place .w1.l4 -x 8 -y 118 -width 220
	place .w1.rb0 -x 228 -y 118 -width 162
	place .w1.rb1 -x 390 -y 118 -width 162
	place .w1.l5 -x 8 -y 144 -width 210
	place .w1.e2 -x 218 -y 144 -width 334
	place .w1.l6 -x 218 -y 170 -width 334

	place .w1.line1 -x 8 -y 197 -width 544 -height 2 

	place .w1.l7 -x 8 -y 202 -width 210
	place .w1.e3 -x 218 -y 202 -width 334
	place .w1.l8 -x 8 -y 228 -width 210
	place .w1.e4 -x 218 -y 228 -width 334

	place .w1.line2 -x 8 -y 255 -width 544 -height 2 

	place .w1.l9 -x 8 -y 260 -width 220
	place .w1.rb2 -x 228 -y 260 -width 162
	place .w1.rb3 -x 390 -y 260 -width 162

	place .w1.line3 -x 8 -y 287 -width 544 -height 2

	place .w1.b1 -x 200 -y 292 -width 60 -height 23
	place .w1.b2 -x 300 -y 292 -width 60 -height 23
	place .w1.t0 -x 8 -y 318 -width 534 -height 126 
	place .w1.sl0 -x 534 -y 318 -width 18 -height 126 
}

#########################################
## 3.Deleteܥ
#########################################
button .b2 -image trash -command {

	## ҥɥ.w2ˤδ
	toplevel .w2
	wm title .w2 "Delete Packge"

	## ҥɥ.w2ѤΥꥹȤκ
	listbox .w2.ls0 -yscrollcommand ".w2.sl0 set"
	scrollbar .w2.sl0 -command ".w2.ls0 yview"
	foreach i [ exec base_tkpkg PkgList ] {
		.w2.ls0 insert end $i
	}

	## ҥɥ.w2ѤΥܥκ
	button .w2.b0 -text "󥻥" -command "destroy .w2"

	button .w2.b1 -text "ܤõ" -command {

		## õǧ
		set D_pname [ .w2.ls0 get active ]
		set warn [ tk_dialog .ww2 "DeletePkg" $D_pname"õޤ衪" warning 1 "λ" "ᡪ" ]
		if { $warn == 0 } then {
			exec base_tkpkg DeletePkg $D_pname
		}

		## õΥꥹȤɤľ
		.w2.ls0 delete 0 end
		foreach i [ exec base_tkpkg PkgList ] {
			.w2.ls0 insert end $i
		}
	}

	## ҥɥ.w2Ѥʤ
	grid .w2.ls0 -column 0 -row 0 -columnspan 2 -sticky ew
	grid .w2.sl0 -column 2 -row 0 -sticky ns
	grid .w2.b0 -column 0 -row 1
	grid .w2.b1 -column 1 -row 1 -columnspan 2
}

#########################################
## 4.Installܥ
#########################################
button .b3 -image unpkg -command {

	## ҥɥ.w3ˤδ
	toplevel .w3
	wm title .w3 "Package Install"
	.w3 configure -width 330 -height 162

	## ҥɥ.w3ѤΥ٥κ
	label .w3.l0 -text "󥹥ȡե :" -anchor w
	label .w3.l1 -text "Υե륵 :" -anchor w
	label .w3.l2 -text "󥹥ȡե :" -anchor w

	## Ϸˤ٥Ǥ
	label .w3.line0 -relief sunken
	label .w3.line1 -relief sunken
	label .w3.line2 -relief sunken

	## ҥɥ.w3ѤΥȥ꡼κ
	entry .w3.e0 -textvariable Bin_name
	entry .w3.e1 -textvariable Bin_size

	## ҥɥ.w3ѤΥܥκ
	button .w3.b0 -text "" -command {
		set Bin_name [ tk_getOpenFile -initialdir "/" -filetypes { { "ѥåե" {.tar.gz .tgz .tar} } } ]
		set Bin_size [ exec base_tkpkg BinSize $Bin_name ]
	}

	button .w3.b1 -text "ɽ" -command {

		## ¹ɥ.ww3ˤν
		toplevel .ww3
		wm title .ww3 "Install File List"
		.ww3 configure -width 400 -height 200

		## ¹ɥ.ww3ѤΥƥȤκ
		text .ww3.t0 -xscrollcommand ".ww3.sl0 set" -yscrollcommand ".ww3.sl1 set"
		scrollbar .ww3.sl0 -orient horizontal -command ".ww3.t0 xview"
		scrollbar .ww3.sl1 -orient vertical -command ".ww3.t0 yview"
		foreach i [ exec base_tkpkg BinList $Bin_name ] {
		.ww3.t0 insert end $i\n
		}

		## ¹ɥ.ww3Ѥʤ
		place .ww3.t0 -relx 0 -rely 0 -relwidth 0.96 -relheight 0.92
		place .ww3.sl0 -relx 0 -rely 0.92 -relwidth 0.96 -relheight 0.08
		place .ww3.sl1 -relx 0.96 -rely 0.0 -relwidth 0.04 -relheight 0.92
	}

	button .w3.b2 -text " Exit " -command "destroy .w3"

	button .w3.b3 -text "Install" -command {
		exec tar xzf $Bin_name -C /

		## 󥹥ȡ뽪λ
		tk_messageBox -type ok -message "λޤ"
	}

	## ҥɥ.w3Ѥʤ
	place .w3.l0 -x 8 -y 8 -width 200
	place .w3.b0 -x 208 -y 8 -width 114 -height 23
	place .w3.e0 -x 8 -y 34 -width 314
	place .w3.line0 -x 8 -y 61 -width 314 -height 2
	place .w3.l1 -x 8 -y 66 -width 200
	place .w3.e1 -x 208 -y 66 -width 114
	place .w3.line1 -x 8 -y 93 -width 314 -height 2
	place .w3.l2 -x 8 -y 98 -width 200
	place .w3.b1 -x 208 -y 98 -width 114 -height 23
	place .w3.line2 -x 8 -y 125 -width 314 -height 2
	place .w3.b2 -x 100 -y 130
	place .w3.b3 -x 200 -y 130
}

#########################################
## 5.PkgInfoܥ
#########################################
button .b4 -image pkginfo -command {

	## ҥɥ.w4ˤν
	toplevel .w4
	wm title .w4 "Package Information"

	## ҥɥ.w4ѤΥꥹȤκ
	listbox .w4.ls0 -yscrollcommand ".w4.sl0 set"
	scrollbar .w4.sl0 -command ".w4.ls0 yview"
	foreach i [ exec base_tkpkg PkgList ] {
		.w4.ls0 insert end $i
	}

	## ҥɥ.w4ѤΥܥκ
	button .w4.b0 -text "󥻥" -command "destroy .w4"

	button .w4.b1 -text "ܤξ" -command {

		## ¹ɥ.ww4ˤν
		toplevel .ww4
		wm title .ww4 "Package Detail"
		.ww4 configure -width 400 -height 300

		## ¹ɥ.ww4ѤΥ٥κ
		label .ww4.l0 -text "ץꥱ̾ :" -anchor w
		label .ww4.l1 -text "ե륵 :" -anchor w
		label .ww4.l2 -text "ѥå :" -anchor w

		## ¹ɥ.ww4ѤΥȥ꡼κ
		set Info_pkg [ .w4.ls0 get active ]
		entry .ww4.e0 -textvariable Info_name
		set Info_name $Info_pkg
		entry .ww4.e1 -textvariable Info_size
		set Info_size [ exec base_tkpkg InfoSize $Info_pkg ]
		entry .ww4.e2 -textvariable Info_date
		set Info_date [ exec base_tkpkg InfoDate $Info_pkg ]

		## ¹ɥ.ww4ѤΥܥκ
		button .ww4.b0 -text "OK" -command "destroy .ww4"

		## ¹ɥ.ww4ѤΥƥȤκ
		text .ww4.t0 -xscrollcommand ".ww4.sl0 set" -yscrollcommand ".ww4.sl1 set"
		scrollbar .ww4.sl0 -orient horizontal -command ".ww4.t0 xview"
		scrollbar .ww4.sl1 -orient vertical -command ".ww4.t0 yview"
		foreach i [ exec base_tkpkg InfoList $Info_pkg ] {
			.ww4.t0 insert end $i\n
		}

		## ¹ɥ.ww4Ѥʤ
		place .ww4.l0 -x 8 -y 8 -width 150
		place .ww4.e0 -x 158 -y 8 -width 234 
		place .ww4.l1 -x 8 -y 34 -width 150
		place .ww4.e1 -x 158 -y 34 -width 234
		place .ww4.l2 -x 8 -y 60 -width 150
		place .ww4.e2 -x 158 -y 60 -width 234
		place .ww4.t0 -x 8 -y 86 -width 366 -height 162
		place .ww4.sl0 -x 8 -y 248 -width 366 -height 18
		place .ww4.sl1 -x 374 -y 86 -width 18 -height 162
		place .ww4.b0 -x 180 -y 268
	}

	## ҥɥ.w4Ѥʤ
	grid .w4.ls0 -column 0 -row 0 -columnspan 2 -sticky ew
	grid .w4.sl0 -column 2 -row 0 -sticky ns
	grid .w4.b0 -column 0 -row 1
	grid .w4.b1 -column 1 -row 1 -columnspan 2
}

#########################################
## 6.Exitܥ
#########################################
button .b5 -image pkgclose -command exit

#########################################
## ƥɥ.Ѥʤ
#########################################
grid .b0 -column 0 -row 0 -columnspan 5 -sticky ew
grid .b1 -column 0 -row 1
grid .b2 -column 1 -row 1
grid .b3 -column 2 -row 1
grid .b4 -column 3 -row 1
grid .b5 -column 4 -row 1
grid .l0 -column 0 -row 2
grid .l1 -column 1 -row 2
grid .l2 -column 2 -row 2
grid .l3 -column 3 -row 2
grid .l4 -column 4 -row 2
