#!/usr/bin/env python

import os
from os import path
from subprocess import check_call

from script.helpers import GitWrapper

def main():
    git = GitWrapper()
    open('ffmpeg-mt-enabled', 'w').close()
    # Allow setups where both ffmpeg and ffmpeg-mt are checked out
    # simultaneously, and leave the directories alone in that case.
    if path.exists('ffmpeg/.git') and not path.exists('ffmpeg-mt/.git'):
        # ffmpeg-mt should be an empty directory if things are right
        os.rmdir('ffmpeg-mt')
        os.rename('ffmpeg', 'ffmpeg-mt')
        os.mkdir('ffmpeg')
        if git.supports_nofetch:
            cmd = 'git submodule update --no-fetch'.split()
        else:
            cmd = 'git submodule update'.split()
        check_call(cmd + ['ffmpeg-mt'])
        os.chdir('ffmpeg-mt')
        def func(cmd=cmd):
            check_call(cmd)
        git.foreach_module(func)

main()
