#!/usr/bin/env python

import sys
import os
from os import path
from helpers import parse_configfile, run_command
from subprocess import check_call

def main():
    try:
        os.mkdir('ffmpeg_build')
    except:
        pass
    mydir = os.getcwd()
    extra_args = parse_configfile('common_options')
    for arg in parse_configfile('ffmpeg_options'):
        if arg == 'librtmp_magic':
            try:
                check_call('pkg-config --exists librtmp'.split())
            except:
                sys.exit('You have specified "librtmp_magic" in ffmpeg_options,'
                         'but running "pkg-config --exists librtmp" failed - '
                         "can't enable librtmp! Aborting.")
            extra_args.append('--enable-librtmp')
            cflags = run_command('pkg-config --cflags librtmp').strip()
            libs = run_command('pkg-config --libs librtmp').strip()
            extra_args.append('--extra-cflags=%s' % cflags)
            extra_args.append('--extra-libs=%s' % libs)
        else:
            extra_args.append(arg)

    args = ['--prefix=%s/build_libs' % mydir,
            '--enable-gpl',
            '--cpu=host',
            '--disable-debug',
            '--enable-pthreads',
            '--disable-shared', '--enable-static',
            '--enable-postproc',
            '--disable-devices', '--disable-ffmpeg',
            '--disable-ffplay', '--disable-ffserver','--disable-ffprobe',
            '--disable-vaapi']

    if os.path.exists('ffmpeg-mt-enabled'):
        ffmpeg_dir = 'ffmpeg-mt'
    else:
        ffmpeg_dir = 'ffmpeg'
    executable = path.join(mydir, ffmpeg_dir, 'configure')

    os.chdir('ffmpeg_build')
    check_call([executable] + args + extra_args)

main()
