<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <UsingTask TaskName="Firaxis.ModBuddy.Civ5ModBuildTasks.GenerateModInfo"
                AssemblyFile="$(MSBuildExtensionsPath)\Firaxis\ModBuddy\ModBuddy.Civ5ModBuildTasks.dll" />
    <UsingTask TaskName="Firaxis.ModBuddy.Civ5ModBuildTasks.PackageMod"
                AssemblyFile="$(MSBuildExtensionsPath)\Firaxis\ModBuddy\ModBuddy.Civ5ModBuildTasks.dll" />
    <UsingTask TaskName="Firaxis.ModBuddy.Civ5ModBuildTasks.RemoveEmptyDirectories"
                AssemblyFile="$(MSBuildExtensionsPath)\Firaxis\ModBuddy\ModBuddy.Civ5ModBuildTasks.dll" />

    <PropertyGroup>
        <StartAction>Program</StartAction>
        <StartProgram>$(Civ5Path)\Civ5Win32Release D3D11.exe</StartProgram>
    </PropertyGroup>

    <PropertyGroup>
        <ModPackageExtension>.civ5mod</ModPackageExtension>
        <ModPackageName>$(SafeName) (v $(ModVersion))</ModPackageName>
        <ModPackageFileName>$(ModPackageName)$(ModPackageExtension)</ModPackageFileName>
    </PropertyGroup>

    <PropertyGroup>
        <ProjectDir Condition="'$(BuildPath)'==''">$(MSBuildProjectDirectory)</ProjectDir>
        <BuildDir>$(ProjectDir)\..\Build\$(SafeName)\</BuildDir>
        <ModsDir>$(Civ5UserPath)\Mods\</ModsDir>
        <PackageDir>$(ProjectDir)\..\Packages\</PackageDir>
        <OutputDir>$(ModsDir)$(ModPackageName)\</OutputDir>
    </PropertyGroup>

    <Target Name="GetFrameworkPaths" />

    <Target Name="_CheckProperties">
        <Error Text="Civ5Path must be defined. Use the Project Properties window to set it to the location where Civlization 5 is installed."
                         Condition="('$(Civ5Path)' == '')" />

        <Error Text="Civ5UserPath must be defined. Use the Project Properties window to set it to the location where Civlization 5 Mods are installed."
                         Condition="('$(Civ5UserPath)' == '')" />
    </Target>

    <Target Name="Clean" DependsOnTargets="_CheckProperties">

        <RemoveDir Directories="$(BuildDir);$(OutputDir);$(PackageDir)" />
        <Delete Files="$(ModPackagePath)" />

    </Target>

    <Target Name="Rebuild" DependsOnTargets="_CheckProperties">
        <CallTarget Targets="Clean" />
        <CallTarget Targets="Deploy" />
    </Target>

    <Target Name ="Build" DependsOnTargets="_CheckProperties">

        <ItemGroup>
            <_ModFiles Include="@(Content)">
                <TargetPath>$(BuildDir)%(Content.Identity)</TargetPath>
                <TargetDir>$(BuildDir)%(Content.RelativeDir)</TargetDir>
            </_ModFiles>
        </ItemGroup>

        <Message Importance="High" Text="Creating target folder structure..." />
        <MakeDir Directories="$(BuildDir)" Condition = "!Exists('$(BuildDir)')"/>
        <MakeDir Directories="%(_ModFiles.TargetDir)" Condition = "!Exists('%(_ModFiles.TargetDir)')"/>

        <Message Importance="low" Text="Cleaning build folder..." />
        <ItemGroup>
            <BuildFilesToDelete Include="$(BuildDir)**\*.*" Exclude="@(Content->'$(BuildDir)%(RelativeDir)%(FileName)%(Extension)')" />
        </ItemGroup>

        <Delete Files="%(BuildFilesToDelete.FullPath)" />
        <RemoveEmptyDirectories Directories="$(BuildDir)" DeleteBaseFolder="false"/>

        <!-- Copy files to output folder -->
        <Message Importance="High" Text="Copying files..." />
        <Copy SourceFiles="@(_ModFiles)" SkipUnchangedFiles="true" DestinationFiles="%(_ModFiles.TargetPath)">
            <Output TaskParameter="CopiedFiles" ItemName="_CopiedFiles" />
        </Copy>

        <!-- Generate modinfo file -->
        <GenerateModInfo
          ID="$(Guid)"
          Version="$(ModVersion)"
          Stability="$(Stability)"
          Name="$(Name)"
          Teaser="$(Teaser)"
          Description="$(Description)"
          Authors="$(Authors)"
          SpecialThanks="$(SpecialThanks)"
          Homepage="$(Homepage)"
          AffectsSavedGames ="$(AffectsSavedGames)"
          MinCompatibleSaveVersion="$(MinCompatibleSaveVersion)"
          ReloadLandmarkSystem="$(ReloadLandmarkSystem)"
          ReloadUnitSystem="$(ReloadUnitSystem)"
          SupportsSinglePlayer="$(SupportsSinglePlayer)"
          SupportsMultiplayer="$(SupportsMultiplayer)"
          SupportsMac="$(SupportsMac)"
          Content="$(ModContent)"
          Actions="$(ModActions)"
          CustomProperties="$(ModProperties)"
          Files="@(_CopiedFiles)"
          TargetPath="$(BuildDir)$(ModPackageName).modinfo"/>
        
    </Target>

    <Target Name="Package" DependsOnTargets="Build">
        <!-- Create final package -->
        <Message Importance="High" Text="Packaging mod..." />
        <MakeDir Directories="$(PackageDir)" Condition = "!Exists('$(PackageDir)')"/>
        <Delete Files="$(PackageDir)$(ModPackageFileName)" />
        <ItemGroup>
            <PackagedModFiles Include="$(BuildDir)**\*.*" />
         </ItemGroup>
        <PackageMod TargetPath="$(PackageDir)$(ModPackageFileName)" BasePath="$(BuildDir)" Files="@(PackagedModFiles)" />
    </Target>

    <Target Name="Deploy" DependsOnTargets="Package">
        <Message Importance="High" Text="Deploying mod..." />

        <Message Text="$(ModPackageFileName)" />
        <ItemGroup>
            <DeployedModFiles Include="$(BuildDir)**\*.*" />
         </ItemGroup>

        <Message Text="@(DeployedModFiles)" />

        <MakeDir Directories="$(OutputDir)" Condition = "!Exists('$(OutputDir)')"/>

        <Message Text="@(DeployedModFiles->'$(OutputDir)%(RelativeDir)%(FileName)%(Extension)')" />
        
        <ItemGroup>
            <OutputFilesToDelete Include="$(OutputDir)**\*.*" Exclude="@(DeployedModFiles->'$(OutputDir)%(RelativeDir)%(FileName)%(Extension)')" />
        </ItemGroup>

        <Message Text="@(OutputFilesToDelete)" />

        <Delete Files="%(OutputFilesToDelete.FullPath)" />
        <RemoveEmptyDirectories Directories="$(OutputDir)" DeleteBaseFolder="false"/>

        <Copy SourceFiles="%(DeployedModFiles.FullPath)" DestinationFolder="$(OutputDir)%(RecursiveDir)" SkipUnchangedFiles="true" />

        <Message Importance="High" Text="Mod deployed." />
    </Target>
</Project>