#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import lib_raise_terminal as Print
import lib_raise_find as Find
import lib_raise_users as Users
import lib_raise_cxx as CXX
import lib_raise_ar as AR
import lib_raise_fs as FS
import lib_raise_config as Config


def clean():
	"""Removes any files and directories generated when building."""
	FS.remove_binaries('lib_math')
	FS.remove_binaries('main')

def _configure():
	cxx = CXX.cxx_compilers[Config.arg]
	cxx.debug = True
	cxx.position_independent_code = True
	cxx.optimize_level = 2
	cxx.warnings_all = True
	cxx.warnings_as_errors = True

	os.environ['LD_LIBRARY_PATH'] = os.getcwd()
	return cxx

def setup_failure():
	# Empty the path and reset the CXX module so it finds no compilers
	os.environ['PATH'] = ''
	CXX.is_setup = False
	CXX.cxx_compilers = {}
	CXX.setup()

def build_object():
	clean()
	cxx = _configure()

	cxx.build_object('lib_math.o', ['lib_math.cc'])
	cxx.build_object('main.o', ['main.cc'])

	cxx.build_program('main.exe', ['lib_math.o', 'main.o'])

	CXX.run_print('./main.exe')

def build_program():
	clean()
	cxx = _configure()

	cxx.build_program('main.exe', ['lib_math.cc', 'main.cc'])

	CXX.run_print('./main.exe')

def build_shared_library():
	clean()
	cxx = _configure()

	# Build the library
	cxx.build_object('lib_math.o', ['lib_math.cc'])
	cxx.build_shared_library('lib_math.so', ['lib_math.o'])

	cxx.build_program('main.exe', ['main.cc', 'lib_math.so'])

	CXX.run_print('./main.exe')

def build_static_library():
	clean()
	cxx = _configure()

	# Build the library
	cxx.build_object('lib_math.o', ['lib_math.cc'])
	AR.build_static_library('lib_math.a', ['lib_math.o'])

	cxx.build_program('main.exe', ['main.cc', 'lib_math.a'])

	CXX.run_print('./main.exe')

# Tests that require root
def install_and_uninstall_program():
	# Configure
	Users.require_root()
	clean()
	cxx = _configure()

	# Build as a normal user
	def actual_build():
		FS.remove_binaries('raise_example')
		cxx.build_program('raise_example.exe', ['main.cc', 'lib_math.cc'])
	Users.do_as_normal_user(actual_build)

	# Install
	CXX.uninstall_program('raise_example.exe')
	CXX.install_program('raise_example.exe')

	# Run
	CXX.run_print('raise_example.exe')

	# Remove
	CXX.uninstall_program('raise_example.exe')

def install_and_uninstall_shared_library():
	# Configure
	Users.require_root()
	clean()
	cxx = _configure()

	# Build as a normal user
	def actual_build():
		cxx.build_object('lib_math.o', ['lib_math.cc'])
		cxx.build_shared_library('lib_math.so', ['lib_math.o'])
		cxx.build_program('raise_example.exe', ['main.cc', 'lib_math.so'])
	Users.do_as_normal_user(actual_build)

	# Install
	CXX.uninstall_program('raise_example.exe')
	CXX.uninstall_library('lib_math.so')
	CXX.install_program('raise_example.exe')
	CXX.install_library('lib_math.so')

	# Run
	CXX.run_print('raise_example.exe')

	# Remove
	CXX.uninstall_program('raise_example.exe')
	CXX.uninstall_library('lib_math.so')

def install_and_uninstall_static_library():
	# Configure
	Users.require_root()
	clean()
	cxx = _configure()

	# Build as a normal user
	def actual_build():
		cxx.build_object('lib_math.o', ['lib_math.cc'])
		AR.build_static_library('lib_math.a', ['lib_math.o'])
		cxx.build_program('raise_example.exe', ['main.cc', 'lib_math.a'])
	Users.do_as_normal_user(actual_build)

	# Install
	CXX.uninstall_program('raise_example.exe')
	CXX.uninstall_library('lib_math.a')
	CXX.install_program('raise_example.exe')
	CXX.install_library('lib_math.a')

	# Run
	CXX.run_print('raise_example.exe')

	# Remove
	CXX.uninstall_program('raise_example.exe')
	CXX.uninstall_library('lib_math.a')

