#!/usr/bin/env python

import os
from datetime import datetime
from datetime import date
import numpy as np

def print_log(msg=None, debug=False):
    if debug:
        print("WARNING DEBUG: "+msg)
    else:
        print(msg)

def move_log(logFile=None, oldDir=None, newDir=None):
    ''' move the content of logFile in /newDir/logFile-date_save 
    and create a new empty logFile in the original directory  '''

    DEBUG = False #True
    print_log(msg = str(datetime.now())+":  Archiving and Reset of Python-Log ...", debug=DEBUG)

    cmd_list = []
    date_save = str(date.today())
    year  = date_save.split("-")[0]
    month = date_save.split("-")[1]
    day   = date_save.split("-")[2]
    path = "/data/log/"
    subDir = year+"-"+month+"/"
   
    if logFile is None:
        logFile = "log_of_python_test.log"
    else:
        logFile = str(logFile)

    if oldDir is None:
        oldDir = path
    else:
        oldDir = str(oldDir)

    if newDir is None:
        newDir = path+"old/"+subDir
    else:
        newDir = str(newDir)

    if(0<int(day)<8):  #First week of the month -> create new directory
        print_log(msg="First week of the month -> creating the new directory", debug=DEBUG)
        cmd0 = "mkdir "+newDir
        cmd_list.append(cmd0)
        print_log(msg="command 0 : "+cmd0, debug=DEBUG)

    cmd1 = "mv "+oldDir+logFile+" "+newDir+logFile+"-"+date_save
    cmd_list.append(cmd1)
    cmd2 = "touch "+oldDir+logFile
    cmd_list.append(cmd2)
    cmd3 = "chown shinsmgr:oapd "+oldDir+logFile
    cmd_list.append(cmd3)
    cmd4 = "chmod 664 "+oldDir+logFile
    cmd_list.append(cmd4)

    print_log(msg="command 1 : "+cmd1, debug=DEBUG)
    print_log(msg="command 2 : "+cmd2, debug=DEBUG)
    print_log(msg="command 3 : "+cmd3, debug=DEBUG)
    print_log(msg="command 4 : "+cmd4, debug=DEBUG)

    if not DEBUG:
        for cmd in cmd_list:
            os.system(cmd)
            #print(cmd)
            
        # os.system(cmd1)
        # os.system(cmd2)
        # os.system(cmd3)
        # os.system(cmd4)

    # os.system("chown shinsmgr:oapd "+path+"cronlog.log")
    print_log(msg=str(datetime.now())+":  Archiving and Reset of Python-Log ... DONE", debug=DEBUG)



def move_log2(logFile=None, oldDir=None, newDir=None):
    ''' move the content of logFile in /newDir/logFile-date_save 
    and create a new empty logFile in the original directory  '''

    DEBUG = False #True
    print_log(msg = str(datetime.now())+":  Archiving and Reset of Python-Log ...", debug=DEBUG)

    cmd_list = []
    date_save = str(date.today())
    year  = date_save.split("-")[0]
    month = date_save.split("-")[1]
    day   = date_save.split("-")[2]
    path = "/data/log/"
    subDir = year+"-"+month+"/"
   
    if logFile is None:
        logFile = "log_of_python_test.log"
    else:
        logFile = str(logFile)

    if oldDir is None:
        oldDir = path
    else:
        oldDir = str(oldDir)

    if newDir is None:
        newDir = path+"old/"+subDir
    else:
        newDir = str(newDir)

    if(0<int(day)<8):  #First week of the month -> create new directory
        print_log(msg="First week of the month -> creating the new directory", debug=DEBUG)
        cmd0 = "mkdir "+newDir
        cmd_list.append(cmd0)
        print_log(msg="command 0 : "+cmd0, debug=DEBUG)

    cmd1 = "cp "+oldDir+logFile+" "+newDir+logFile+"-"+date_save
    cmd1bis = "cp /data/log/log_of_python_test.log /data/log/old/log_of_python_test.log"
    # cmd1 = cmd1bis
    cmd_list.append(cmd1)
    cmd2 = "echo "" > "+oldDir+logFile
    cmd_list.append(cmd2)

    print_log(msg="command 1 : "+cmd1, debug=DEBUG)
    print_log(msg="command 2 : "+cmd2, debug=DEBUG)
    
    if not DEBUG:
        for cmd in cmd_list:
            os.system(cmd)
            #print(cmd)
            
    # os.system(cmd1)
    # os.system(cmd2)
    
    # os.system("chown shinsmgr:oapd "+path+"cronlog.log")
    print_log(msg=str(datetime.now())+":  Archiving and Reset of Python-Log ... DONE", debug=DEBUG)

def test_tmux():
    cmdList = []
    cmdList.append("tmux attach -t alog")
    cmdList.append("tmux ls")
    cmdList.append("tmux detach")
    i = 0
    for cmd in cmdList:
        i += 1
        print("command {} : {}".format(i,cmd))
        os.system(cmd)


    
def run():
    
    print(str(datetime.now())+": --------------- EXECUTION STARTED ---------------")
    # print("WARNING: This execution is manual since previous didn't work due to an error (executed on monday but save tag is on sunday)")
    # print("WARNING: This is a DEBUG execution, no action will be actually performed")

    logFile = "log_of_python_test.log"                 #      Default value
    oldDir  = "/data/log/"                             #      Default value
    newDir  = "/data/log/old/"+str(date.today())[0:-3] #      Default value

    #test_tmux()
    move_log()
    #move_log2()
    print(str(datetime.now())+": ---------------  EXECUTION ENDED  ---------------")
    print("#######################################################################################")

    
if __name__ == "__main__":
    ''' If running from command line'''
    run()

    
