#!/usr/bin/python
#
# Tree Filter
#
# Written by Stelios Xanthakis <axanth@tee.gr>
#
#  Input from stdin,
#   if input is in the form of ls -lR
#   then the output will be a 'graphic-like' tree
#   if input is not that then i cannot predict the output
#
#  It might take some time to do the tree.
#   and that is because it has to read ALL of stdin
#   and THEN produce the tree.
#
#  Thanx to the guy who wrote python (www.python.org for more)
#   for making a c00l - high level - elegant - neat language.
#   It was heaven compared to a previous version i wrote in awk!

#-----Function : split a string to a dirs list
#
def split(x):
    s, e, lis = 0, 0, []
    for e in range(len(x)):
        if x[e] == '/':
         lis.append(x[s:e])
         s = e+1
    lis.append(x[s:])
    return lis

#-----Function : countslash. Count the number of '/' in a string
def countslash(x):
    s, a = 1, 0
    for a in x:
        if a == '/':
         s = s+1
    return s

#----Main script
import sys, string
BRANCHWIDTH=3
branchok = '|'+BRANCHWIDTH*' '
branchno = ' '+BRANCHWIDTH*' '
branchext = BRANCHWIDTH*'-'
depth, showFiles, showSizes, counttot = 20, 0, 0, 0

# Check and get the options from the arguments
tp=sys.argv
for i in range(len(tp)):
    if tp[i][0] == '-':
     if tp[i][1] == 'd':
      if len(sys.argv) == i+1:
       sys.stderr.write("No number specified for depth\n");
       sys.exit(1)
      i = i + 1
      depth = string.atoi(tp[i])
     else:
      for a in tp[i]:
          if a == 'f':
           showFiles = 1
          if a == 's':
           showSizes = 1
          if a == 't':
           counttot = 1

# now from stdin get : directories, sizes and count files per dir
sz, total, pnt, dirs, nums, sizes = 0, 0, 0, [], [], []
i = sys.stdin.readline ()
if counttot == 1:
 while i != '':
  if i[0] != '\n':
   if i[-2] == ':':
    if countslash(i[:-2]) <= depth:
     dirs.append(i[:-2])
     nums.append(pnt)
     sizes.append(sz)
     pnt, sz = 0, 0
   elif i[0] == 't':
    x = string.atoi(i[6:][:-1])
    sz = sz + x
    total = total + x
   else:
    pnt = pnt+1
  i = sys.stdin.readline ()       
else:
 while i != '':
  if i[0] != '\n':
   if i[-2] == ':':
    if countslash(i[:-2]) <= depth:
     dirs.append(i[:-2])
     nums.append(pnt)
     sizes.append(sz)
    pnt = 0
   elif i[0] == 't':
    sz = string.atoi(i[6:][:-1])
   else:
    pnt = pnt+1
  i = sys.stdin.readline ()

nums.append(pnt)
sizes.append(sz)
del sizes[0]
del nums[0]

# Create a list of tuples containing tree branches
tuplist = [()]
for kk in range(len(dirs)):
    t = tuplist[-1]
    pnt = countslash(dirs[-kk-1])
    if pnt > len(t):
     for j in range(len(tuplist[-1]), pnt-1):
         t = t+(0,)
     t = t+(1,)
     tuplist.append(t)
    elif pnt == len(t):
     tuplist.append(t)
    else:
     tuplist.append(t[:-2]+(t[-1],))

# Ok now print out the TREE!
pnt = len(tuplist)-1
for j in range(pnt):
    kk, st = pnt - j, ''
    for jj in tuplist[kk][:-1]:
        if jj == 1:
         st = st + branchok
        else:
         st = st + branchno
    if len(tuplist[kk]) > len(tuplist[kk-1]):
     st = st+'\\'
    else:
     if tuplist[kk-1][len(tuplist[kk])-1] == 0:
      st = st+'\\'
     else:
      st = st+'+' 
    st = st+branchext+split(dirs[j])[-1]
    if showSizes == 1:
     st = st+'\t ('+str(sizes[j])+' kB)'
    if showFiles == 1:
     st = st+'\t ('+str(nums[j])+')'
    print st
if counttot == 1:
 print 'Total under Tree:', total, 'kB'
# Say goodbye..
