<?xml version="1.0" encoding="UTF-8"?>
<project name="aCis_gameserver" default="dist" basedir=".">

	<property name="src" location="java" />
	<property name="lib" location="lib" />
	<property name="build" location="build" />
	<property name="build.classes" location="${build}/classes" />
	<property name="build.dist" location="${build}/dist" />
	<property name="build.dist.login" location="${build.dist}/login" />
	<property name="build.dist.game" location="${build.dist}/gameserver" />

	<path id="classpath">
		<fileset dir="${lib}">
			<include name="*.jar" />
		</fileset>
	</path>

	<pathconvert property="manifest.libs" pathsep=" ">
		<path refid="classpath" />
		<mapper>
			<chainedmapper>
				<flattenmapper />
				<globmapper from="*.jar" to="../libs/*.jar" />
			</chainedmapper>
		</mapper>
	</pathconvert>

	<target name="init" depends="clean,checkRequirements,version" description="Create the output directories.">
		<mkdir dir="${build}" />
		<mkdir dir="${build.classes}" />
		<mkdir dir="${build.dist}" />
		<mkdir dir="${build.dist.login}" />
		<mkdir dir="${build.dist.game}" />
	</target>

	<target name="compile" depends="init" description="Compile the source.">
		<javac srcdir="${src}" classpathref="classpath" destdir="${build.classes}" compiler="javac1.7" debug="true" debuglevel="lines,vars,source" includeantruntime="false" source="1.7" target="1.7" />
	</target>

	<target name="jar" depends="compile" description="Create the jar file.">
		<jar destfile="${build}/l2jserver.jar">
			<fileset dir="${build.classes}" />
			<manifest>
				<attribute name="Main-Class" value="net.sf.l2j.Server" />
				<attribute name="Class-Path" value="${manifest.libs}" />
			</manifest>
		</jar>

		<copy todir="${build.dist.login}/libs">
			<fileset dir="${build}">
				<include name="l2jserver.jar" />
			</fileset>
		</copy>

		<copy todir="${build.dist.game}/libs">
			<fileset dir="${build}">
				<include name="l2jserver.jar" />
			</fileset>
		</copy>
	</target>

	<target name="dist" depends="jar">
		<copy todir="${build.dist.login}/libs">
			<fileset dir="${src}/../lib">
				<include name="c3p0-0.9.2-pre1.jar" />
				<include name="mchange-commons-0.2.jar" />
				<include name="mysql-connector-5.1.22.jar" />
				<include name="mmocore.jar" />
			</fileset>
		</copy>
		<copy todir="${build.dist.game}/libs">
			<fileset dir="${src}/../lib">
				<include name="*.jar" />
			</fileset>
		</copy>
		<copy todir="${build.dist.login}">
			<fileset dir="dist">
				<include name="startAccountManager.*" />
				<include name="startSQLAccountManager.*" />
				<include name="LoginServer_loop.sh" />
				<include name="startLoginServer.*" />
				<include name="RegisterGameServer.*" />
			</fileset>
		</copy>
		<copy todir="${build.dist.game}">
			<fileset dir="dist">
				<include name="GameServer_loop.sh" />
				<include name="startGameServer.*" />
			</fileset>
		</copy>
		<fixcrlf srcdir="${build.dist.game}" eol="lf" eof="remove" includes="**/*.sh" />
		<fixcrlf srcdir="${build.dist.login}" eol="lf" eof="remove" includes="**/*.sh" />
		<fixcrlf srcdir="${build.dist.game}" eol="crlf" eof="remove" includes="**/*.bat" />
		<fixcrlf srcdir="${build.dist.login}" eol="crlf" eof="remove" includes="**/*.bat" />
		<mkdir dir="${build.dist.game}/log" />
		<mkdir dir="${build.dist.login}/log" />
		<mkdir dir="${build.dist.game}/config" />
		<mkdir dir="${build.dist.login}/config" />
		<copy todir="${build.dist.game}/config">
			<fileset dir="config">
				<include name="*.properties" />
				<exclude name="loginserver.properties" />
			</fileset>
			<fileset dir="config">
				<include name="log.cfg" />
				<include name="console.cfg" />
			</fileset>
		</copy>
		<copy todir="${build.dist.login}/config">
			<fileset dir="config">
				<include name="loginserver.properties" />
			</fileset>
			<fileset dir="config">
				<include name="log.cfg" />
				<include name="banned_ip.cfg" />
				<include name="console.cfg" />
			</fileset>
		</copy>
		<mkdir dir="${build.dist.game}/data" />
	</target>

	<target name="clean" description="Remove the output directories.">
		<delete dir="${build}" />
	</target>

	<target name="checkRequirements" description="Check requirements.">
		<echo message="Verification of your JDK version."/>
		<available classname="java.lang.AutoCloseable" property="JDK7.present" />
		<fail unless="JDK7.present" message="Java 1.7 is required, but your version is Java ${ant.java.version}. Install latest JDK." />
	</target>

	<target name="version" description="Create version files.">
		<tstamp>
			<format property="date" pattern="yyyy-MM-dd HH:mm:ss" />
		</tstamp>
		<exec dir="." executable="./subversion/svnversion" outputproperty="revision" failifexecutionfails="false">
			<arg line="-n ." />
		</exec>
		<echo message="aCis gameserver revision: ${revision}" />
	</target>

</project>