#!/usr/bin/python3
import subprocess
import os
import sys
import time;
from optparse import OptionParser
from scripts import version
from scripts import configfile
import re

from scripts.run import run

TARGETS = ['libbirdfont', 
           'libbirdgems', 
           'birdfont', 
           'birdfont-autotrace',
           'birdfont-export',
           'birdfont-import',
           'birdfont-test']
           
VERSION = version.VERSION

HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'

gee = '';

def test_program_version (program, a, b, c):
	print ('Checking for %s version >= %s.%s.%s' % (program, a, b, c))
	process = subprocess.Popen (program + ' --version', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
	v = process.stdout.readline().decode('utf-8')
	process.communicate()[0]
	if not process.returncode == 0:
		print (FAIL + 'Not found' + ENDC)
		exit (1)		
	print ('Found ' + v)
	
	o = v.split (' ');
	for s in o:
		if re.search( r'[0-9]*\.', s):
			v = s
			break
			
	v = re.sub(r'[a-zA-Z\-].*', '0', v)
	version = [int(n) for n in v.split ('.')]
	return [a,b,c] <= version	

def test_library_version (lib):
	print ('Looking for library: ' + lib + '\t\t')
	process = subprocess.Popen ('pkg-config --modversion ' + lib, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
	process.communicate()[0]
	return process.returncode == 0

def has_posixvala ():
	posixvala = test_library_version ('posixvala')
	if not posixvala:
		print (OKGREEN + 'Glib will be used instead of Posix (libc).' + ENDC)
		return 'False'
	else: 
		print (OKGREEN + 'Using posix profile.' + ENDC)			
	return 'True'

def configure(gtk, libbgee):
	global gee
	
	if not test_program_version ('valac', 0, 16, 0):
		print (FAIL + 'valac is too old.' + ENDC)
		exit (1)

	if gtk:
		libs = [
				'cairo', 
				'gdk-pixbuf-2.0',
				'gio-2.0', 
				'glib-2.0', 
				'gtk+-3.0',
				'webkitgtk-3.0', 
				'libsoup-2.4',
				'libnotify',
				'sqlite3',
				'xmlbird'
				]
	else:
		libs = [
				'gio-2.0', 
				'glib-2.0', 
				'sqlite3',
				'fontconfig',
				'xmlbird'
				]

	for lib in libs:
		if not test_library_version (lib):
			print (FAIL + 'Can not find ' + lib + ENDC)
			exit (1)

	if libbgee == 'Any':
		if test_library_version ('gee-0.8'):
			gee = 'gee-0.8'
		elif test_library_version ('gee-1.0'):
			gee = 'gee-1.0'
		else:
			print (FAIL + 'Can not find libgee (version 0.8 or version 1.0).' + ENDC)
			exit (1)
	else:
		if not test_library_version (libbgee):
			print (FAIL + 'Can not find lib gee.' + ENDC)
			exit (1)
		gee = libbgee;

	run ('mkdir -p build')
	run ('touch build/configured')

	print ('');
	print (OKGREEN + 'Done' + ENDC);


parser = OptionParser()
parser.add_option('-p', '--prefix', dest='prefix', help='Install prefix', metavar='PREFIX')
parser.add_option('-d', '--dest', dest='dest', help='Install to this directory', metavar='DEST')
parser.add_option('-c', '--cc', dest='cc', help='C compiler', metavar='CC')
parser.add_option('-g', '--gtk', dest='gtk', help='Build Gtk version, default is True', metavar='GTK')
parser.add_option('-e', '--gee', dest='gee', help='Version of libgee', metavar='GEE')
parser.add_option('-v', '--valac', dest='valac', help='Vala compiler', metavar='VALAC')
parser.add_option('-n', '--nonnull', dest='nonnull', action="store_true", help='Enable compiletime checks for null pointers', metavar='NONNULL')

parser.add_option('', '--valac-flags', dest='valac_flags', help='Vala compiler flags for all targets', metavar='VALAC_FLAGS', default='')
for target in TARGETS:
	parser.add_option('', '--valac-flags-' + target, dest='valac_flags_' + target, help='Vala compiler flags for ' + target, metavar='VALAC_FLAGS', default='')

parser.add_option('', '--cflags', dest='cflags', help='C compiler flags for all targets', metavar='CFLAGS', default='')
for target in TARGETS:
	parser.add_option('', '--cflags-' + target, dest='cflags_' + target, help='C compiler flags for ' + target, metavar='CFLAGS', default='')

parser.add_option('', '--ldflags', dest='ldflags', help='Linker flags for all targets', metavar='LDFLAGS', default='')
for target in TARGETS:
	parser.add_option('', '--ldflags-' + target, dest='ldflags_' + target, help='Linker flags for ' + target, metavar='LDFLAGS', default='')

(options, args) = parser.parse_args()
option_dict = vars(options)

valacflags = dict()
cflags = dict()
ldflags = dict()

for target in TARGETS:
	cflags[target] = options.cflags
	cflags[target] = cflags[target] + ' ' + option_dict.get('cflags_' + target, "")
	cflags[target] = cflags[target].strip()

	ldflags[target] = options.ldflags
	ldflags[target] = ldflags[target] + ' ' + option_dict.get('ldflags_' + target, "")
	ldflags[target] = ldflags[target].strip()
	
	valacflags[target] = options.valac_flags
	valacflags[target] = valacflags[target] + ' ' + option_dict.get('valac_flags_' + target, "")
	valacflags[target] = valacflags[target].strip()
	
if not options.prefix:
	if 'bsd' in sys.platform:
		options.prefix = '${DESTDIR}${PREFIX}'
	else:
		options.prefix = '/usr'
		
if not options.dest:
	options.dest = ''
if not options.cc:
	options.cc = 'gcc'
if not options.gtk:
	options.gtk = True
if options.gtk == 'False':
	options.gtk = False
if not options.gee:
	options.gee = 'Any'
if not options.valac:
	options.valac = 'valac'
if not options.nonnull:
	options.nonnull = False
else:
	options.nonnull = True
	
configure(options.gtk, options.gee)

configfile.write_config(options.prefix)
configfile.write_compile_parameters(options.prefix,
									 options.dest,
									 options.cc,
									 gee,
									 options.valac,
									 options.nonnull,
									 valacflags,
									 cflags,
									 ldflags,
									 options.gtk)
