#!/usr/bin/python

# Copyright (c) 2012 Oliver Burger obgr_seneca@mageia.org
#
# This file is part of videoconvert.
#
# videoconvert is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# videoconvert is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with videoconvert.  If not, see <http://www.gnu.org/licenses/>.

videoconvertVersion = '0.1'

import os
import sys
import getopt

# self written imports
import videoconvert.Conf as Conf
import videoconvert.SystemCall as SystemCall

# check for the command line parameters
arguments = sys.argv
commandCall = arguments.pop(0)
optList, arguments = getopt.getopt(arguments, 'cvhr:f:', ['configure', 'resolution=',
  'framerate=', 'container=', 'audiocodec=', 'videocodec=', 'keep', 'debug', 'help',
  'version'])

configure=False
resolution=False
framerate=False
container=False
audioCodec=False
videoCodec=False
keep=False
debug=False
outputHelp=False
version=False

for param in optList:
    if (param[0] == '--configure' or param[0] == '-c'):
        configure=True
    if (param[0] == '--keep'):
        keep=True
    if (param[0] == '--resolution' or param[0] == '-r'):
        resolution=param[1]
    if (param[0] == '--framerate' or param[0] == '-f'):
        framerate=param[1]
    if (param[0] == '--container'):
        container=param[1]
    if (param[0] == '--audiocodec'):
        audioCodec=param[1]
    if (param[0] == '--videocodec'):
        videoCodec=param[1]
    if (param[0] == '--debug'):
        debug=True
    if (param[0] == '--help' or param[0] == '-h'):
        outputHelp=True
    if (param[0] == '--version' or param[0] == '-v'):
        version=videoconvertVersion

conf = Conf.Configuration(debug)
confFile = conf.getConfFile()

# if no config file exists or --configure or -c is given ask the user for his preffered settings
# otherwise read the config file
if (os.path.exists(confFile) == False or configure == True):
    conf.getNewConfig()
else:
    conf.readConfig()

# set parameters given on the command line
if framerate != False:
    conf.setFramerate(framerate)
if resolution != False:
    conf.setResolution(resolution)
if container != False:
    conf.setContainer(container)
if audioCodec != False:
    conf.setAudioCodec(audioCodec)
if videoCodec != False:
    conf.setVideoCodec(videoCodec)

# read the remaining arguments from the command line
argumentIsGiven = False
if (len(arguments) >= 1 and outputHelp == False and version == False):
    argumentIsGiven = True
    isPath = True
    if os.path.isdir(arguments[0]):
        inDir = arguments.pop(0)
        inFile = ''
    elif os.path.isfile(arguments[0]):
        inFile = arguments.pop(0)
        inDir = ''
    else:
        isPath = False
    if len(arguments) >= 1:
        name = arguments.pop(0)
    else:
        name = 'videoconvert'

    # if a directory is given try to sync vdr files in that directory and convert them
    # if a file is given only convert it
    if isPath:
        systemCall = SystemCall.SystemCall(conf,keep,debug)
        if inDir != '':
            systemCall.sync(inDir,name)
        elif inFile != '':
            systemCall.convertFile(inFile,os.getcwd(),os.getcwd())

if ((argumentIsGiven == False or isPath == False) and outputHelp == False and version == False):
    print 'Usage : '+commandCall+' [-c|--config] [--keep] [-f <framerate>|--framerate=<framerate>] [-r <resolution>|--resolution=<resolution>] <input directory> [<name>]'
if debug:
    print conf.toString()
if outputHelp:
    print 'Help, I need somebody\'s help...'
if version != False:
    print 'videoconvert version ' + version
