#!/usr/local/bin/wish -f
#
#	SCCS ID: @(#)install	1.1 95/07/17 09:41:43
#
wm title . "XPHONE INSTALL"

set WISH /usr/local/bin
set CWD [pwd]
pack [frame .1 -relief ridge -bd 2] -fill both -padx 2m -pady 2m

########## Entry widgets 
pack [frame .1.wish] -fill x
pack [label .1.wish.label -text "Wish Interpreter path -> " -width 25 \
	-anchor e] -side left
pack [entry .1.wish.entry -relief sunken -textvariable WISH] \
	-side left -padx 2m -pady 1m -expand 1 -fill x
focus .1.wish.entry
bind .1.wish.entry <Tab> {focus .1.xphone.entry}
bind .1.wish.entry <Return> {focus .1.xphone.entry}

set XPHONE /usr/local/lib
pack [frame .1.xphone] -fill x
pack [label .1.xphone.label -text "Xphone Parent Directory -> " -width 25 \
	-anchor e] -side left
pack [entry .1.xphone.entry -relief sunken -textvariable XPHONE] \
	-side left -padx 2m -pady 1m -expand 1 -fill x
bind .1.xphone.entry <Tab> {focus .1.man.entry}
bind .1.xphone.entry <Return> {focus .1.man.entry}

set MAN /usr/local/man/man1
pack [frame .1.man] -fill x
pack [label .1.man.label -text "Man page Directory -> " -width 25 -anchor e] \
	-side left
pack [entry .1.man.entry -relief sunken -textvariable MAN] \
	-side left -padx 2m -pady 1m -expand 1 -fill x
bind .1.man.entry <Tab> {focus .3.install}
bind .1.man.entry <Return> {focus .3.install}
########## Entry widgets 

########## Status box
pack [frame .2] -padx 2m -pady 2m -fill both
pack [label .2.label -text "STATUS OF INSTALL" -bg grey -relief sunken] -fill x
pack [scrollbar .2.scroll -relief sunken -command ".2.status yview"] -fill y \
	-side right
pack [text .2.status -bd 2 -relief sunken -yscroll ".2.scroll set"] -fill both
.2.status config -width 70 -height 10
########## Status box

########## Button box
pack [frame .3] -padx 2m -pady 2m -fill both
pack [button .3.install -text INSTALL] -padx 2m -pady 2m -fill x -side left \
	-expand 1
pack [button .3.cancel -text CANCEL -command exit] \
	-padx 2m -pady 2m -fill x -side left -expand 1
bind .3.install <Tab> {focus .3.cancel}
bind .3.install <Return> {.3.install flash; .3.install invoke}
bind .3.install <FocusIn> {.3.install config -state active}
bind .3.install <FocusOut> {.3.install config -state normal}
bind .3.cancel <Tab> {focus .1.wish.entry}
bind .3.cancel <Return> {.3.cancel flash; .3.cancel invoke}
bind .3.cancel <FocusIn> {.3.cancel config -state active}
bind .3.cancel <FocusOut> {.3.cancel config -state normal}
.3.install config -command ".3.install config -state disabled;install;\
	.3.install config -state normal"
########## Button box

proc status {message} {
        .2.status config -state normal
        .2.status insert end "$message\n"
        .2.status yview {end -9 lines}
        .2.status config -state disabled
        update
}

