#!/usr/bin/python3

import sys
import os
import re
import pika
import logging
import argparse
from asebat.timefmt import timestamp_fmt as ts

from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from pyftpdlib.authorizers import UnixAuthorizer
from pyftpdlib.filesystems import UnixFilesystem


class ASEHandler(FTPHandler):
    def on_file_received(self, file):
        path, filenameExt = os.path.split(file)
        filename, fileExtension = os.path.splitext(filenameExt)

        m = re.match( r'^(G\d\d\d)_(ID\d\d\d\d)_(DT\d\d\d\d)_(\d\d)(\d\d)(\d\d\d\d)(\d\d)(\d\d)(\d\d)$', filename, re.I)
        if m : 
            unitType = m.group(1).upper()
            unitName = m.group(2).upper()
            toolName = m.group(3).upper()
            toolType = "N/A"
            fileDate = m.group(6) + "/" + m.group(5) + "/" + m.group(4)
            fileTime = m.group(7) + ":" + m.group(8) + ":" + m.group(9)
        else :
            with open(file, "r") as fileCsv:
                try:
                    for i, line in enumerate(fileCsv.readlines(4096), 1):
                        if i == 1:
                            m1 = re.match( r'File Creation Date: (\d*\/\d*\/\d*)\s(\d*:\d*:\d*).*', line, re.I )
                            fileDate = m1.group(1)
                            fileTime = m1.group(2)
                        if i == 2:
                            unitType, unitName = line.strip('\n').replace(";","").replace(",","").split(" ")
                        if i == 6:
                            m6 = re.match( r'SD path: .*\/.*\/(.*)\/(.*)\..*', line, re.I )
                            toolType = m6.group(1)
                            toolName = m6.group(2)
                            break
                except:
                    logging.error("PID {:>5} >> Error: {}.".format( os.getpid(), sys.exc_info()[1]))
                fileCsv.close
        
        logging.info("PID {:>5} >> {} - {} - {} - {} - {} - {}.".format(os.getpid(), unitType.upper(), unitName.upper(), toolName.upper(), toolType.upper(), fileDate, fileTime ))
        newPath = '/home/' + self.username + '/received/' + unitName.upper() + '/'
        newFilename = newPath + filename + '_' + str(ts.timestamp("tms") + fileExtension)
        try:
            os.makedirs(newPath)
            logging.info("PID {:>5} >> path {} created.".format(os.getpid(), newPath))
        except FileExistsError:
            logging.info("PID {:>5} >> path {} already exists.".format(os.getpid(), newPath))
        try: 
            os.rename(file, newFilename)
            logging.info("PID {:>5} >> {} moved into {}.".format(os.getpid(), filenameExt, newFilename))
        except OSError:
            logging.error("PID {:>5} >> Error to move {} into {}.".format(os.getpid(), filenameExt, newFilename))

    def on_incomplete_file_received(self, file):
        # remove partially uploaded files
        os.remove(file)

def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("-l", "--logfilename", help="log file", required=True)
    parser.add_argument("-p", "--port", help="passive port range start", type=int, required=True)
    parser.add_argument("-x", "--proxy", help="proxy address", required=True)
    args = parser.parse_args()

    try:
        authorizer = UnixAuthorizer(
            rejected_users=["root"], require_valid_shell=True)
        handler = ASEHandler
        handler.authorizer = authorizer
        handler.abstracted_fs = UnixFilesystem
        handler.masquerade_address = args.proxy
        _range = list(range(args.port, args.port + 20))
        handler.passive_ports = _range

        logging.basicConfig(format='%(asctime)s %(message)s',
                            filename="/var/log/" + args.logfilename, level=logging.INFO)

        server = FTPServer(('0.0.0.0', 21), handler)
        
        server.serve_forever()
    except KeyboardInterrupt:
        logging.info("PID {:>5} >> Error: {}.".format(os.getpid(), "Shutdown requested...exiting"))
    except Exception:
        print("{} - PID {:>5} >> Error: {}.".format(ts.timestamp("log"), os.getpid(), sys.exc_info()[1]))
        
if __name__ == "__main__":
        main()