#!../wafe --f
# This is a small example script to demonstrate how the 
# submit callback of an HTML Form can be used in a shell-like
# manner to execute arbitrary tcl commands.
#
# This demo-backup script allows a use to choose between
#
#  - various backup sources
#  - various backup methods
#  - various backup devices
# 
# Note, that the HTML-page is here specified within this script.
# In general it could be obtained from various other sources
# as well. The HTML document could be part in a general
# System administration tool and contain references from and to
# other HTML pages.
#
# Gustaf Neumann                  Mon Nov 22 07:21:29 MET 1993

sV topLevel geometry +0+0
mergeResources topLevel \
    *background gainsboro \
    *HTML*Command*background wheat \
    *errors*background pink \
    *errors*Text*background gray95

set stamp [exec date +%y-%m-%d]      

set text {
<H1>Backup Administration Tool</H1>

<H3>Make a backup of of...</H3>

<FORM action="backup">
<UL>
<LI> 
   <INPUT type=radio name=source value=home/neumann CHECKED>
   <i>Gustaf's</i> home directory

<LI> 
   <INPUT type=radio name=source value=home/alkier>
   <i>Lore's</i> home directory

<LI> 
   <INPUT type=radio name=source value="home/ usr/spool/mail/">
   <i>All</i> home directories and mail boxes

<LI> 
   <INPUT type=radio name=source 
   value="usr/local/bin etc/rc.local usr/lib/X11/Xconfig usr/lib/X11/xinit/xinitrc">
   locally installed files and certain configuration files
</UL>

<H3>... using as an archive program ...</H3>
<UL>
<LI> 
   <INPUT type=radio name=archiver 
          value="tar zcf \$target \$source" CHECKED>
   <i>Gnu Tar</i> (compressed)
<LI> 
   <INPUT type=radio name=archiver 
          value="find \$source | cpio -oa &gt \$target ">
   <i>cpio</i> 
</UL>

<H3> ... on  ... </H3>
<UL>
<LI> 
   <INPUT type=radio name=target value="/dev/fd0" CHECKED>
   the <i>first</i> floppy disk drive, or
<LI> 
   <INPUT type=radio name=target value="/dev/mt0">
   on the <i>tape</i> drive, or
<LI> 
   <INPUT type=radio name=target value="/usr/backups/backup-$stamp">
   in the <i>backup directroy</i> on the hard disk
</UL>

<HR>

In order to 
<UL>
<LI> 
   <INPUT type=radio name=show value=0>
   <i>start</i> the backup, or to
<LI> 
   <INPUT type=radio name=show value=1 CHECKED>
   <i>show</i> the backup command,
</UL>
press: <INPUT type=submit value=Backup>
</FORM>
}

proc submit {action name value} {
#    puts stderr "<$action> <$name> <$value>"

    if [string match "backup" $action] {
      # set tcl variables according to the name value pairs
      for {set i [expr [llength $name]-1]} {$i > -1} {incr i -1} {
	# puts "setting <[lindex $name $i]> to <[lindex $value $i]>"
	set [lindex $name $i] [lindex $value $i]
      }
      backup [eval concat $archiver] $show
    }
}

proc backup {cmd show} {
#  puts stderr "cmd=<$cmd> $show"

  if $show {
    sV info label $cmd
  } else {
    setBusy f true
    cd /
    catch {eval exec $cmd} result
    setBusy f false
    if [string compare $result ""] {
      if ![isWidget errors] {
	TransientShell errors topLevel
	Form errorForm errors
	Text errorText errorForm width 600 height 300 string $result
	Command dismiss errorForm fromVert errorText \
	  callback {popdown errors}
	callback errors popupCallback position info:40
      } else {
	sV errorText string $result
      }
      popup errors none
    }
  }
}

 Form f topLevel
   Label info f shadowWidth 3 width 450 label {}
   HTML h f \
    width 450 height 650 fromVert info \
    text $text \
    submitFormCallback { submit "%h" "%n" "%v" }

  Command quit f \
    fromVert h \
    callback quit

realize
