<Project Sdk="Microsoft.NET.Sdk">
    <Import Project="VersionInfo.props" Condition="Exists('VersionInfo.props')" />
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net9.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <Version>$(PoloniumSdkVersion)</Version>
        <SelfContained>true</SelfContained>
        <PublishSingleFile>true</PublishSingleFile>
        <OutputPath>bin/</OutputPath>
        <PublishDir>$(OutputPath)publish/</PublishDir>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Mono.Cecil" Version="0.11.6" />
    </ItemGroup>
    
    <Target Name="PostPublish" AfterTargets="Publish">
        <PropertyGroup>
            <AppDir>$(PublishDir)AppDir/</AppDir>
            <AppImageToolPath>$(ProjectDir)appimagetool</AppImageToolPath>
            <AppName>Polonium.Godot.Patcher</AppName>
        </PropertyGroup>
        <Exec Command="mkdir -p $(AppDir)usr/bin $(AppDir)usr/lib $(AppDir)usr/share/applications $(AppDir)usr/share/icons/hicolor/256x256/apps" />
        <Exec Command="cp $(PublishDir)$(AppName) $(AppDir)usr/bin/" />
        <Exec Command="cp $(PublishDir)*.dll $(AppDir)usr/lib/ || true" />
        <Exec Command="cp $(PublishDir)*.so $(AppDir)usr/lib/ || true" />
        <Exec Command="echo '#!/bin/bash' > $(AppDir)AppRun" />
        <Exec Command="echo 'export LD_LIBRARY_PATH=$APPDIR/usr/lib:$LD_LIBRARY_PATH' >> $(AppDir)AppRun" />
        <Exec Command="echo 'exec $APPDIR/usr/bin/$(AppName) $@' >> $(AppDir)AppRun" />
        <Exec Command="chmod +x $(AppDir)AppRun" />
        <Exec Command="echo '[Desktop Entry]' > $(AppDir)usr/share/applications/$(AppName).desktop" />
        <Exec Command="echo 'Name=$(AppName)' >> $(AppDir)usr/share/applications/$(AppName).desktop" />
        <Exec Command="echo 'Exec=AppRun' >> $(AppDir)usr/share/applications/$(AppName).desktop" />
        <Exec Command="echo 'Type=Application' >> $(AppDir)usr/share/applications/$(AppName).desktop" />
        <Exec Command="echo 'Categories=Utility;' >> $(AppDir)usr/share/applications/$(AppName).desktop" />
        <Exec Command="echo 'Icon=$(AppName)' >> $(AppDir)/usr/share/applications/$(AppName).desktop" />
        <Exec Command="cp -f $(AppName).png $(AppDir)" />
        <Exec Command="cp $(AppDir)/usr/share/applications/$(AppName).desktop $(AppDir)/" />
        <Exec Command="$(AppImageToolPath) $(ProjectDir)$(AppDir) $(ProjectDir)$(PublishDir)$(AppName).AppImage" />
    </Target>
</Project>
