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

import os
import lib_raise_c as C
import lib_raise_linker as Linker
import lib_raise_ar as AR
import lib_raise_fs as FS
import lib_raise_find as Find
import lib_raise_users as Users
import lib_raise_process as Process


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

def _configure():
	cc = C.get_default_compiler()
	cc.debug = True
	cc.optimize_level = 2
	cc.warnings_all = True
	cc.warnings_as_errors = True
	return cc

def test():
	"""Builds the program, and runs it."""
	# Setup
	Users.require_not_root()
	clean()
	cc = _configure()

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

	# Build the program and link against the library
	cc.build_program('raise_example.exe', ['main.c'], ['libexample.a'])

	# Run the program
	C.run_print('./raise_example.exe')

def _remove():
	C.uninstall_program('raise_example.exe')
	C.uninstall_library('libexample.a')
	C.uninstall_header('libexample.h')

def remove():
	"""Uninstalls the program."""
	# Setup
	Users.require_root()
	clean()
	cc = _configure()

	# Remove the files
	_remove()

	Linker.ldconfig()

def install():
	"""Installs the program."""
	# Setup
	Users.require_root()
	clean()
	cc = _configure()

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

	# Remove old install
	_remove()

	# Install
	C.install_program('raise_example.exe')
	C.install_library('libexample.a')
	C.install_header('libexample.h')

	Linker.ldconfig()



