<?xml version="1.0"?>
<project name="MonoGame Build Script" default="build" basedir=".">

  <description>The MonoGame automated build script.</description>

  <property name="os" value="${operating-system::get-platform(environment::get-operating-system())}" />
  <property name="msbuild12dir" value="C:\Program Files (x86)\MSBuild\12.0\Bin" />
  <property name="msbuild14dir" value="C:\Program Files (x86)\MSBuild\14.0\Bin" />
  
  <!-- Helper default target. -->
  <target   name="build" 
            description="Build, run tests, generate docs, and create installers." 
            depends="build_code,run_tests,build_docs,build_installer" />

  <!-- Some additional helper dependencies. -->
  <target name="_default" depends="_checkos" />
  <target name="_prebuild" depends="_default,_clean" />

  <!-- Do some OS checks required by the other steps. -->
  <target name="_checkos">
    <property name="os" value="${operating-system::get-platform(environment::get-operating-system())}"/>
    <if test="${os == 'Unix'}">
      <if test="${directory::exists('/Applications') and directory::exists('/Library')}">
        <property name="os" value="MacOS"/>
      </if>
    </if>
    <echo message="Detected : ${os}"/>
  </target>

  <!-- Clean the build output directories. -->
  <target name="_clean">
    <delete dir="${project::get-base-directory()}\MonoGame.Framework\obj" />
    <delete dir="${project::get-base-directory()}\MonoGame.Framework.Content.Pipeline\obj" />
  </target>
  
  <!-- Shortcut for building all valid target platforms -->
  <target name="build_code" description="Build all the projects.">
    <call target="build_windows"/>
    <call target="build_windows10" />
    <call target="build_web" />
    <call target="build_linux"/>
    <call target="build_android" />
    <call target="build_mac" />
    <call target="build_ios" />
  </target>
 


  <!-- Build targets for the various platforms. -->

  <target name="build_windows" description="Build Windows" depends="_prebuild">
    <if test="${os == 'Win32NT'}">
      <exec program="Protobuild" commandline="-generate Windows" />
      <exec program="${nuget3path}/nuget.exe" commandline="restore MonoGame.Framework.Windows.sln -NonInteractive" />
      <exec program="msbuild " commandline="MonoGame.Framework.Windows.sln /t:Clean /p:Configuration=Release" />
      <exec program="msbuild " commandline="MonoGame.Framework.Windows.sln /t:Build /p:Configuration=Release" />
      <exec program="msbuild " commandline="MonoGame.Framework.Windows.sln /t:Clean /p:Configuration=Debug" />
      <exec program="msbuild " commandline="MonoGame.Framework.Windows.sln /t:Build /p:Configuration=Debug" />
      <exec program="Protobuild" commandline="-generate WindowsGL" />
      <exec program="msbuild " commandline="MonoGame.Framework.WindowsGL.sln /t:Clean /p:Configuration=Release" />
      <exec program="msbuild " commandline="MonoGame.Framework.WindowsGL.sln /t:Build /p:Configuration=Release" />
      <call target="build_portable"/>
    </if>
    <if test="${os == 'Unix'}">
      <exec program="mono" commandline="Protobuild.exe -generate Windows" />
      <exec program="${nuget3path}/nuget.exe" commandline="restore MonoGame.Framework.Windows.sln -NonInteractive" />
      <exec program="msbuild " commandline="MonoGame.Framework.Windows.sln /t:Clean /p:Configuration=Release" />
      <exec program="msbuild " commandline="MonoGame.Framework.Windows.sln /t:Build /p:Configuration=Release" />
      <exec program="mono" commandline="Protobuild.exe -generate WindowsGL" />
      <exec program="msbuild " commandline="MonoGame.Framework.WindowsGL.sln /t:Clean /p:Configuration=Release" />
      <exec program="msbuild " commandline="MonoGame.Framework.WindowsGL.sln /t:Build /p:Configuration=Release" />
    </if>
  </target>

  <target name="build_portable" description="Build Portable" depends="_prebuild">
	
    <!-- Convert MonoGame.Framework.dll -->
    <mkdir dir="MonoGame.Framework\bin\Portable\AnyCPU" />
    <exec program="ThirdParty\Dependencies\Piranha\Piranha.exe " commandline='make-portable-skeleton -i MonoGame.Framework\bin\Windows\AnyCPU\Release\MonoGame.Framework.dll -o MonoGame.Framework\bin\Portable\AnyCPU\MonoGame.Framework.dll -p ".NETPortable,Version=v4.0,Profile=Profile328"' />

    <!-- Convert MonoGame.Framework.Net.dll -->
    <copy file="MonoGame.Framework\bin\Windows\AnyCPU\Release\MonoGame.Framework.Net.dll" tofile="MonoGame.Framework\bin\Portable\AnyCPU\temp.dll" overwrite="true"/>
    <exec program="ThirdParty\Dependencies\Piranha\Piranha.exe " workingdir="MonoGame.Framework\bin\Portable\AnyCPU" commandline='make-portable-skeleton -i temp.dll -o MonoGame.Framework.Net.dll -p ".NETPortable,Version=v4.0,Profile=Profile328"' />
    <delete file="MonoGame.Framework\bin\Portable\AnyCPU\temp.dll" />

    <!-- Convert MonoGame.Framework.Content.Pipeline.dll -->
    <mkdir dir="MonoGame.Framework.Content.Pipeline\bin\Portable\AnyCPU" />
    <copy file="MonoGame.Framework\bin\Portable\AnyCPU\MonoGame.Framework.dll" tofile="MonoGame.Framework.Content.Pipeline\bin\Portable\AnyCPU\MonoGame.Framework.dll" overwrite="true"/>
    <copy file="MonoGame.Framework.Content.Pipeline\bin\Windows\AnyCPU\Release\MonoGame.Framework.Content.Pipeline.dll" tofile="MonoGame.Framework.Content.Pipeline\bin\Portable\AnyCPU\temp.dll" overwrite="true"/>
    <exec program="ThirdParty\Dependencies\Piranha\Piranha.exe " workingdir="MonoGame.Framework.Content.Pipeline\bin\Portable\AnyCPU" commandline='make-portable-skeleton -i temp.dll -o MonoGame.Framework.Content.Pipeline.dll -p ".NETPortable,Version=v4.0,Profile=Profile328"' />
    <delete file="MonoGame.Framework.Content.Pipeline\bin\Portable\AnyCPU\temp.dll" />

  </target>

  <target name="build_linux" description="Build Linux" depends="_prebuild">
    <if test="${os == 'Win32NT'}">
      <exec program="Protobuild" commandline="-generate Linux" />
      <exec program="${nuget3path}/nuget.exe" commandline="restore MonoGame.Framework.Linux.sln -NonInteractive" />
      <exec program="msbuild " commandline="MonoGame.Framework.Linux.sln /t:Clean /p:Configuration=Release" />
      <exec program="msbuild " commandline="MonoGame.Framework.Linux.sln /t:Build /p:Configuration=Release" />
      <exec program="msbuild " commandline="MonoGame.Framework.Linux.sln /t:Clean /p:Configuration=Debug" />
      <exec program="msbuild " commandline="MonoGame.Framework.Linux.sln /t:Build /p:Configuration=Debug" />
    </if>
    <if test="${os == 'Unix'}">
      <exec program="mono" commandline="Protobuild.exe -generate Linux" />
      <exec program="${nuget3path}/nuget.exe" commandline="restore MonoGame.Framework.Linux.sln -NonInteractive" />
      <exec program="msbuild" commandline="/t:Clean /p:Configuration=Release MonoGame.Framework.Linux.sln" />
      <exec program="msbuild" commandline="/t:Build /p:Configuration=Release MonoGame.Framework.Linux.sln" />
      <exec program="msbuild" commandline="/t:Clean /p:Configuration=Debug MonoGame.Framework.Linux.sln" />
      <exec program="msbuild" commandline="/t:Build /p:Configuration=Debug MonoGame.Framework.Linux.sln" />
    </if>
    <if test="${os == 'MacOS'}">
      <exec program="mono" commandline="Protobuild.exe -generate Linux" />
      <exec program="${nuget3path}/nuget.exe" commandline="restore MonoGame.Framework.Linux.sln -NonInteractive" />
      <exec program="msbuild" commandline="MonoGame.Framework.Linux.sln /t:Clean /p:Configuration=Release" />
      <exec program="msbuild" commandline="MonoGame.Framework.Linux.sln /t:Build /p:Configuration=Release" />
      <exec program="msbuild" commandline="MonoGame.Framework.Linux.sln /t:Clean /p:Configuration=Debug" />
      <exec program="msbuild" commandline="MonoGame.Framework.Linux.sln /t:Build /p:Configuration=Debug" />
      
      <!--  
        The Linux build is considered the DesktopGL build.  For this 
        reason need the Mac ffmpeg binaries to be able to run the 
        Linux unit tests on a Mac system.
      -->
      <copy file="ThirdParty/Dependencies/ffmpeg/MacOS/ffmpeg" tofile="Test/bin/Linux/AnyCPU/Debug/ffmpeg" overwrite="true"/>
      <copy file="ThirdParty/Dependencies/ffmpeg/MacOS/ffprobe" tofile="Test/bin/Linux/AnyCPU/Debug/ffprobe" overwrite="true"/>
            
    </if>
  </target>

  <target name="build_web" description="Build Web JSIL" depends="_prebuild">
    <if test="${os == 'Win32NT'}">
      <exec program="Protobuild" commandline="-generate Web">
        <environment>
          <variable name="JSIL_DIRECTORY" value="C:\JSIL" />
        </environment>
      </exec>
      <exec program="msbuild " commandline="MonoGame.Framework.Web.sln /t:Clean /p:Configuration=Release" />
      <exec program="msbuild " commandline="MonoGame.Framework.Web.sln /t:Build /p:Configuration=Release" />
    </if>
    <if test="${os == 'Unix'}">
      <exec program="mono" commandline="Protobuild.exe -generate Web" />
      <exec program="msbuild " commandline="MonoGame.Framework.Web.sln /t:Clean /p:Configuration=Release" />
      <exec program="msbuild " commandline="MonoGame.Framework.Web.sln /t:Build /p:Configuration=Release" />
    </if>
  </target>

  <target name="build_mac" description="Build MacOS" depends="_prebuild">
    <if test="${os == 'MacOS'}">
      <exec program="mono" commandline="Protobuild.exe -generate MacOS" />
      <exec program="${nuget3path}/nuget.exe" commandline="restore MonoGame.Framework.MacOS.sln -NonInteractive" />
      <exec program="msbuild" commandline="MonoGame.Framework.MacOS.sln  /t:Clean /p:Configuration=Release" />
      <exec program="msbuild" commandline="MonoGame.Framework.MacOS.sln  /t:Build /p:Configuration=Release" />
      <exec program="msbuild" commandline="MonoGame.Framework.MacOS.sln  /t:Clean /p:Configuration=Debug" />
      <exec program="msbuild" commandline="MonoGame.Framework.MacOS.sln  /t:Build /p:Configuration=Debug" />
    </if>
  </target>

  <target name="build_ios" description="Build iOS" depends="_prebuild">
    <if test="${os == 'MacOS'}">
      <if test="${file::exists('/Developer/MonoTouch/MSBuild/Xamarin.ObjcBinding.CSharp.targets') or file::exists('/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Xamarin.ObjcBinding.CSharp.targets')}">
        <exec program="mono" commandline="Protobuild.exe -generate iOS" />
        <exec program="msbuild" commandline="MonoGame.Framework.iOS.sln /t:Clean /p:Configuration=Release" />
        <exec program="msbuild" commandline="MonoGame.Framework.iOS.sln /t:Build /p:Configuration=Release" />
      </if>
      <if test="${file::exists('/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/TVOS/Xamarin.TVOS.CSharp.targets')}" >
        <exec program="mono" commandline="Protobuild.exe -generate tvOS" />
        <exec program="msbuild" commandline="MonoGame.Framework.tvOS.sln /t:Clean /p:Configuration=Release" />
        <exec program="msbuild" commandline="MonoGame.Framework.tvOS.sln /t:Build /p:Configuration=Release" />
      </if>
    </if>
  </target>

  <target name="build_android" description="Build Android" depends="_prebuild">
    <if test="${os == 'Win32NT'}">
      <if test="${file::exists('C:\Program Files (x86)\MSBuild\Novell\Novell.MonoDroid.CSharp.targets')}">
        <exec program="Protobuild" commandline="-generate Android" />
        <exec program="msbuild " commandline="MonoGame.Framework.Android.sln /t:Clean /p:Configuration=Release" />
        <exec program="msbuild " commandline="MonoGame.Framework.Android.sln /t:Build /p:Configuration=Release" />
      </if>
    </if>
    <if test="${os == 'MacOS'}">
      <if test="${file::exists('/Developer/MonoAndroid/usr/bin/mandroid')}">
        <exec program="mono" commandline="Protobuild.exe -generate Android" />
        <exec program="msbuild" commandline="MonoGame.Framework.Android.sln /t:Clean /p:Configuration=Release" />
        <exec program="msbuild" commandline="MonoGame.Framework.Android.sln /t:Build /p:Configuration=Release" />
      </if>
    </if>
  </target>

  <!-- old targets left here for TeamCity -->
  <target name="build_windows8" />
  <target name="build_windowsphone81" />

  <target name="build_windows10" description="Build Windows 10 UAP" depends="_prebuild">
    <if test="${os == 'Win32NT'}">
      <if test="${file::exists('c:\Program Files (x86)\MSBuild\Microsoft\WindowsXaml\v14.0\Microsoft.Windows.UI.Xaml.CSharp.targets')}">
        <exec program="Protobuild" commandline="-generate WindowsUniversal" />

        <!--
            Currently MSBuild doesn't restore the nuget packages like buiding from the
            IDE does.  So we have to do it ourselves using the recommended work around.
        -->
        <exec program="${nuget3path}/nuget.exe " commandline='restore MonoGame.Framework\MonoGame.Framework.WindowsUniversal.project.json -NonInteractive' />
        <exec program="${nuget3path}/nuget.exe " commandline='restore MonoGame.Framework\MonoGame.Framework.Net.WindowsUniversal.project.json -NonInteractive' />

        <exec program="${msbuild14dir}\msbuild.exe " commandline='MonoGame.Framework.WindowsUniversal.sln /t:Clean /p:Configuration=Release /p:Platform="Any CPU"' />
        <exec program="${msbuild14dir}\msbuild.exe " commandline='MonoGame.Framework.WindowsUniversal.sln /t:Build /p:Configuration=Release /p:Platform="Any CPU"' />

      </if>
    </if>
  </target>


  <!-- Run the unit tests... will fail if the code hasn't been built. -->
  <target name="run_tests" description="Run all the tests" depends="_default">

    <if test="${os == 'Win32NT'}">
      <exec program="MonoGameTests.exe" commandline="--teamcity" workingdir="Test/bin/Windows/AnyCPU/Debug/" basedir="Test/bin/Windows/AnyCPU/Debug/"/>
    </if>

    <if test="${os == 'MacOS'}">
      <exec program="chmod" commandline="u+x Test/bin/Linux/AnyCPU/Debug/ffmpeg" />
      <exec program="chmod" commandline="u+x Test/bin/Linux/AnyCPU/Debug/ffprobe" />
      <exec program="mono" commandline="MonoGameTests.exe --teamcity" workingdir="Test/bin/Linux/AnyCPU/Debug/" />
    </if>

  </target>


  <!-- Generate the docs... will fail if the code hasn't been built. -->
  <target name="build_docs" description="Build the documentation." depends="_default">
    <if test="${os == 'Win32NT'}">
      <delete dir="${project::get-base-directory()}\Documentation\Output" />
      <exec program="SharpDoc.exe" basedir="ThirdParty\Dependencies\SharpDoc" commandline="-config Documentation\config.xml" />
    </if>
  </target>


  <!-- Create the installers... will fail if the code hasn't been built. -->
  <target name="build_installer" description="Build the installers." depends="_default">
    <nant buildfile="ProjectTemplates/VisualStudio2010/default.build" target="build" />
    <nant buildfile="ProjectTemplates/VisualStudio2013/default.build" target="build" />
    <nant buildfile="ProjectTemplates/VisualStudio2015/default.build" target="build" />
    <nant buildfile="ProjectTemplates/VisualStudio2017/default.build" target="build" />
    <nant buildfile="ProjectTemplates/DotNetTemplate/default.build" target="build" />
    <nant buildfile="IDE/MonoDevelop/default.build" target="build" />
    <nant buildfile="IDE/VisualStudioForMac/default.build" target="build" />
    <nant buildfile="Installers/default.build" target="build" />
  </target>

</project>