#!/usr/bin/env python
# -*- coding: utf-8 -*-

FICHIER_MUNIN = '/var/lib/munin/tmp/wiki_pages'

import shutil
import sys
import tempfile

try :
    arg = sys.argv[1]
except :
    arg = ''

if arg == "config" :
        print 'host_name web.wiki'
        print 'graph_category wiki'
        print 'graph_title Nombres de pages'
        print 'graph_args --base 1000 --lower-limit 0'
        print 'graph_vlabel nombre de pages'
        print 'total.label Total'
        print 'total.draw AREA'
        print 'orphelines.label Pages orphelines'
        print 'orphelines.draw AREA'
elif arg == "fichier" :
    from MoinMoin.request.request_cli import Request
    sys.path.insert(0, '/etc/moin')

    request = Request(u"wiki.crans.org/")
    request.form = request.args = request.setup_args()
    
    pages = request.rootpage.getPageDict(user='')
    orphaned = {}
    orphaned.update(pages)
    for page in pages.values():
        links = page.getPageLinks(request)
        for link in links:
            if link in orphaned:
                del orphaned[link]

    tmpfile = tempfile.NamedTemporaryFile()
    tmpfile.write("total.value %d\n" % pages.__len__())
    tmpfile.write("orphelines.value %d\n" % orphaned.__len__())
    tmpfile.flush()

    shutil.copy(tmpfile.name, FICHIER_MUNIN)
    
    tmpfile.close()
else:
    sys.stdout.write(open(FICHIER_MUNIN).read())
