#! /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 os, sys, argparse

from celcius.utils import terminal_utils, file_utils
from celcius.utils import cron_utils

parser = argparse.ArgumentParser(description="Utility for unscheduling celcius data pipelining tasks")
parser.add_argument("jobid", help="The jobid to remove from cron")
args = parser.parse_args()

if file_utils.get_celcius_config() == None:
    print("You must first run celcius-init")
    sys.exit(1)

tasks = cron_utils.get_all_celcius_commands()
string_of_crontabs = ""

task_found = False
for task in tasks:
    if not "CJOBID=\'{}\'".format(args.jobid) in task:
        string_of_crontabs = "{}{}".format(string_of_crontabs, task)
    else:
        task_found = True

if not task_found:
    print("Task with jobid '{}' was not found!".format(args.jobid))
    sys.exit(1)

cmd = "echo \"{}\" | crontab -".format(string_of_crontabs)
if terminal_utils.confirm_command(cmd):
    os.system(cmd)
