#!/usr/bin/tclsh

#
# pui-archivetodos: mark all "completed" todos for archive/deletion.
#
# THIS IS SAMPLE CODE ONLY
#
# Copyright (c) 1996, Scotter <scotter@iname.com>
#
# This is free software, licensed under the GNU Public License V2.
#

if {[llength $argv] == 1} {

  load tclpui.so

  # Open file read-write
  set puidb [puiOpen [lindex $argv 0] "w"]
  # Loop through all records
  set id [puiFirst $puidb]
  while {$id != 0} {
    puiRead $puidb $id record
    # Skip already-deleted records
    if {!($record(attribute) & 0x80)} {
      # Check to see if item has been completed
      if {$record(complete)} {
        # Set the "delete" and "archive" attribute flags
        set record(attribute) [expr $record(attribute) | 0x88]
        # Write modified record back to database.
        puts "Archiving: $record(description)"
        puiWrite $puidb $id record
      }
    }
    set id [puiNext $puidb $id]
  }
  puiClose $puidb

} {
  puts "pui-archivetodos <puidb>"
  puts "Mark all \"completed\" todos for archiving and deletion."
  exit -1
}

exit 0
