#!/usr/bin/wish
# andrew K (C) 2005

# Welcome to tkDolTool. These are the comments. The interface is a bit ugly, but it works.
# The options here are fairly obvious; the IP addresses of your gamecube and your
# PC, and the port your gamecube is listening on if you are sending via netcat. 
# Type in or browse to the location of the DOL to upload. The program will block
# while trying to load the DOL, and will display the results when finished.
# Obviously, all the appropriate programs need to be in your path. (nc and psoload2)
# if you're running on windows, change these to nc.exe and psoload2.exe - 

# This is just a quick hack to bring DOL loading out of the command line and into the
# GUI. I mainly did it because I wanted to smack a "reload" button after doing a compile,
# without having to be in a bash shell.

# This app saves your settings in your $HOME/ directory in $HOME/.tkDolTool

# execute with wish, the interpreter for the TK extension to the TCL language.
# get it at http://www.tcl.tk/software/

set globalWelcome {Welcome to tkDolTool v 1.0 (AndrewK). Please read the source comments.}
set dolLoadMethod 0

wm withdraw .
set mainWin [toplevel .mainWin]
wm title .mainWin {tkDolTool : X11 Gamecube DOL Loader}

set fileFrame [frame $mainWin.fileFrame]
pack [label $fileFrame.dolFNLabel -text {DOL Filename}] -side left
pack [entry $fileFrame.dolFileName] -side left -fill x -expand true
pack [button $fileFrame.dolSelect -text {Browse ...} -default active -command {
    $fileFrame.dolFileName delete 0 9999
    $fileFrame.dolFileName insert 1 [tk_getOpenFile -title {Please select a DOL to load ...} -initialfile [$fileFrame.dolFileName get]] }] -side right
pack $fileFrame

set settingsFrame [frame $mainWin.settingsFrame]
pack [label $settingsFrame.gcIPAddrLabel -text {GCN IP: }] -side left
pack [entry $settingsFrame.gcIPAddr ] -side left
pack [label $settingsFrame.pcIPAddrLabel -text {PC IP: }] -side left
pack [entry $settingsFrame.pcIPAddr ] -side left
pack [label $settingsFrame.gcNetcatPortLabel -text {GCN Port:}] -side left
pack [entry $settingsFrame.gcNetcatPort ] -side left
pack $settingsFrame

set outputFrame [frame $mainWin.outputFrame] 
pack [text $outputFrame.commandOutput -height 15 -width 80 -wrap word]
$outputFrame.commandOutput insert end "\t$globalWelcome\n\nCommand output:"
pack $outputFrame

set buttonFrame [frame $mainWin.buttonFrame]
pack [frame $buttonFrame.rbuttons -relief groove -bd 3] -side left
pack [label $buttonFrame.rbuttons.loadMethod -text {Load method:}] -side left
pack [radiobutton $buttonFrame.rbuttons.psoLoad -text {PSOLoad} -variable dolLoadMethod -value 0] -side left
pack [radiobutton $buttonFrame.rbuttons.psoReload -text {PSO Reload} -variable dolLoadMethod -value 1] -side left
pack [radiobutton $buttonFrame.rbuttons.netcat -text {Netcat} -variable dolLoadMethod -value 2] -side left
pack [button $buttonFrame.closeButton -text {Exit} -default normal -command {
    saveSettings $dolLoadMethod [$settingsFrame.gcIPAddr get] [$settingsFrame.pcIPAddr get] [$settingsFrame.gcNetcatPort get] [$fileFrame.dolFileName get]
    exit 0 }] -side right
pack [button $buttonFrame.loadButton -text {Load} -default normal -command { 
    $outputFrame.commandOutput insert end "\n\nBlocking until completion of load command!\n\n"
    loadDOL $dolLoadMethod [$settingsFrame.gcIPAddr get] [$settingsFrame.pcIPAddr get] [$settingsFrame.gcNetcatPort get] [$fileFrame.dolFileName get] $outputFrame.commandOutput} ] -side right
pack $buttonFrame -before $outputFrame

proc loadDOL { loadmethod gcip pcip port dolfile output} {
    set result ""
    set command ""
    
    if {$loadmethod == 0} {
        set result [exec psoload2 -i $pcip $dolfile]
    }
    if {$loadmethod == 1} {
        set result [exec psoload2 -R $gcip $dolfile]
    }
    if {$loadmethod == 2} {
        set result [exec nc $gcip $port -s $pcip < $dolfile]
    }
    $output insert end $result
}

proc saveSettings { loadmethod gcip pcip port dolfile } {
    # save the user's current settings for later
    cd
    set confout [open ".tkDolTool" w]
    puts $confout $loadmethod
    puts $confout $gcip
    puts $confout $pcip
    puts $confout $port
    puts $confout $dolfile
    close $confout
    return
}

proc loadSettings { loadmethod gcip pcip port dolfile } {
    #load any previous settings
    cd
    set confin [open ".tkDolTool" a+]
    seek $confin 0 start
    gets $confin tmpdata
    if { [string length $tmpdata] == 0 } {
        # there are no previous settings, this is the first run
        close $confin
        return
    }
    set loadmethod $tmpdata
    gets $confin tmpdata
    $gcip insert end $tmpdata
    gets $confin tmpdata
    $pcip insert end $tmpdata
    gets $confin tmpdata
    $port insert end $tmpdata
    gets $confin tmpdata
    $dolfile insert end $tmpdata
    close $confin
    return
}


loadSettings $dolLoadMethod $settingsFrame.gcIPAddr $settingsFrame.pcIPAddr $settingsFrame.gcNetcatPort $fileFrame.dolFileName
