#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 Clay L. McLeod <clay.l.mcleod@gmail.com>
#
# Distributed under terms of the MIT license.

from __future__ import print_function

import re
from celcius.utils import cron_utils

length_of_status_str = 60

def format_status_for_cmd(cmd):
    match = None
    match = re.search(r'CJOBID=\'[\w\s\.]*\'', cmd)
    if match:
        jobid_regex = re.search(r'\'[\w\s\.]*\'', match.group())
        jobid = jobid_regex.group().replace('\'', '')
        if jobid:
            jobstr = "[{}]".format(jobid)
            cutoff_str = "MAILTO=''"
            cutoff_index = cmd.index(cutoff_str) + len(cutoff_str)
            modified_str = cmd[cutoff_index:cutoff_index+length_of_status_str]
            return "{} {}...".format(jobstr.rjust(6, ' '), modified_str)

    return ""

commands = cron_utils.get_all_celcius_commands()
if len(commands) > 0:
    print("The following celcius commands are present:")
    print()
    for cmd in commands:
        print(format_status_for_cmd(cmd))
else:
    print("No celcius commands are present!")
