#!/usr/bin/python
#
# Moo Database Lobotomizer 1.0
# Copyright (C) 2009 Neil Fraser, San Francisco
# http://neil.fraser.name/software/moobrowser/
#
# This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.  http://www.gnu.org/

# Reads in a Moo database and prints it to standard output, replacing
# the clocks, tasks and listeners with empty blocks.

import sys
import os

# Read a line from the database file.
# Write the line to standard out.
def readstr():
  line = file.readline().rstrip("\r\n")
  print line
  return line


# Read an integer from the database file
def readint():
  return int(readstr())


# Read a Moo value from the database file
def readvalue():
  type = readint() # e.g. 1
  if type in (0, 1, 2, 3, 9): # integer, object, string, error, float
    readstr()
  elif type == 4: # list
    elements = readint() # e.g. 2
    while elements > 0:
      readvalue()
      elements -= 1
  # 5 is clear and has no data.


# Read all the lines associated with an object
def readobject():
  objnumber = readstr() # e.g. #5
  objnumber.index("#")  # Blow up if we're out of sync
  if objnumber.find("recycled") != -1:
    return

  readstr() # name
  readstr() # blank line
  readint() # flags
  readint() # owner
  readint() # location
  readint() # first contents
  readint() # next neighbour
  readint() # parent
  readint() # first child
  readint() # next sibling

  # Verb Definitions
  objverbs = readint() # e.g. 6
  while objverbs > 0:
    readstr() # vername
    readint() # verbowner
    readint() # verbpoerms
    readint() # verbprep
    objverbs -= 1

  # Property Names
  objpropnames = readint() # e.g. 4
  while objpropnames > 0:
    readstr() # propname
    objpropnames -= 1

  # Property Definitions
  objprops = readint() # e.g. 12
  while objprops > 0:
    readvalue() # propvalue
    readint() # propowner
    readint() # propperms
    objprops -= 1


# Read all the lines associated with a verb
def readverb():
  verblocation = readstr() # e.g. #5:0
  lastline = readstr()
  while lastline != ".":
    lastline = readstr()


if __name__ == "__main__":
  # Obtain the database's filename from the command line argument
  if len(sys.argv) != 2:
    print >> sys.stderr, "Usage:  " + sys.argv[0] + " <DATABASE>"
    sys.exit(1)
  filename = sys.argv[1]

  # Open the database file
  try:
    file = open(filename, "r")
  except IOError, err:
    print >> sys.stderr, str(err)
    sys.exit(1)

  # Intro Block
  versionstring = readstr() # e.g. ** LambdaMOO Database, Format Version 4 **
  totalobjects  = readint() # e.g. 95
  totalverbs    = readint() # e.g. 1698
  dummy         = readstr() # e.g. 0
  totalplayers  = readint() # e.g. 4

  # Player Block
  while totalplayers > 0:
    dummy = readstr() # e.g. 71
    totalplayers = totalplayers - 1

  # Object Block
  while totalobjects > 0:
    readobject()
    totalobjects = totalobjects - 1

  # Verb Block
  while totalverbs > 0:
    readverb()
    totalverbs = totalverbs - 1

  # Finished reading the database file
  file.close()

  print "0 clocks"
  print "0 queued tasks"
  print "0 suspended tasks"
  print "0 active connections with listeners"
