#!python
#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import os
dirpath = os.path.join(os.path.dirname(os.path.abspath(__file__)),"../")
sys.path.insert(0,dirpath)
jbio = os.path.join(os.path.dirname(os.path.abspath(__file__)),"jbio.send")
jmail = os.path.join(os.path.dirname(os.path.abspath(__file__)),"jmail")
cmd2showdoc = os.path.join(os.path.dirname(os.path.abspath(__file__)),"cmd2showdoc.py")
from jbiot import csub
import time
import glob
startime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
import yaml
import getpass


def reademail():
    home = os.environ["HOME"]
    cfg = os.path.join(home,".lcsub","config.yml")
    if not os.path.exists(cfg):
        return

    fp = open(cfg)
    cfg = yaml.load(fp.read())
    if not cfg:
        return
    if "email" in cfg:
        return cfg["email"]

def setemail(email):
    home = os.environ["HOME"]
    cfg = os.path.join(home,".lcsub","config.yml")
    cfgdir = os.path.join(home,".lcsub")
    if not os.path.exists(cfgdir):
        os.mkdir(cfgdir)
    if os.path.exists(cfg):
        infodict = yaml.load(open(cfg).read())
    if not infodict:
        infodict = {}
    infodict["email"]  = email
    fp = open(cfg,"w")
    outstr = yaml.dump(infodict,default_flow_style=False)
    fp.write(outstr)


def readwechat():
    home = os.environ["HOME"]
    cfg = os.path.join(home,".lcsub","config.yml")
    if not os.path.exists(cfg):
        return

    fp = open(cfg)
    cfg = yaml.load(fp.read())
    if not cfg:
        return
    if "wechat" in cfg:
        return cfg["wechat"]

def setwechat(wechat):
    home = os.environ["HOME"]
    cfg = os.path.join(home,".lcsub","config.yml")
    cfgdir = os.path.join(home,".lcsub")
    if not os.path.exists(cfgdir):
        os.mkdir(cfgdir)
    if os.path.exists(cfg):
        infodict = yaml.load(open(cfg).read())
    if not infodict:
        infodict = {}
    infodict["wechat"]  = wechat

    fp = open(cfg,"w")
    outstr = yaml.dump(infodict,default_flow_style=False)
    fp.write(outstr)



def readlogs():
    content = "Job Finished.\n\n"
    content = content + "working dir : %s\n" % os.getcwd()
    content = content + "start time: %s\n" % (startime)
    endtime = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    content = content + "finished time: %s\n\n" % (endtime)

    content = content + "Details:\n\n"
    if not os.path.exists("logs"):
        return "ERROR!!! no logs found. Job probably failed\n"

    logs = glob.glob("logs/*.log")
    logs = sorted(logs)

    errors = []

    for log in logs:
        err = {}
        fp = open(log)
        head = fp.readline()
        head = fp.readline()
        head = head.strip()
        err[head] = []
        for line in fp.readlines():
            if line.find("Error") != -1 or line.find("error") != -1 or line.find("ERROR") != -1 or line.find("not found") != -1 or line.find(u"无法") != -1 or line.find(u"没有") != -1 or line.find(u"错误") != -1:
                err[head].append(line)

        errors.append(err)

    for err in errors:
        for cmd,lines in err.items():
            line =  "%s\n" % cmd
            content = content + line
            if not lines:
                content = content +  "\t seems success\n"
            for line in lines:
                line = "\t%s\n" % line
                content = content + line
    content = content + "\n"
    return content


if __name__ == "__main__":
    from docopt import docopt

    usage = """
    Usage:
        csub.py <cmdfile> [--with-docker|--with-singularity][-e <email>][-w <weixin_name>][-n <name>] 

    Options:
        -h --help           print this screen
        --with-docker       prefer to user docker when exec cmd
        --with-singularity  prefer to user singularity when exec cmd
        -e,--email=<email>  email which to remind of
        -w,--wechat=<name>  wechat name
        -n,--name=<name>    jobname
    """
    args = docopt(usage)
    cmdfile = args["<cmdfile>"]
    docker = args["--with-docker"]
    sing = args["--with-singularity"]
    email = args["--email"]
    wechat = args["--wechat"]
    name = args["--name"]
    csub.csub(cmdfile,docker=docker,sing=sing)


    content = readlogs()
    
    msg = content[:500]
    if wechat:
        setwechat(wechat)
    wechat = readwechat()
    #if wechat:
    #    cmd = "%s '%s' -u '%s' " % (jbio,msg,wechat)
    #    os.system(cmd)    
    # with email...
    if email :
        setemail(email)
    email = reademail()
    if email:
        subject = "csub run finished report"
        cmd = "%s -t %s -s '%s' -c '%s'" % (jmail,email,subject,content)
        os.system(cmd)

    # showdoc
    startime = time.strftime('%m-%d-%H:%M',time.localtime(time.time()))
    user = getpass.getuser()
    title = "%s-%s" % (user,startime)
    if name:
        title =  "%s" % (name)
    cmd = "%s %s -t %s" % (cmd2showdoc,cmdfile,title)
    os.system(cmd)

