# Copyright 2026 SigmaSRC, Inc. All Rights Reserved.

"""Gather script for document value.
We need to parameterize it. Parameters:
halflife
doclist
"""


import logging
import os
import platform
import time
from sigma.runtime import rtlib
from sigma.util.fuelset import FuelSet

from pprint import pprint

logger = logging.getLogger("gather.gather_docdata")

# Default data for Admin/System/Risk/HostGroup/DocValue
# TODO: pull this data from server matrix
calculatedocumentvalue = True
calculatedocumenthalflife = True
documenthalflife = 15
searchlist = [
        ["c:\\Documents and Settings\\**\\My Documents\\**", ['*.ppt'], "PowerPoint - Windows", 500],
        ["c:\\Documents and Settings\\**\\My Documents\\**", ['*.doc'], "Word - Windows", 400],
        ["c:\\Documents and Settings\\**\\My Documents\\**", ['*.xls'], "Excel - Windows", 600],
        ["c:\\Documents and Settings\\**\\My Documents\\**", ['*.dwg','*.dwf'], "Drawings - Windows", 400],
        ["c:\\Documents and Settings\\**\\My Documents\\**", ['*.fm'], "Frame Maker - Windows", 400],
        ["c:\\Documents and Settings\\**\\My Documents\\**", ['*.pdf'], "Adobe Acrobat - Windows", 100],
        ["c:\\Users\\**\\Documents\\**", ['*.ppt','*.pps','*.pptx','*.ppsx','*.odp'], "PowerPoint XML - Windows", 500],
        ["c:\\Users\\**\\Documents\\**", ['*.doc','*.docx','*.odt'], "Word XML - Windows", 400],
        ["c:\\Users\\**\\Documents\\**", ['*.xls','*.xlsx','*.ods','*.csv'], "Excel XML - Windows", 600],
        ["c:\\Users\\**\\Documents\\**", ['*.vsd','*.vsdx'], "Visio XML- Windows", 500],
        ["c:\\Users\\**\\Documents\\**", ['*.dwg','*.dwf','*.dwfx'], "Drawings 2 - Windows", 400],
        ["c:\\Users\\**\\Documents\\**", ['*.mpp','*.mpx','*.mpd'], "Microsoft Project - Windows", 600]
        ]

def Gather(collector):
    # write header to output
    table = collector.table("docdata", ["doc", "doc_count", "doc_full_val", "doc_depreciated_val", "doc_half_life"])
    host = rtlib.Host("localhost")
    halflife = documenthalflife
    halflifeseconds = documenthalflife * 24  * 60 * 60
    curtime = int(time.time())

    ostype = platform.system().lower().replace("-", "")
    if ostype in ("windows", "microsoft", "microsoft windows"):
        for entry in searchlist:
            path = entry[0]
            filetypes = entry[1]
            entryname = entry[2]
            value = entry[3]
            i = path.replace("c:\\", "").find("\\") + 3
            # Check if root directory of 0 exists
            if os.path.isdir(path[:i]):
                filepathlist = FuelSet()

                for filetype in filetypes:
                    thepath = path + "\\" + filetype
                    filepathlist.add(thepath)

                files = host.glob(filepathlist, {}, False)
                docvalue = float(0)
                docDepValue = float(0)
                docOrigValue = float(value)
                doccount = 0
                

                for filerecord in files:
                    doccount = doccount + 1
                    if calculatedocumentvalue:
                        filetime = filerecord.mtime

                        filevalue = docOrigValue
                        if calculatedocumenthalflife:
                            if halflifeseconds > 0:
                                while curtime - filetime >= halflifeseconds:
                                    filevalue = filevalue / 2
                                    filetime = filetime + halflifeseconds
                        docDepValue = docDepValue + (docOrigValue - filevalue)
                        docvalue = docvalue + filevalue

                # Send data to Gather
                table.add((entryname, doccount, docvalue, docDepValue, halflife))

def _test():
    from sigma.gather import gathering
    gathering.test(Gather)

if __name__ == "__main__":
    _test()
