import os
import sys
from optparse import make_option
from django.core.management.base import BaseCommand
from django.conf import settings

class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--log', '-l', dest='log', action="store_true", default=False,
            help=u"returns nagios alert's script."),
    )
    
    help = u"Sphinx's administration tools."

    output_transaction = True

    def handle(self, **options):
        
        if options.get('log', False):
            return self.log_script()
    
    def log_script(self):
        """docstring for log_script"""
        
        path = __file__
        for i in range(6):
            path = os.path.dirname(path)
        path2 = os.path.dirname(path)
        params = {
            'settings':os.environ['DJANGO_SETTINGS_MODULE'],
            'pythonpath':path,
            'pythonpath2':path2,
        }
        template = r"""#!/usr/bin/python
import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'wanajob_pro.%(settings)s'

sys.path.append("%(pythonpath)s")
sys.path.append("%(pythonpath2)s")

from django.conf import settings
from django.db import models
from wanajob_pro.job_board.models import JobPost
# JobPost = models.get_model('job_board','JobPost')


def log_exit(lvl=2):
    if lvl == 0:
        print "OK: sphinx is up"
    else:
        print "CRITICAL: sphinx is down"
    sys.exit(lvl)




if __name__ == '__main__':
    try:
        job = JobPost.search.query("").set_options(maxmatches=1, mode=6, limit=1)[0]
    except:
        log_exit(2)
    
    log_exit(0)
""" % params
        
        print template