# vim: set ts=2 sw=2 tw=99 noet ft=python: 
# 
# Copyright (C) 2004-2012 David Anderson
# 
# This file is part of SourcePawn.
# 
# SourcePawn 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.
# 
# SourcePawn 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
# SourcePawn. If not, see http://www.gnu.org/licenses/.
#
import os
import os.path
import shutil
import ambuild.osutil as osutil
from ambuild.command import Command

job = AMBuild.AddJob('dist')

# Single file copy
class CopyFile(Command):
	def __init__(self, filename, fromPath, toPath):
		Command.__init__(self)
		self.fromPath = os.path.join(fromPath, filename)
		self.toPath = toPath

	def run(self, runner, job):
		runner.PrintOut('copy {0} to {1}'.format(self.fromPath, self.toPath))
		shutil.copy(self.fromPath, self.toPath)

# Create a folder
class CreateFolder(Command):
	def __init__(self, folder):
		Command.__init__(self)
		self.folder = folder

	def run(self, runner, job):
		runner.PrintOut('mkdir {0}'.format(self.folder))
		os.makedirs(self.folder)


def ShouldCopy(filename, fromPath, toPath):
	fromfile = os.path.join(fromPath, filename)
	tofile = os.path.join(AMBuild.outputFolder, 'dist', toPath, filename)
	if not osutil.FileExists(tofile):
		return True
	return osutil.IsFileNewer(fromfile, tofile)

def AddCopy(job, filename, fromPath, toPath):
	if not ShouldCopy(filename, fromPath, toPath):
		return
	job.AddCommand(CopyFile(filename, fromPath, toPath))

def AddFolder(job, folder):
	if not os.path.exists(os.path.join(AMBuild.outputFolder, 'dist', folder)):
		job.AddCommand(CreateFolder(folder))

libkeima = osutil.StaticLibPrefix() + 'keima' + osutil.StaticLibSuffix()
spshell = 'spshell' + osutil.ExecutableSuffix()

AddFolder(job, 'include')
AddFolder(job, 'lib')
AddFolder(job, 'bin')

AddCopy(job,
        'keima.h',
        os.path.join(AMBuild.sourceFolder, 'include'),
        'include')
AddCopy(job,
        'keima_util.h',
        os.path.join(AMBuild.sourceFolder, 'include'),
        'include')
AddCopy(job,
        libkeima,
        os.path.join(AMBuild.outputFolder, 'keima'),
        'lib')
AddCopy(job,
        spshell,
        os.path.join(AMBuild.outputFolder, 'spshell'),
        'bin')

