#!/usr/bin/env python
import argparse
from datetime import date, timedelta, datetime
from termcolor import colored

"""
Script to generate extensions that can be used for copy of a file (backup).

See https://github.com/righettod/toolbox-pentest-web/issues/4
"""

SEPARATOR_CHARS = ["_", "-", "."]

def generate_dates(year_back):
    values = {"ISO":[], "FR":[], "OTHER": [], "TS":[]}
    end_date = date.today()
    # To include the the 01/01/[year]
    start_date = date(year = end_date.year - (year_back+1), month = 12, day = 31) 
    delta_days = (end_date - start_date).days
    while delta_days > 0:
        start_date += timedelta(days=1) 
        # ISO format: YYYYMMDD
        values["ISO"].append(start_date.strftime("%Y%m%d"))
        # FR format: DDMMYYYY
        values["FR"].append(start_date.strftime("%d%m%Y"))
        # OTHER format: MMDDYYYY
        values["OTHER"].append(start_date.strftime("%m%d%Y"))        
        # Timestamp format
        ts = int(datetime.fromordinal(start_date.toordinal()).timestamp())
        values["TS"].append(ts)        
        delta_days -= 1
    for k in values:
        values[k].sort()
    return values

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Script to generate extensions that can be used for copy of a file (backup).")
    required_params = parser.add_argument_group("required named arguments")
    required_params.add_argument("-y", action="store", dest="year_back_count", type=int, help="Year back to take as start point like 2 for example.", required=True)    
    parser.add_argument("-o", action="store", dest="output_file", help="File in which the generated content must be wrote.", required=False, default="/tmp/backup-extensions-dict.txt") 
    args = parser.parse_args()
    year_back = abs(args.year_back_count)
    print(colored(f"[+] Generate dates extensions from {year_back} year(s) back from January 1st...", "magenta"))
    extensions = generate_dates(year_back)
    print(colored(f"[+] Write file '{args.output_file}'...", "magenta"))
    with open(args.output_file, "w", encoding="utf-8") as f:
        for char in SEPARATOR_CHARS:
            for k in extensions:
                for ext in extensions[k]:
                    f.write(f"{char}{ext}\n")
    print(colored(f"[+] Operation finished.", "magenta"))
