#!/usr/bin/env python3

import os
from os import path
import sys
from optparse import OptionParser
from subprocess import check_call

from script.helpers import run_command, GitWrapper

option_helptext = \
"""# Place each option on its own line. Empty lines and lines starting with '#'
# are ignored. The options do not need quoting, so you can have for example
# --extra-libs=-lfoo -lbar
# (and NOT --extra-libs='-lfoo -lbar').
"""

mplayer_options = \
"""# You can place options for MPlayer configure in this file.

""" + option_helptext

libav_options = \
"""# You can place options for Libav configure in this file.

""" + option_helptext

common_options = \
"""# You can place options common for both MPlayer and Libav configure in
# this file. This mainly makes sense for generic things like --cc.
#
# NOTE: Several people used --prefix here for some reason. Don't do that!
# The internal Libav library created during the build is installed under
# the build tree by default and should not be placed anywhere else!
# If you want to specify a custom location for the final install that
# should be done in mplayer_options.

""" + option_helptext

def create_helpfile(filename, text):
    if not path.exists(filename):
        f = open(filename, 'w')
        f.write(text)
        f.close()

def main():
    usage = 'usage: %prog [options]'
    parser = OptionParser(usage=usage)
    parser.add_option('-s', '--shallow', action='store_true',
                      help='only shallow git clone (uses less bandwidth)')
    parser.add_option('--init-optionfiles-only', action='store_true',
                      help='do nothing but create initial option files')
    parser.set_defaults(shallow=False, init_optionfiles_only=False)
    opts, args = parser.parse_args()
    if args:
        parser.print_help()
        sys.exit(1)

    create_helpfile('mplayer_options', mplayer_options)
    create_helpfile('libav_options', libav_options)
    create_helpfile('common_options', common_options)
    if opts.init_optionfiles_only:
        return

    git = GitWrapper()
    git.shallow = opts.shallow

    check_call('git submodule init'.split())
    git.submodule_clone('mplayer')
    git.submodule_clone('libav')
    git.submodule_clone('libass')

main()
