# xgu  :  Tcl/Tk script for SHELXL programs
# by   :  J. Reibenspies, Texas A & M University
# date :  Dec. 1997, March 1998, October 2004, August 2007
# all rights reserved.
# disclaimer :  The author and Texas A & M University
# are not responsible for any damages that are the
# result of use of this program.  
# USE AT YOUR OWN RISK
# Feel free to modify this text but please do not
# distribute modified scripts (with the name xgu)
# 
# Installation
#     Unix insert this line at the top of the script
#    for unix platforms (other than LINUX)   
#    #!/usr/bin/wish
# use chmod to change the execution status of the script.
#    e.g.   chmod +x xgu
# copy xgu to the directory containing your project and click the icon or
# type the script name :: xgu 
# hint : debug with the command line >>  sh xgu
#
# Red Linux version (#\ and exec wish line)
#\
exec wish "$0" "$@"
wm title . "gux adapted from xgu (1.0.0b)    CMSL 2007"
frame .top -borderwidth 20
pack .top -side top -fill x
button .top.quit -text EXIT -command exit
set but [button .top.run -text DIR -command dir]
button .top.xs -text XS -command xs
button .top.xls -text XL -command xls
button .top.plot -text XP -command plot
button .top.xsl -text XSL -command xsl
# button .top.wd -text WD -command wd
button .top.ed -text ED -command ed
button .top.edi -text EDI -command edi
button .top.edr -text EDR -command edr
button .top.pl -text PL -command pl
button .top.rm -text XT  -command xt
button .top.cdd -text CD   -command cdd
button .top.sp -text S+ -command sp
button .top.edl -text EDL -command edl

# pack .top.xs .top.xls .top.plot .top.pl .top.wd .top.ed .top.edi .top.edr \
#     .top.run .top.rm .top.cdd .top.quit -side left 

pack .top.sp .top.xs .top.xls .top.plot .top.xsl .top.pl .top.ed .top.edi .top.edr \
     .top.edl .top.run .top.cdd .top.quit -side left
label .top.l -text ": "  -padx 0
entry .top.cmd -width 10 -relief sunken \
               -textvariable command
pack .top.l -side left
pack .top.cmd -side left -fill x -expand true
bind .top.cmd <Return> dir
bind .top.cmd <Control-c> Stop
focus .top.cmd
frame .t
set log [text .t.log -width 80 -height 30 \
         -borderwidth 2 -relief raised -setgrid true \
         -yscrollcommand {.t.scroll set}]
scrollbar .t.scroll -command {.t.log yview}
pack .t.scroll -side right -fill y
pack .t.log -side left -fill both -expand true
pack .t -side top -fill both -expand true
$log insert end \
"\Hello :  To start, move mouse to the empty box, click and type in\
 the project name\n"
$log insert end \
"\HINT : Use DIR to list the files and the highlight the INS file.\n
Move the cursor to the action box and the press the middle mouse key and\
then delete the .ins extension\n"
$log insert end "\nPoint to the action button (XS,XL, etc.) to use.  Some commands\
such as XL and XS are not interactive.\n"
$log insert end "Information from these commands are spooled\
to the output after completion, so be patient.\n"
$log insert end "\nS+=splus, XS=solve, XL=refine, XP=xp, XSL=xshell, PL=platon, ED=edit, EDI=edit ins\
EDR=edit res, \nEDL=edit lst, DIR=directory, CD=change directory\n"
#
proc pl {} {
  global command input log but
  set input [exec xterm -e platon $command.ins]
  $log insert end $input\n
}
proc ed {} {
  global command input log but
  if [catch {open "|kedit $command"} input ] {
    $log insert end $input\n
  } else {
    fileevent $input readable Log
    $log insert end $command\n
    $but config -text Stop -command Stop
  }
}
proc edi {} {
  global command input log but
  if [catch {open "|kedit $command.ins"} input ] {
    $log insert end $input\n
  } else {
    fileevent $input readable Log
    $log insert end $command\n
    $but config -text Stop -command Stop
  }
}
proc edr {} {
  global command input log but
  if [catch {open "|kedit $command.res"} input ] {
    $log insert end $input\n
  } else {
    fileevent $input readable Log
    $log insert end $command\n
    $but config -text Stop -command Stop
  }
}

proc edl {} {
  global command input log but
  if [catch {open "|kedit $command.lst"} input ] {
    $log insert end $input\n
  } else {
    fileevent $input readable Log
    $log insert end $command\n
    $but config -text Stop -command Stop
  }
}

proc xsl {} {
  global command input log but
  if [catch {open "|xshell $command.res"} input ] {
    $log insert end $input\n
  } else {
    fileevent $input readable Log
    $log insert end $command\n
    $but config -text Stop -command Stop
  }
}

proc xs {} {
  global command input log but
  if [catch {open "|xs $command |& cat"} input ] {
    $log insert end $input\n
  } else {
    fileevent $input readable Log
    $log insert end $command\n
    $but config -text Stop -command Stop
  }
}
proc xls {} {
  global command input log but
  if [catch {open "|xl $command |& cat"} input ] {
    $log insert end $input\n
  } else {
    fileevent $input readable Log
    $log insert end $command\n
    $but config -text Stop -command Stop
  }
}
proc plot {} {
  global command input log but
  set input [exec xp $command]
  $log insert end $input\n
}

proc sp {} {
  global command input log but
  set input [exec splus $command]
  $log insert end $input\n
}

# proc wd {} {
#  global command input log but
#  set input [exec xterm -e weed $command]
#  $log insert end $input\n
# }
 
proc cdd {} {
  global command input log but paths
  set pwd [pwd]

  set input [cd $command]
  $log insert end $input\n
  set pw2 [pwd]
  set input "Change Dir from $pwd to $pw2"

  $log insert end \n$input\n
  
}
proc dir {} {
  global command input log but
  if [catch {open "| ls -px |& cat"} input ] {
    $log insert end $input\n
  } else {
    fileevent $input readable Log
    $log insert end $command\n
    $but config -text Stop -command Stop
  }
}

# proc xt {} {
#   global command input log but
#   set input [exec xterm ]
#  $log insert end $input\n
# }

proc Log {} {
   global input log
   if [eof $input] {
     Stop
   } else {
     gets $input line
     $log insert end $line\n
     $log see end
   }
}
proc Stop {} {
    global input but
    catch {close $input}
    $but config -text DIR -command dir
}
