#!/usr/bin/env python3

import os.path
import xml.etree.ElementTree as ET
tree = ET.parse('glife.qproj')
root = tree.getroot()


rootFolder = 'sugarcube/src/autogenerated'
unsortedFolder = os.path.join(rootFolder,'unsorted')

for child in root[0]:
    if(child.tag == 'Folder'):
        folderName = child.attrib['name']
        for subchild in child:
            passageName = subchild.attrib['name']
            oldFilePath = os.path.join(unsortedFolder,passageName+'.tw')
            if(os.path.isfile(oldFilePath)):
                newFolder = os.path.join(rootFolder,folderName)
                os.makedirs(newFolder, exist_ok=True)
                newFilePath = os.path.join(newFolder,passageName+'.tw')
                os.replace(oldFilePath,newFilePath)
                