import os

rootdir = os.getcwd()

girlType = ''
girlAge = ''
girlClothing = ''
action = ''
picAmounts = {}
sceneAmounts = {}
for subdir, dirs, files in os.walk(rootdir + os.sep + 'images' + os.sep + 'girls'):
    currentDir = os.path.basename(subdir)
    if currentDir in ['1', '2', '3', 'hookers', 'demon', 'employees', 'police', 'cityhall', 'meet']:
        continue
    else:
        if currentDir in ['african', 'asian', 'blonde', 'brunette', 'latina', 'redhead']:
            girlType = currentDir + '_'
        else:
            if currentDir in ['mature', 'young']:
                girlAge = currentDir
            else:
                if currentDir in ['alley', 'citycenter', 'oldcanal', 'oldtown', 'park']:
                    picAmounts[girlType + girlAge + '_' + currentDir] = []
                    for file in files:
                        filepath = subdir + os.sep + file
                        if filepath.endswith(".jpg"):
                            picAmounts[girlType + girlAge + '_' + currentDir].append(int(os.path.splitext(file)[0]))
                else:
                    if currentDir in ['dressed', 'naked', 'undies']:
                        girlClothing = currentDir[0].upper() + '_'
                    else:
                        if currentDir != 'girls':
                            action = currentDir
                            sceneAmounts[girlType + girlAge + '_' + girlClothing + action] = 0
                            for file in files:
                                filepath = subdir + os.sep + file
                                if filepath.endswith(".webm"):
                                    sceneAmounts[girlType + girlAge + '_' + girlClothing + action] += 1

with open('Inseminator.html', 'r', encoding='utf-8') as fd:
    filedata = fd.read()
    fd.seek(0)
    for ln in fd:
        if ln.startswith('$girlPics to'):
            currentPicAmounts = ln
        if ln.startswith('setup.sceneNames'):
            currentSceneNames = ln
        if ln.startswith('setup.sceneAmounts'):
            currentSceneAmounts = ln

newPicAmounts = '$girlPics to ' + str(picAmounts) + ';\r\n'

newSceneNames = 'setup.sceneNames to ['
keys = list(sceneAmounts.keys())
for key in keys:
    newSceneNames += '&quot;' + key + '&quot;' + ','
newSceneNames = newSceneNames[:-1] + '],\r\n'
newSceneAmounts = 'setup.sceneAmounts to ['
values = list(sceneAmounts.values())
for value in values:
    newSceneAmounts += str(value) + ','
newSceneAmounts = newSceneAmounts[:-1] + ']\r\n'

filedata = filedata.replace(currentPicAmounts, newPicAmounts)
filedata = filedata.replace(currentSceneNames, newSceneNames)
filedata = filedata.replace(currentSceneAmounts, newSceneAmounts)

with open('Inseminator.html', 'w', encoding='utf-8') as fd:
    fd.write(filedata)