#! /usr/bin/env python
#coding=utf8

import os
import shutil
import calendar

count_gk = 0
count_jo = 0

prefix_dict = {
    'rn' : '00258_',
    'gor' : '20452_',
    'vazh' : '16454_',
    'NAME_1' : '16458_',
    'vozn' : '16455_',
    'nik' : '16456_'
    }
dir_list = {'rn','gor','vazh','NAME_1','vozn','nik'}


def day_off(date):
    if date[:1] == 0:
        day = date[1:2]
    else:
        day = date[:2]
    if date[2:3] == 0:
        month = date[3:4]
    else:
        month = date[2:4]
    if calendar.weekday(2023, int(month), int(day)) in {5,6}:
        return True
    else:
        return False


for dir in dir_list:
    for month in os.listdir('./Glavn_kniga/' + dir):
        #print(dir, ': ' + month)
        file_path = './Glavn_kniga/' + dir + '/' + month
        file_path_new = './Out/' + month
        for file_old in os.listdir(file_path):
            file_new = prefix_dict[dir] + file_old
            dir_new = file_path_new + '/' + file_old[:2] + '/' + dir
            if os.path.exists(dir_new + '/' + 'ГК_' +file_new):
                    pass
                    #print('[-] Файл уже существует')
            elif day_off(file_old):
                pass
                #print('[-] Выходной день')
            else:
                print('[+] Скопировано:' + file_path + '/' + file_old + '  >>  ' + dir_new + '/' + file_new)
                if not os.path.exists(dir_new):
                    os.makedirs(dir_new)
                shutil.copy(file_path + '/' + file_old ,  dir_new + '/' + 'ГК_' +file_new)
                count_gk += 1
                
for dir in dir_list:
    for month in os.listdir('./Jurnal_operacii/' + dir):
        #print(dir, ': ' + month)
        file_path = './Jurnal_operacii/' + dir + '/' + month
        file_path_new = './Out/' + month
        for file_old in os.listdir(file_path):            
            file_new = prefix_dict[dir] + file_old
            dir_new = file_path_new + '/' + file_old[:2] + '/' + dir
            if os.path.exists(dir_new + '/' + 'ЖО_' +file_new):
                    pass
                    #print('[-] Файл уже существует')
            elif day_off(file_old):
                pass
                #print('[-] Выходной день')
            else:
                print('[+] Скопировано:' + file_path + '/' + file_old + '  >>  ' + dir_new + '/' + file_new)
                if not os.path.exists(dir_new):
                    os.makedirs(dir_new)
                shutil.copy(file_path + '/' + file_old , dir_new + '/' + 'ЖО_' +file_new)
                count_jo += 1

print(