#!/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_csharp as CSharp
import lib_raise_fs as FS

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

def _configure():
	csc = CSharp.get_default_compiler()
	csc.debug = True
	csc.optimize_level = 2
	csc.warnings_all = True
	csc.warnings_as_errors = True
	return csc

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

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

	csc.build_program('main.exe', ['main.cs', 'lib_math.cs'])

	csc.run_print('main.exe')

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

	csc.build_shared_library('lib_math.dll', ['lib_math.cs'])
	csc.build_program('main.exe', ['main.cs'], ['-r:lib_math.dll'])

	csc.run_print('main.exe')

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

	# Build as a normal user
	def actual_build():
		csc.build_program('raise_example.exe', ['main.cs', 'lib_math.cs'])
	Users.do_as_normal_user(actual_build)

	# Install
	CSharp.uninstall_program('raise_example.exe', 'raise_example')
	CSharp.install_program('raise_example.exe', 'raise_example')

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

	# Remove
	CSharp.uninstall_program('raise_example.exe', 'raise_example')

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

	# Build as a normal user
	def actual_build():
		csc.build_shared_library('lib_math.dll', ['lib_math.cs'])
		csc.build_program('raise_example.exe', ['main.cs'], ['-r:lib_math.dll'])
	Users.do_as_normal_user(actual_build)

	# Install
	CSharp.uninstall_program('raise_example.exe', 'raise_example')
	CSharp.uninstall_library('lib_math.dll', 'raise_example')
	CSharp.install_program('raise_example.exe', 'raise_example')
	CSharp.install_library('lib_math.dll', 'raise_example')

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

	# Remove
	CSharp.uninstall_program('raise_example.exe', 'raise_example')
	CSharp.uninstall_library('lib_math.dll', 'raise_example')

