# -*- Tcl -*-

# Group handling functions

proc load_groupdb { } {
  global kadmin group

  # Assumes the simple format of one line per group, of format:
  #   groupname: princ1 princ2 princ3 princ4 ...
  catch {
    set F [open $kadmin(groupdb) r]
    while {[gets $F str] != -1} {
      if {[regexp {^([^:]+): *(.*)$} $str dummy gname glist]} {
	set group($gname) $glist
      }
    }
    close $F
  }
}

proc save_groupdb { } {
  global kadmin group

  # Writes out the group file.
  set F [open $kadmin(groupdb) w]
  foreach g [array names group] {
    if {$g != ""} {
      puts $F "$g: $group($g)"
    }
  }
  close $F
}

proc populate_groups { } {
  global group

  if {! [winfo exists .groups]} {
    create_win_groups
  }

  # populate the groups window
  .groups.list.lb delete 0 end
  foreach g [array names group] {
	if {$g != ""} {
    .groups.list.lb insert end $g
}
  }
}

proc show_group_info { name } {
  global group gname
  
  # save old information, if given
  if {$gname != ""} {
    group_dismiss
  }

  # fill in data
  set gname $name
  .groups.names.lb delete 0 end

  if {[info exists group($name)]} {
    foreach p [lsort $group($name)] {
      .groups.names.lb insert end $p
    }
  } 
}

proc add_group { } {
  show_group_info ""
}

proc group_dismiss { } {
  global group gname
#  if {$gname == ""} {
#    tk_dialog .dialog {Empty Group Name} \
#	{Please provide a name for this group.} "" 0 OK
#  } else { 
    # read in list and stick it into the group
    set group($gname) ""
    for {set i 0} {$i < [.groups.names.lb size]} {incr i} {
      lappend group($gname) [.groups.names.lb get $i]
    }
#  }
  save_groupdb
}

# commands for editing groups
proc create_win_groups { } {
  global gname
  set gname ""

  # create and open the group window
  toplevel .groups
  wm title .groups "Group Listing"

  frame .groups.list
  frame .groups.names

  scrollbar .groups.list.sb -orient vertical -relief sunken \
      -command ".groups.list.lb yview"
  listbox .groups.list.lb -selectmode single -width 25 -relief sunken \
      -yscrollcommand ".groups.list.sb set"

  scrollbar .groups.names.sb -orient vertical -relief sunken \
      -command ".groups.names.lb yview"
  listbox .groups.names.lb -selectmode extended -width 25 -relief sunken \
      -yscrollcommand ".groups.names.sb set"
  create_labeled_entry .groups.names.add \
	  "Add Name:" "" \
	  newname
  bind .groups.names.add.e <Return> { 
    .groups.names.lb insert end $newname; set newname ""; group_dismiss }
  bind .groups.names.add.e <Tab> { 
    .groups.names.lb insert end $newname; set newname ""; group_dismiss }

  bind .groups.list.lb <1> {
    group_dismiss
    show_group_info [.groups.list.lb get @%x,%y]
  }

  pack .groups.list.sb -side right -fill y
  pack .groups.list.lb -side left -expand yes -fill both
  pack .groups.names.add -side bottom -fill x
  pack .groups.names.sb -side right -fill y
  pack .groups.names.lb -side left -expand yes -fill both

  pack .groups.list .groups.names -side left -expand yes -fill both
  populate_groups
}

