import subprocess, os

ue4_win = r"C:\Program Files\Epic Games\UE_4.16"
ue4_linux = "/home/qiuwch/workspace/UE416"
ue4_mac = '/Users/Shared/Epic Games/UE_4.16'

win_uprojects = [
    r'C:\qiuwch\workspace\uprojects\UE4RealisticRendering\RealisticRendering.uproject',
    r'C:\qiuwch\workspace\uprojects\UE4ArchinteriorsVol2Scene1\ArchinteriorsVol2Scene1.uproject',
    r'C:\qiuwch\workspace\uprojects\UE4ArchinteriorsVol2Scene2\ArchinteriorsVol2Scene2.uproject',
    r'C:\qiuwch\workspace\uprojects\UE4ArchinteriorsVol2Scene3\ArchinteriorsVol2Scene3.uproject',
    r'C:\qiuwch\workspace\uprojects\UE4UrbanCity\UrbanCity.uproject',
    r'D:\workspace\uprojects\Matinee\Matinee.uproject',
    r'D:\workspace\uprojects\PhotorealisticCharacter\PhotorealisticCharacter2.uproject',
]

linux_uprojects = [
    os.path.expanduser('~/workspace/uprojects/UE4RealisticRendering/RealisticRendering.uproject'),
    os.path.expanduser('~/workspace/uprojects/UE4ArchinteriorsVol2Scene1/ArchinteriorsVol2Scene1.uproject'),
    os.path.expanduser('~/workspace/uprojects/UE4ArchinteriorsVol2Scene2/ArchinteriorsVol2Scene2.uproject'),
    os.path.expanduser('~/workspace/uprojects/UE4ArchinteriorsVol2Scene3/ArchinteriorsVol2Scene3.uproject'),
    os.path.expanduser("~/workspace/uprojects/UE4UrbanCity/UrbanCity.uproject"),
]

mac_uprojects = [
    os.path.expanduser('~/workspace/UnrealEngine/Templates/FP_FirstPerson/FP_FirstPerson.uproject'),
    os.path.expanduser('~/uprojects/RealisticRendering/RealisticRendering.uproject'),
    os.path.expanduser('~/uprojects/UE4ArchinteriorsVol2Scene1/ArchinteriorsVol2Scene1.uproject'),
    os.path.expanduser('~/uprojects/UE4ArchinteriorsVol2Scene2/ArchinteriorsVol2Scene2.uproject'),
    os.path.expanduser('~/uprojects/UE4ArchinteriorsVol2Scene3/ArchinteriorsVol2Scene3.uproject'),
    os.path.expanduser('~/uprojects/UE4UrbanCity/UrbanCity.uproject'),
]

uprojects = []

for uproject_path in win_uprojects:
    uproject_name = os.path.basename(uproject_path).split('.')[0]
    uprojects.append(
        dict(
            uproject_path = uproject_path,
            ue4_path = ue4_win,
            log_file = 'log/win_%s.log' % uproject_name
        ),
    )

for uproject_path in linux_uprojects:
    uproject_name = os.path.basename(uproject_path).split('.')[0]
    uprojects.append(
        dict(
            uproject_path = uproject_path,
            ue4_path = ue4_linux,
            log_file = 'log/linux_%s.log' % uproject_name
        ),
    )

for uproject_path in mac_uprojects:
    uproject_name = os.path.basename(uproject_path).split('.')[0]
    uprojects.append(
        dict(
            uproject_path = uproject_path,
            ue4_path = ue4_mac,
            log_file = 'log/mac_%s.log' % uproject_name
        ),
    )


if __name__ == '__main__':
    for uproject in uprojects:
        uproject_path = uproject['uproject_path']
        if not os.path.isfile(uproject_path):
            print("Can not find uproject file %s, skip this project" % uproject_path)
            continue

        cmd = [
                'python', 'build.py',
                '--UE4', uproject['ue4_path'],
                # '--output', uproject['output_folder'],
                uproject['uproject_path']
            ]
        print(cmd)
        subprocess.call(cmd,
            stdout = open(uproject['log_file'], 'w'))
        with open(uproject['log_file']) as f:
            lines = f.readlines()
            print(''.join(lines[-10:])) # Print the last few lines