proc install {} {
	global WISH MAN XPHONE

	set_cursor watch
	if {! [file readable ./xphone-3.tz]} {
		set msg "./xphone-3.tz is not accessable.\n\tPlease make sure\
			xphone-3.tz is located in current directory."
		status "$msg"
		dialog .d Error "$msg" error 0 OK
		set_cursor arrow
		return 1
	}
		
	foreach dir "$WISH $MAN $XPHONE" {
		# make sure user gives a value.
		if {"$dir" == ""} {
			status "\nUser did not supply a value for $dir.\n"
			dialog .d Error "You must supply a value for $dir" \
                	error 0 OK
			set_cursor arrow
			return 1
		}
		# create the directory if it does not exist
		if {! [file isdirectory $dir]} {
			set answer [dialog .d Question "$dir does not\
                        exist." questhead 0 "CREATE IT" CANCEL]
                	if {$answer} {set_cursor arrow;return}
			status "Creating $dir ..."
			if {[catch {exec mkdir -p $dir} error]} {
			  status "\nFailed to create $dir.\n"
			  dialog .d Error "Failed to create $dir\n$error" \
				error 0 OK
			  set_cursor arrow
			  return 1
			}
		}
	}
	# copy tar ball to XPHONE directory and unpack it.
	status "Copying ./xphone-3.tz to $XPHONE"
	if {[catch {exec cp ./xphone-3.tz $XPHONE} error]} {
		status "\nFailed to copy ./xphone-3.tz to $XPHONE\n"
		dialog .d Error "Failed to copy ./xphone-3.tz to $XPHONE\n\
			$error" error 0 OK
		set_cursor arrow
		return 1
	}
	status "Uncompressing $XPHONE/xphone-3.tz into xphone-3.tar ..."
	cd $XPHONE
	if {[catch {exec cat [pwd]/xphone-3.tz | uncompress > xphone-3.tar} error]} {
		status "\nFailed to uncompress [pwd]/xphone-3.tz into \
			xphone-3.tar\n"
		dialog .d Error "Failed to uncompress [pwd]/xphone-3.tz into \
			xphone-3.tar\n$error" error 0 OK
		set_cursor arrow
		global CWD;cd $CWD
		return 1
	}
	catch {exec rm xphone-3.tz}
	status "Untarring $XPHONE/xphone-3.tar ..."
	if {[catch {exec tar xvf xphone-3.tar} return]} {
		status "\nFailed to untar [pwd]/xphone-3.tar.\n$return\n"
		dialog .d Error "Failed to untar [pwd]/xphone-3.tar." \
			error 0 OK
		set_cursor arrow
		global CWD;cd $CWD
		return 1
	}
	catch {exec rm xphone-3.tar}
	status "$return"

	# Copy the man page to the MAN directory.
	status "Copying xphone.1 man page to $MAN ..."
	cd xphone-3
	if {[catch {exec cp xphone.1 $MAN} error]} {
		status "\nFailed to cp [pwd]/xphone.1 to $MAN\n"
		dialog .d Error "Failed to cp [pwd]/xphone.1 to $MAN\n$error" \
			error 0 OK
	}
	
	# create top template for xphone executable
	status "Modifying the xphone executable ..."
	set id [open ./xphone w]
	puts $id "#!$WISH/wish -f"
	puts $id {# MAKE SURE THAT THE LINE ABOVE POINTS TO YOUR COPY OF WISH}
	puts $id {}
	puts $id {# MAKE SURE THAT THE LINE BELOW POINTS TO WHERE YOU UNPACK \
		THE XPHONE PACKAGE}
	puts $id "set lib $XPHONE/xphone-3"
	puts $id {}
	puts $id {# MAKE SURE THAT THE LINE BELOW POINTS TO WHERE YOU PLACE \
		THE XPHONE.1 MANPAGE}
	puts $id "set man $MAN"
	flush $id
	close $id
	if {[catch {exec cat xphone.template >> xphone} error]} {
		status "\nFailed to modify xphone executable.\n$error\n"
		dialog .d Error "Failed to modify xphone executable.\n$error" \
			error 0 OK
		set_cursor arrow
		global CWD;cd $CWD
		return 1
	}
	catch {exec chmod +x xphone}

	# Link xphone executable to the WISH dir.
	status "Linking [pwd]/xphone to $WISH/xphone ..."
	if {[file exists $WISH/xphone]} {catch {exec rm -f $WISH/xphone}}
	if {[catch {exec ln -s [pwd]/xphone $WISH/xphone} error]} {
		status "\nFailed to link [pwd]/xphone to $WISH/xphone\n"
		status "You should copy [pwd]/xphone to a dir. in your path"
		dialog .d Warning "Failed to link xphone to $WISH\n$error" \
			warning 0 OK
	}
	set_cursor arrow
	dialog .d "SUCCESSFUL INSTALL" "Install complete." info 0 OK
}
		

# dialog procedure taken/enhanced from the Ousterhout book that creates 
# dialog boxes with bitmaps and messages.
proc dialog {w title text bitmap default args} {
	global button

	catch {destroy $w}
	toplevel $w -class Dialog
	wm transient $w .
	wm title $w $title
	wm geom $w +500+400
	wm iconname $w Dialog
	frame $w.top -relief raised -bd 1
	pack $w.top -side top -fill both
	frame $w.bot -relief raised -bd 1
	pack $w.bot -side bottom -fill both

	message $w.top.msg -width 10c -text $text 
	pack $w.top.msg -side right -expand 1 -fill both -padx 3m -pady 3m
	if {$bitmap != ""} {
		label $w.top.bitmap -bitmap $bitmap
		pack $w.top.bitmap -side left -padx 3m -pady 3m
	}

	set i 0
	foreach but $args {
		button $w.bot.button$i -text $but -command "set button $i"
		if {$i == $default} {
			frame $w.bot.default -relief sunken -bd 1
			raise $w.bot.button$i
			pack $w.bot.default -side left -expand 1 \
				-padx 3m -pady 2m 
			pack $w.bot.button$i -in $w.bot.default -side left \
				-padx 2m -pady 2m -ipadx 2m -ipady 1m
		} else {
			pack $w.bot.button$i -side left -expand 1 \
				-padx 3m -pady 3m -ipadx 2m -ipady 1m
		}
		incr i
	}

	if {$default >= 0} {
		bind $w <Return> "$w.bot.button$default flash
		set button $default"
	}
	set oldFocus [focus]
	tkwait visibility $w
	grab set $w
	focus $w

	tkwait variable button
	destroy $w
	focus $oldFocus
	return $button
}

proc set_cursor {cursor} {
	foreach widget [winfo children .] {$widget config -cursor $cursor}
	.2.status config -cursor $cursor
}

status "Awaiting user command to begin install...\n"
