<?xml version="1.0" encoding="UTF-8"?>
<!--
~ JBoss, Home of Professional Open Source
~ Copyright 2014 Red Hat Inc. and/or its affiliates and other contributors
~ as indicated by the @authors tag. All rights reserved.
~ See the copyright.txt in the distribution for a
~ full listing of individual contributors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~ http://www.apache.org/licenses/LICENSE-2.0
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <!-- Parent -->
    <parent>
        <groupId>org.aesh</groupId>
        <artifactId>readline-all</artifactId>
        <version>3.5</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>terminal-tty</artifactId>
    <packaging>jar</packaging>
    <name>Aesh Terminal TTY</name>
    <description>Aesh (Another Extendable SHell) Terminal TTY - Local terminal implementation</description>
    <scm>
        <connection>scm:git:git://github.com/aeshell/aesh-readline.git</connection>
        <developerConnection>scm:git:ssh://git@github.com/aeshell/aesh-readline.git</developerConnection>
        <url>https://github.com/aeshell/aesh-readline/tree/master</url>
    </scm>

    <!-- Licenses -->
    <licenses>
        <license>
            <name>Apache License, Version 2.0</name>
            <url>http://www.apache.org/licenses/LICENSE-2.0</url>
        </license>
    </licenses>

    <properties>
        <javaModuleName>org.aesh.terminal.tty</javaModuleName>
    </properties>

    <developers>
        <developer>
            <name>Jean-Francois Denise</name>
            <email>jdenise@redhat.com</email>
            <organization>Red Hat</organization>
            <organizationUrl>http://www.redhat.com</organizationUrl>
        </developer>
        <developer>
            <name>Stale Pedersen</name>
            <email>spederse@redhat.com</email>
            <organization>Red Hat</organization>
            <organizationUrl>http://www.redhat.com</organizationUrl>
        </developer>
    </developers>

    <dependencies>
        <dependency>
            <groupId>org.aesh</groupId>
            <artifactId>terminal-api</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                    <trimStackTrace>false</trimStackTrace>
                    <includes>
                        <include>**/*TestCase.java</include>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                        <manifestEntries>
                            <Automatic-Module-Name>${javaModuleName}</Automatic-Module-Name>
                        </manifestEntries>
                        <index>true</index>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>test-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>native-windows</id>
            <activation>
                <os><family>windows</family></os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>compile-native-dll</id>
                                <phase>process-classes</phase>
                                <goals><goal>run</goal></goals>
                                <configuration>
                                    <target xmlns:if="ant:if">
                                        <mkdir dir="${project.build.outputDirectory}/native/windows-x86_64"/>
                                        <!-- Try MSVC (cl) first -->
                                        <exec executable="cl" dir="${project.build.directory}"
                                              resultproperty="cl.result"
                                              failonerror="false" failifexecutionfails="false">
                                            <arg value="/LD"/>
                                            <arg value="/O2"/>
                                            <arg value="/I${env.JAVA_HOME}\include"/>
                                            <arg value="/I${env.JAVA_HOME}\include\win32"/>
                                            <arg value="/Fe:${project.build.outputDirectory}\native\windows-x86_64\aesh-console.dll"/>
                                            <arg value="${project.basedir}\src\main\native\WinConsoleNative.c"/>
                                            <arg value="kernel32.lib"/>
                                        </exec>
                                        <!-- Fall back to MinGW gcc if cl is not available -->
                                        <condition property="cl.failed">
                                            <not><equals arg1="${cl.result}" arg2="0"/></not>
                                        </condition>
                                        <exec executable="gcc" dir="${project.build.directory}"
                                              failonerror="false" failifexecutionfails="false"
                                              if:set="cl.failed">
                                            <arg value="-shared"/>
                                            <arg value="-O2"/>
                                            <arg value="-I${env.JAVA_HOME}\include"/>
                                            <arg value="-I${env.JAVA_HOME}\include\win32"/>
                                            <arg value="-o"/>
                                            <arg value="${project.build.outputDirectory}\native\windows-x86_64\aesh-console.dll"/>
                                            <arg value="${project.basedir}\src\main\native\WinConsoleNative.c"/>
                                            <arg value="-lkernel32"/>
                                        </exec>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>cross-windows</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>cross-compile-native-dll</id>
                                <phase>process-classes</phase>
                                <goals><goal>run</goal></goals>
                                <configuration>
                                    <target>
                                        <mkdir dir="${project.build.outputDirectory}/native/windows-x86_64"/>
                                        <exec executable="x86_64-w64-mingw32-gcc" dir="${project.basedir}/src/main/native"
                                              failonerror="true">
                                            <arg value="-shared"/>
                                            <arg value="-O2"/>
                                            <arg value="-I${env.JAVA_HOME}/include"/>
                                            <arg value="-I${project.basedir}/src/main/native/win32"/>
                                            <arg value="-o"/>
                                            <arg value="${project.build.outputDirectory}/native/windows-x86_64/aesh-console.dll"/>
                                            <arg value="WinConsoleNative.c"/>
                                            <arg value="-lkernel32"/>
                                        </exec>
                                        <!-- Copy the compiled DLL back to src/main/resources so it can be committed -->
                                        <mkdir dir="${project.basedir}/src/main/resources/native/windows-x86_64"/>
                                        <copy file="${project.build.outputDirectory}/native/windows-x86_64/aesh-console.dll"
                                              todir="${project.basedir}/src/main/resources/native/windows-x86_64"
                                              overwrite="true"/>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>