[((18, 4, 18, 97), 'os.path.expanduser', 'os.path.expanduser', ({(18, 23, 18, 96): '"""~/workspace/uprojects/UE4RealisticRendering/RealisticRendering.uproject"""'}, {}), "(\n    '~/workspace/uprojects/UE4RealisticRendering/RealisticRendering.uproject')", False, 'import subprocess, os\n'), ((19, 4, 19, 107), 'os.path.expanduser', 'os.path.expanduser', ({(19, 23, 19, 106): '"""~/workspace/uprojects/UE4ArchinteriorsVol2Scene1/ArchinteriorsVol2Scene1.uproject"""'}, {}), "(\n    '~/workspace/uprojects/UE4ArchinteriorsVol2Scene1/ArchinteriorsVol2Scene1.uproject'\n    )", False, 'import subprocess, os\n'), ((20, 4, 20, 107), 'os.path.expanduser', 'os.path.expanduser', ({(20, 23, 20, 106): '"""~/workspace/uprojects/UE4ArchinteriorsVol2Scene2/ArchinteriorsVol2Scene2.uproject"""'}, {}), "(\n    '~/workspace/uprojects/UE4ArchinteriorsVol2Scene2/ArchinteriorsVol2Scene2.uproject'\n    )", False, 'import subprocess, os\n'), ((21, 4, 21, 107), 'os.path.expanduser', 'os.path.expanduser', ({(21, 23, 21, 106): '"""~/workspace/uprojects/UE4ArchinteriorsVol2Scene3/ArchinteriorsVol2Scene3.uproject"""'}, {}), "(\n    '~/workspace/uprojects/UE4ArchinteriorsVol2Scene3/ArchinteriorsVol2Scene3.uproject'\n    )", False, 'import subprocess, os\n'), ((22, 4, 22, 79), 'os.path.expanduser', 'os.path.expanduser', ({(22, 23, 22, 78): '"""~/workspace/uprojects/UE4UrbanCity/UrbanCity.uproject"""'}, {}), "('~/workspace/uprojects/UE4UrbanCity/UrbanCity.uproject')", False, 'import subprocess, os\n'), ((26, 4, 26, 99), 'os.path.expanduser', 'os.path.expanduser', ({(26, 23, 26, 98): '"""~/workspace/UnrealEngine/Templates/FP_FirstPerson/FP_FirstPerson.uproject"""'}, {}), "(\n    '~/workspace/UnrealEngine/Templates/FP_FirstPerson/FP_FirstPerson.uproject'\n    )", False, 'import subprocess, os\n'), ((27, 4, 27, 84), 'os.path.expanduser', 'os.path.expanduser', ({(27, 23, 27, 83): '"""~/uprojects/RealisticRendering/RealisticRendering.uproject"""'}, {}), "('~/uprojects/RealisticRendering/RealisticRendering.uproject'\n    )", False, 'import subprocess, os\n'), ((28, 4, 28, 97), 'os.path.expanduser', 'os.path.expanduser', ({(28, 23, 28, 96): '"""~/uprojects/UE4ArchinteriorsVol2Scene1/ArchinteriorsVol2Scene1.uproject"""'}, {}), "(\n    '~/uprojects/UE4ArchinteriorsVol2Scene1/ArchinteriorsVol2Scene1.uproject')", False, 'import subprocess, os\n'), ((29, 4, 29, 97), 'os.path.expanduser', 'os.path.expanduser', ({(29, 23, 29, 96): '"""~/uprojects/UE4ArchinteriorsVol2Scene2/ArchinteriorsVol2Scene2.uproject"""'}, {}), "(\n    '~/uprojects/UE4ArchinteriorsVol2Scene2/ArchinteriorsVol2Scene2.uproject')", False, 'import subprocess, os\n'), ((30, 4, 30, 97), 'os.path.expanduser', 'os.path.expanduser', ({(30, 23, 30, 96): '"""~/uprojects/UE4ArchinteriorsVol2Scene3/ArchinteriorsVol2Scene3.uproject"""'}, {}), "(\n    '~/uprojects/UE4ArchinteriorsVol2Scene3/ArchinteriorsVol2Scene3.uproject')", False, 'import subprocess, os\n'), ((31, 4, 31, 69), 'os.path.expanduser', 'os.path.expanduser', ({(31, 23, 31, 68): '"""~/uprojects/UE4UrbanCity/UrbanCity.uproject"""'}, {}), "('~/uprojects/UE4UrbanCity/UrbanCity.uproject')", False, 'import subprocess, os\n'), ((70, 15, 70, 44), 'os.path.isfile', 'os.path.isfile', ({(70, 30, 70, 43): 'uproject_path'}, {}), '(uproject_path)', False, 'import subprocess, os\n'), ((37, 20, 37, 51), 'os.path.basename', 'os.path.basename', ({(37, 37, 37, 50): 'uproject_path'}, {}), '(uproject_path)', False, 'import subprocess, os\n'), ((47, 20, 47, 51), 'os.path.basename', 'os.path.basename', ({(47, 37, 47, 50): 'uproject_path'}, {}), '(uproject_path)', False, 'import subprocess, os\n'), ((57, 20, 57, 51), 'os.path.basename', 'os.path.basename', ({(57, 37, 57, 50): 'uproject_path'}, {}), '(uproject_path)', False, 'import subprocess, os\n')]