#!/usr/bin/wish

package require Tk

wm title . "LilyPond Contributor's GUI"
button .update -text "Clone/Update LilyPond" -command update_lilypond
text .output -width 80 -height 15
pack .update .output

set lily_dir $env(HOME)/lilypond

proc write_to_output {f} {
	if {[eof $f]} {
		global git_command
		fconfigure $f -blocking true
		if {[catch {close $f} err]} {
			tk_messageBox -type ok -message "Git aborted: $err"
		}
		unset git_command
	} else {
		.output insert end [read $f 24]
	}
}

proc call_git {args} {
	global lily_dir git_command
	set git_command [linsert $args 0 "|git" "--git-dir=$lily_dir/.git"]
	set git_command "$git_command 2>@1"
	#.output insert end "$git_command\n"
	set git [open $git_command r]
	fconfigure $git -blocking false
	fileevent $git readable [list write_to_output $git]
	vwait git_command
}

proc update_lilypond {} {
	global lily_dir
	if {![file exists $lily_dir]} {
		file mkdir $lily_dir
		call_git init
		call_git config core.bare false
		call_git remote add -t master \
			origin git://repo.or.cz/lilypond.git
		call_git fetch --depth 1
	} else {
		call_git pull
	}
}


