#!/user/bin/env python3
# invokes shadercross
# and builds shaders for samples

import os
import subprocess

from sys import platform

origDir = os.path.abspath(os.path.dirname(__file__))
contentDir = os.path.join(origDir, 'content/')
shadercrossDir = os.path.join(origDir, 'shadercross/bin')

if platform == "linux" or platform == "linux2":
    shadercrossDir = os.path.join(shadercrossDir, 'linux')
elif platform == "darwin":
    shadercrossDir = os.path.join(shadercrossDir, 'osx')
elif platform == "win32":
    shadercrossDir = os.path.join(shadercrossDir, 'win64')

shadercross = os.path.join(shadercrossDir, 'shadercross')

inputFiles = [os.path.abspath(os.path.join(contentDir, x)) for x in os.listdir(contentDir) if x.endswith('.hlsl')]

def cookAll():
    # general cooking flow I think for shaders is 
    # they will go under 
    cookedRoot = os.path.join(contentDir, '_cooked')

    # we cook into 3 formats 
    # spirv
    # msl
    # dxil

    outputFormats = [
        ('dxil', []),
        ('spv', []),
        ('msl', []),
    ]

    for fmt in outputFormats:
        outdir = os.path.join(cookedRoot, fmt[0])
        for f in inputFiles:
            basefile = f[:-5]
            outfile = os.path.join(outdir, os.path.basename(basefile) + '.' + fmt[0] )
            os.makedirs(os.path.dirname(outfile), exist_ok=True)
            cmd = [shadercross, f] + \
                fmt[1] + [ '-o', outfile ]

            print(cmd)
            subprocess.run(cmd)

if __name__ == '__main__':
    cookAll()
