#! /opt/bin/wish

# user settable section

set BACKUP_HOME	$env(BACKUP_HOME)
set found 0
set configfiles "$BACKUP_HOME/lib/backup.conf /etc/buclient.conf /etc/afbackup/client.conf"
lappend configfiles "@clientlibdir@/@clientconf@"
foreach configfile "$configfiles" {
  if { [ file readable $configfile ] } {
    set found 1
    break
  }
}

if { ! $found } {
  puts stderr "Error: Cannot read configuration file. Exiting."
  exit 2
}

set textwidth	30
set entrywidth	40

set num_fix_params	21
set num_var_sp_idx	21

set var_param_idx [ expr $num_fix_params + 2 ]
set fix_param_idx [ expr $num_fix_params + 1 ]

set comments(1) {\The backup server hosts}
set comments(2) {\The backup server port-numbers}
set comments(3) {\The cartridge sets to use}
set comments(4) {\The compress and decompress programs, if desired.}
set comments(5) {}
set comments(6) {\The part of the file, where the saved filenames\are stored (current number appended)}
set comments(7) {\Whether to compress the saved files}
set comments(8) {\Whether to compress the filename logfiles}
set comments(9) {\Patterns for names of files, no compression\is attempted on}
set comments(10) {\The number of such files, that are maintained.\More (older ones) are removed.}
set comments(11) {\The file where to log events}
set comments(12) {\The directory for varying files}
set comments(13) {\The file with the authentication encryption key}
set comments(14) {\The program to save startup information}
set comments(15) {\The program to be run before attempting a backup. If this program\returns an exit status unequal to 0, no backup is performed.}
set comments(16) {\The program to be executed when everything requested is done.\%l is replaced by the filename-logfile, %r by the file with\the report statistics, %e by the overall exit status}
set comments(17) {\The directory, where to run the backup}
set comments(18) {\Names of files to be skipped during the backup\(wildcards are allowed)}
set comments(19) {\Names of directories to be skipped during the backup\(wildcards are allowed)}
set comments(20) {\Name of a file containing a (pattern-) list of files\and directories to be skipped during the backup}
set comments(21) {\The number of parts of the full backup (if this takes a long time)}
set comments(22) {\The subdirectories of the RootDirectory to backup\(wildcards are allowed)}
set comments(23) {\The subdirectories of the RootDirectory to backup, part #num#\(wildcards are allowed)}

set names(1) {BackupHosts}
set names(2) {BackupPorts}
set names(3) {CartridgeSets}
set names(4) {CompressCmd}
set names(5) {UncompressCmd}
set names(6) {IndexFilePart}
set names(7) {CompressBackupedFiles}
set names(8) {CompressLogfiles}
set names(9) {DoNotCompress}
set names(10) {NumIndexesToStore}
set names(11) {LoggingFile}
set names(12) {VarDirectory}
set names(13) {EncryptionKeyFile}
set names(14) {StartupInfoProgram}
set names(15) {InitProgram}
set names(16) {ExitProgram}
set names(17) {RootDirectory}
set names(18) {FilesToSkip}
set names(19) {DirsToSkip}
set names(20) {ExcludeListFile}
set names(21) {NumBackupParts}
set names(22) {DirsToBackup}
set names(23) {DirsToBackup#num#}

set patterns(1) {[Bb]ackup[ \t_-]*[Hh]osts?:?}
set patterns(2) {[Bb]ackup[ \t_-]*[Pp]orts?:?}
set patterns(3) {[Cc]artr?i?d?g?e?[-_ \t]*[Ss]ets?:?}
set patterns(4) {[Cc]ompress[-_ \t]*[Cc]o?m?ma?n?d:?}
set patterns(5) {[Uu]ncompress[-_ \t]*[Cc]o?m?ma?n?d:?}
set patterns(6) {[Ii]ndex[ \t_-]*[Ff]ile[ \t_-]*[Pp]art:?}
set patterns(7) {[Cc]ompress[-_ \t]*[Bb]ackupe?d?[-_ \t]*([Ff]iles)?:?}
set patterns(8) {[Cc]ompress[-_ \t]*[Ll]ogg?i?n?g?[-_ \t]*[Ff]iles:?}
set patterns(9) {[Dd]o[-_ \t]*[Nn][\'o]?t[-_ \t]*[Cc]ompress:?}
set patterns(10) {[Nn]um[-_ \t]*[Ii]nd(ic|ex)es[-_ \t]*[Tt]o[ \t_-]*[Ss]tore:?}
set patterns(11) {[Ll]ogg?i?n?g?[-_ \t]*[Ff]ile:?}
set patterns(12) {[Vv][Aa][Rr][-_ \t]*[Dd]ire?c?t?o?r?y?:?}
set patterns(13) {([Ee]n)?[Cc]rypti?o?n?[ \t_-]*[Kk]ey[ \t_-]*[Ff]ile:?}
set patterns(14) {[Ss]tartu?p?[-_ \t]*[Ii]nfo[-_ \t]*[Pp]rogram:?}
set patterns(15) {[Ii]nit[-_ \t]*[Pp]rogram:?}
set patterns(16) {[Ee]xit[-_ \t]*[Pp]rogram:?}
set patterns(17) {[Rr]oot[ \t_-]*[Dd]ire?c?t?o?r?y?:?}
set patterns(18) {[Ff]iles[ \t_-]*[Tt]o[ \t_-]*[Ss]kip:?}
set patterns(19) {[Dd]ire?c?t?o?r?i?e?s[ \t_-]*[Tt]o[ \t_-]*[Ss]kip:?}
set patterns(20) {[Ee]xclu?d?e?[-_ \t]*[Ll]ist[-_ \t]*[Ff]ile[-_ \t]*[Nn]?a?m?e?:?}
set patterns(21) {[Nn]um[-_ \t]*[Bb]ackup[-_ \t]*[Pp]arts:?}
set patterns(22) {[Dd]ire?c?t?o?r?i?e?s[ \t_-]*[Tt]o[ \t_-]*[Bb]ackup:?}
set patterns(23) {[Dd]ire?c?t?o?r?i?e?s[ \t_-]*[Tt]o[ \t_-]*[Bb]ackup[ \t]*#num#[ \t]*:?}

for { set i 1 } { $i <= $var_param_idx } { incr i } {
  set types($i) s
}
set types(7) b
set types(8) b

set prompts(1) {Backup server hostnames:}
set prompts(2) {Backup server port-numbers:}
set prompts(3) {Cartridge-sets to use:}
set prompts(4) {Compress program:}
set prompts(5) {Decompress program:}
set prompts(6) {Backup logging file part:}
set prompts(7) {Compress saved files:}
set prompts(8) {Compress filename logfiles:}
set prompts(9) {Files not to be compressed:}
set prompts(10) {Number of saved logging files:}
set prompts(11) {Event/error-logging file:}
set prompts(12) {Var-Directory:}
set prompts(13) {Encryption-Key-File:}
set prompts(14) {Startup info logging program:}
set prompts(15) {Program to run before backup:}
set prompts(16) {Program to run at exit:}
set prompts(17) {Working directory:}
set prompts(18) {Names of files to skip:}
set prompts(19) {Names of directories to skip:}
set prompts(20) {File with names to skip:}
set prompts(21) {Number of full backup parts:}
set prompts(22) {Directories to backup:}
set prompts(23) {Directories to backup, part #num#:}

set helps(1) "These are the hostnames of the machines where a server side
of the backup service resides. Some kind of streamer device
must be connected to these machines. The files and directories,
that should be saved, are packed, eventually compressed,
and then sent to the named machines, who writes them to the
connected device. The named machines are tested for service
availability. If a server is busy, the next one is tried.
BackupPorts can be configured in the same order as the host
entries supplied here. The servers in this list may be
separated by whitespace and/or commas. If the backup server
is the same host as the client, the use of the name localhost
is encouraged."
set helps(2) "These are the port numbers on the backup server machines, where
the backup server processes listen. The default is 2988 or the
number found in the file /etc/services (or in NIS if it is
configured). Several ports can be supplied, positionally according
to the backup server hosts supplied in the BackupHosts parameter.
The numbers can be separated by whitespace and/or commas. If
fewer numbers are supplied than backup servers, the default port
2988 applies for the rest. If more port numbers are given, the
superfluous ones are ignored."
set helps(3) "The cartridge sets on the server side to use for backups.
They must bes legal number between 1 and the number of cartridge
sets configured on the appropriate server side. Several sets can
be supplied, positionally according to the backup server hosts
supplied in the BackupHosts parameter. The numbers can be separated
by whitespace and/or commas. If fewer numbers are supplied than
backup servers, the default set # 1 applies for the rest. If more
cartridge set numbers are given, the superfluous ones are ignored."
set helps(4) "If you want your files to be compressed, you can supply the
name of the program that should perform the compression here.
If you do so, you MUST also supply the appropriate decompress-
program. Note that this program may be specified with options
but no shell-like constructions such as pipes, variables or
wildcards. This program must read standard input and write to
standard output."
set helps(5) "The counterpart to the compression program. You must either
supply both compress- and uncompress-program or neither of
them. Like the compress program, the uncompress-program must
read standard input and write to standard output."
set helps(6) "The name of the file where the names of the saved files
are stored. The current number is appended to this filename.
The number is incremented each time a full backup starts."
set helps(7) "This flag specifies, whether the files, that are saved,
should be compressed with the given compression program."
set helps(8) "This flag specifies, whether the filename logging files
should be compressed with the given compression program."
set helps(9) "These patterns or filenames specify files, that no
compression is attempted on. Normally compression is attempted on all
files, and if a file cannot be compressed any further, it is saved
uncompressed. This procedure is unefficient for already compressed
files, so their compression can be suppressed with this parameter.
The value of this parameter must be a list separated by
whitespace. Double quotes may enclose list elements."
set helps(10) "This number determines how many log files of previous full
backups are saved. These files may serve for the restore
of older files than those present in the actual backup.
Of course there must be sufficient space to
hold all the backups. It doesn't help to save all the
saved filenames but not to have them available on tape."
set helps(11) "The name of a file error messages or other notable events
are written to. A dash - stands for no logging."
set helps(12) "The directory, where varying files should be put in.
These files must not be deleted. The information they contain is
necessary for restore."
set helps(13) "The file containing the encryption key for
authenticating
the backup client to the server. This file must contain
at least 5 characters and must not have read permission for
group or world."
set helps(14) "This is the (shell-) command to run to save the startup
information of an incremental or full backup, sometimes
called bootstrap information. This program should read the
standard input and do something reasonable with it, e.g.
append it to some file. The produced information can be
used to recover from a hard crash, when the files are
lost, that are containing the names of the saved files.
Therefore this information should not be saved locally on
the client host, but e.g. on an NFS-mounted filesystem,
a floppy disc or in a mail-file (then this command should
be sth. like: mail someuser)."
set helps(15) "A (shell-) command to be run before a backup is
attempted. If this program returns an exit status unequal to 0,
no backup is performed. This parameter makes only sense when
backup is started remotely, cause in that case no shell-command
can be supplied. If backup is started locally, there is no problem
to run whatever is necessery before the backup explicitely."
set helps(16) "This parameter may specify a (shell-) command to run at
exit time of a full or incremental backup. The following
patterns are replaced as explained:\\
 %l  by the name of the file containing the filelists\\
 %r  by the name of the file containing statistics (this\\
     file is automatically removed after execution of this\\
     program)\\
 %e  by the overall exit status."
set helps(17) "This is the directory, the backup client changes to before
packing the files and directories. Their names should be
supplied relative to this directory, e.g. ./home ."
set helps(18) "These are the names of files, that should not be saved.
Wildcards in the usual manner are allowed (shell-style or
glob-style, furthermore path-patterns in the style of GNU's
find program with option -path. Note, that wildcards also
match directory separators i.e. slashes, e.g. a*d matches
ab/cd). E.g. it does not usually make much sense to back up
object files, as they can be easily reproduced from existing
program sources."
set helps(19) "These are the names of directories, that should not be saved.
Wildcards in the usual manner are allowed (shell-style or
glob-style, furthermore path-patterns in the style of GNU's
find program with option -path. Note, that wildcards also
match directory separators i.e. slashes, e.g. a*d matches
ab/cd). E.g. it does not usually make much sense to back up
the lost+found directory or such only containing object
files, as they can be easily reproduced from existing
program sources."
set helps(20) "A file with the name supplied here can be present in any
directory. It should contain a list of file-/directory-names
(or glob-style patterns), that should be skipped during backup.
Each entry must be in an own line. The given names/patterns are
valid only in the same directory, where the file resides. Thus
each directory can have it's individual exclusion list."
set helps(21) "If you have to backup a large amount of files and the
full backup can't be done during one run (e.g. over a
weekend), you can divide the full backup into pieces.
This number determines, how many pieces you need. If
this number is not equal to 1, you have to supply which
files and directories you want to save in which piece.
You do so by setting the parameters appearing after you
have entered a number greater than 1 here and hit Return."
set helps(22) "These are the names of files and diretories, that should be
saved. Wildcards in the usual manner are allowed (shell-
style or glob-style). They should be supplied relative to
the working directory, the client changes to when starting.
Descending into directories can be limited to the actual
filesystem by preceding the filename with the four characters .//. or
the option -m (and a space). The prefix .//. is stripped off
the name before saving. Supplying a filename preceded with
the four characters /../ (what makes no sense normally) or
the option -r (and a space) forces the file contents to be
saved regardless of the file type. This way raw partitions
or similar things can be saved. The prefix /../ is stripped
off the name before saving. These file contents are by default
never compressed for safety reasons. If you want to force
compression nonetheless, use //../ as prefix or precede the
name with the option -R (and a space)."
set helps(23) "These are the names of files and diretories, that should
be saved as part #num#. Wildcards in the usual manner are
allowed (shell-style or glob-style). They should be
supplied relative to the working directory the client
changes to when starting. Descending into directories can
be limited to the actual filesystem
by preceding the filename with the four characters .//. or
the option -m (and a space). The prefix .//. is stripped off
the name before saving. Supplying a filename preceded
with the four characters /../ (what makes no sense normally)
forces the file contents to be saved regardless of the file
type. This way raw partitions or similar things can be saved.
The prefix /../ is stripped off the name before saving. These
file contents are by default never compressed for safety
reasons. If you want to force compression nonetheless, use
//../ as prefix."

# end of user settable section

set var_param_idx [ expr $num_fix_params + 2 ]
set fix_param_idx [ expr $num_fix_params + 1 ]

proc replSubstring { string to_repl replacement } {
  set new ""
  set repllen [ string length $to_repl ]

  while { $string != "" } {
    set idx [ string first $to_repl $string ]
    if { $idx >= 0 } {
      append new [ string range $string 0 [ expr $idx - 1 ] ]
      append new $replacement
      set string [ string range $string [ expr $idx + $repllen ] end ]
    } else {
      append new $string
      break
    }
  }

  return $new
}

proc getparam { pat } {
  global configfile

  set pattern {^[ \t]*}
  append pattern $pat
  append pattern {[ \t]*}


  set errfl [ catch { set fp [ open $configfile r ] } ]
  if { $errfl } {
    puts stderr "Error: cannot open $configfile."
    exit 0
  }

  while { [ gets $fp line ] >= 0 } {
    if { [ regexp -indices $pattern $line i1 ] } {
      close $fp
      set idx [ expr [ lindex $i1 1 ] + 1 ]
      return [ string trim [ string range $line $idx end ] ]
    }
  }

  close $fp
  return ""
}

for { set i 1 } { $i <= $num_fix_params } { incr i } {
  set values($i) [ getparam $patterns($i) ]
}
if { $num_var_sp_idx > 0 } {
  set num_var_params $values($num_var_sp_idx)
} else {
  set num_var_params 0
}
if { $num_var_params > 0 } {
  if { $num_var_params > 1 } {
    for { set i 0 } { $i < $num_var_params } { incr i } {
      set l [ expr $num_fix_params + $i + 1 ]
      set pattern [ replSubstring $patterns($var_param_idx) {#num#} [expr $i + 1] ]
      set values($l) [ getparam $pattern ]
    }
  } else {
    set values($fix_param_idx) [ getparam $patterns($fix_param_idx) ]
  }
}

proc what_now { } {
  tk_dialog .help "Help" "Make all the modifications in the entry fields on the right side you need after clicking once into the desired field. To get help on a specific item press F1 after clicking into the field." "" -1 Ok
}

proc show_win { } {
  global	num_fix_params values prompts num_var_sp_idx
  global	var_param_idx fix_param_idx types num_var_params
  global	textwidth entrywidth

  if { $num_var_sp_idx > 0 } {
    set num_var_params $values($num_var_sp_idx)
    if { $num_var_params < 1 } {
      set num_var_params 1
      set values($num_var_sp_idx) 1
    }
  } else {
    set num_var_params 0
  }

  catch { destroy .top }
  frame .top
  frame .top.mbar -relief raised -bd 2
  menubutton .top.mbar.help -text Help -underline 0 -menu .top.mbar.help.menu
  menu .top.mbar.help.menu
  .top.mbar.help.menu add command -label "What now" -command what_now -underline 0
  pack .top.mbar.help -side right
  pack .top.mbar -in .top -side top -fill x
  for { set i 1 } { $i <= $num_fix_params } { incr i } {
    frame .top.line$i
    label .top.line$i.label -width $textwidth -text $prompts($i)
    case $types($i) in {
      s {
	entry .top.line$i.value -width $entrywidth -textvariable values($i)
      }
      b {
	checkbutton .top.line$i.value -onvalue 1 -offvalue 0 -variable values($i)
      }
    }
    if { $i == $num_var_sp_idx } {
      bind .top.line$i.value <Return> show_win
    }
    bind .top.line$i.value <F1> "help_on $i"
    pack .top.line$i.label .top.line$i.value  -side left -fill x
    pack .top.line$i -in .top -side top -anchor w -fill x
  }

  if { $num_var_params > 0 } {
    if { $num_var_params > 1 } {
      for { set i 0 } { $i < $num_var_params } { incr i } {
        set l [ expr $num_fix_params + $i + 1 ]
	if { ! [ info exists values($l) ] } {
	  set values($l) ""
	}

        frame .top.line$l
        set prompt [ replSubstring $prompts($var_param_idx) {#num#} [expr $i + 1] ]
        label .top.line$l.label -width $textwidth -text $prompt
        case $types($var_param_idx) in {
          s {
	    entry .top.line$l.value -width $entrywidth -textvariable values($l)
          }
          b {
	    checkbutton .top.line$l.value -onvalue 1 -offvalue 0 -variable values($l)
          }
        }
        bind .top.line$l.value <F1> "help_on $l"
        pack .top.line$l.label .top.line$l.value  -side left -fill x
        pack .top.line$l -in .top -side top -anchor w -fill x
      }
    } else {
      frame .top.line$fix_param_idx
      label .top.line$fix_param_idx.label -width $textwidth -text $prompts($fix_param_idx)
      case $types($fix_param_idx) in {
        s {
	  entry .top.line$fix_param_idx.value -width $entrywidth -textvariable values($fix_param_idx)
        }
        b {
	  checkbutton .top.line$fix_param_idx.value -onvalue 1 -offvalue 0 -variable values($fix_param_idx)
        }
      }
      bind .top.line$fix_param_idx.value <F1> "help_on $fix_param_idx"
      pack .top.line$fix_param_idx.label .top.line$fix_param_idx.value  -side left -fill x
      pack .top.line$fix_param_idx -in .top -side top -anchor w -fill x
    }
  }

  frame .top.cmds
  button .top.cmds.save -text "Save" -command save_settings
  button .top.cmds.cancel -text "Exit" -command "exit 0"
  pack .top.cmds.save .top.cmds.cancel -side left
  pack .top.cmds -in .top -side top
  pack .top
}

proc save_settings { } {
  global	names values comments configfile num_var_sp_idx
  global	fix_param_idx var_param_idx num_fix_params num_var_params

  set errfl [ catch { set fp [ open $configfile w ] } ]

  if { $errfl } {
    tk_dialog .error "Error" "Error cannot open $configfile for writing." -1 Ok
    return
  }

  for { set i 1 } { $i <= $num_fix_params } { incr i } {
    set comment "# "
    append comment [ replSubstring $comments($i) "\\" "\n# " ]
    puts $fp $comment
    if { [ string length [ string trim $values($i) ] ] == 0 } {
      puts $fp "#$names($i):\t\t$values($i)"
    } else {
      puts $fp "$names($i):\t\t$values($i)"
    }
  }
  if { $num_var_params > 0 } {
    if { $num_var_params > 1 } {
      for { set i 0 } { $i < $num_var_params } { incr i } {
	set comment "# "
	append comment [ replSubstring $comments($var_param_idx) "\\" "\n# " ]
	set comment [ replSubstring $comment {#num#} [ expr $i + 1 ] ]
	set l [ expr $num_fix_params + 1 + $i ]
	puts $fp $comment
	set name [ replSubstring $names($var_param_idx) {#num#} [ expr $i + 1 ] ]
	if { [ string length [ string trim $values($l) ] ] == 0 } {
	  puts $fp "#${name}:\t\t$values($l)"
	} else {
	  puts $fp "${name}:\t\t$values($l)"
	}
      }
    } else {
      set comment "# "
      append comment [ replSubstring $comments($fix_param_idx) "\\" "\n# " ]
      puts $fp $comment
      if { [ string length [ string trim $values($fix_param_idx) ] ] == 0 } {
	puts $fp "#$names($fix_param_idx):\t\t$values($fix_param_idx)"
      } else {
	puts $fp "$names($fix_param_idx):\t\t$values($fix_param_idx)"
      }
    }
  }

  close $fp
}

proc help_on { idx } {
  global	helps num_fix_params var_param_idx values
  global	fix_param_idx num_var_sp_idx num_var_params

  if { $idx > $num_fix_params } {
    if { $num_var_params > 1 } {
      set num [ expr $idx - $var_param_idx + 2 ]
      set msg [ replSubstring $helps($var_param_idx) {#num#} $num ]
    } else {
      set msg $helps($fix_param_idx)
    }
  } else {
    set msg $helps($idx)
  }
  set msg [ replSubstring $msg "\n" " " ]
  set msg [ replSubstring $msg "\\" "\n" ]
  tk_dialog .help "Help" $msg "" -1 Ok
}

show_win

# Workaround for strange wish behaviour: window to small
update idletasks
set h [ winfo height .top.cmds.save ]
if { $h < 20 } {
  show_win
}
