# https://learn.microsoft.com/en-us/dotnet/core/distribution-packaging

# <pkg> (ver): <content>
#   <deps>
#
# sdk (sdk): 3,4,18,21
#   runtime,aspnetcore-runtime,
#   targeting-pack,aspnetcore-targeting-pack,netstandard-targeting-pack,
#   apphost-pack,templates
# sdk-aot (sdk): 19,20
#   sdk
# aspnetcore-runtime (aspnet): 6
#   runtime
# runtime-dep (runtime): null
#   <distro specify>
# runtime (runtime): 5
#   hostfxr,runtime-dep
# hostfxr (runtime): 2
#   host
# host (runtime): 1,8,9,10,16,22,23
# apphost-pack (runtime): 13
# targeting-pack (runtime): 12
# aspnetcore-targeting-pack (aspnet): 11
# netstandard-targeting-pack (netstd): 15 # no longer provided in sdk >= 10
# templates (sdk): 17

# {dotnet_root}            (0)
#   dotnet                 (1)
#   dnx                    (22)
#   LICENSE.txt            (8)
#   ThirdPartyNotices.txt  (8)
#   host/fxr/<fxr>         (2)
#   sdk/<sdk>              (3)
#   sdk-manifests/<sdk feature band version>       (4)
#   library-packs                                  (21)
#   metadata/workloads/<sdk feature band version>  (4)
#   template-packs                                 (4)
#   packs
#     Microsoft.AspNetCore.App.Ref/<aspnetcore ref>            (11)
#     Microsoft.NETCore.App.Ref/<netcore ref>                  (12)
#     Microsoft.NETCore.App.Host.<rid>/<apphost>               (13)
#     Microsoft.WindowsDesktop.App.Ref/<desktop ref>           (14)
#     NETStandard.Library.Ref/<netstandard>                    (15)
#     Microsoft.NETCore.App.Runtime.<rid>/<runtime>            (18)
#     Microsoft.AspNetCore.App.Runtime.<rid>/<aspnetcore>      (18)
#     runtime.<rid>.Microsoft.DotNet.ILCompiler/<runtime>      (19)
#     Microsoft.NETCore.App.Runtime.NativeAOT.<rid>/<runtime>  (20)
#   shared/
#     Microsoft.NETCore.App/<runtime>            (5)
#     Microsoft.AspNetCore.App/<aspnetcore>      (6)
#     Microsoft.AspNetCore.All/<aspnetcore>      (6)
#     Microsoft.WindowsDesktop.App/<desktop app> (7)
#   templates/<templates>                        (17)
# /
#   etc/dotnet/install_location     (16)
#   usr/share/man/man1/dotnet.1.gz  (9)
#   usr/bin/
#     dotnet               (10)
#     dnx                  (23)

# Note: dotnet use different version number in SDK and runtime
ver=$(cat "$SRCDIR"/runtime-version.txt)
abinfo "Set version to $ver ..."
PKGVER="$ver"

abinfo "Deploying files ..."
install -dv "$PKGDIR"/usr/bin
install -dv "$PKGDIR"/usr/lib/dotnet
install -dv "$PKGDIR"/usr/share/doc/dotnet-host
install -dv "$PKGDIR"/usr/bin

install -Dv "$SRCDIR"/output/ThirdPartyNotices.txt "$PKGDIR"/usr/share/doc/dotnet-host # 8
install -Dv "$SRCDIR"/output/LICENSE.txt "$PKGDIR"/usr/share/doc/dotnet-host # 8

# 16 see overrides
install -Dv "$SRCDIR"/output/dotnet "$PKGDIR"/usr/lib/dotnet # 1
ln -sv ../lib/dotnet/dotnet "$PKGDIR"/usr/bin/dotnet # 10
install -Dv "$SRCDIR"/output/dnx "$PKGDIR"/usr/lib/dotnet # 22
ln -sv ../lib/dotnet/dnx "$PKGDIR"/usr/bin/dnx # 23

abinfo "Installing manpages ..."
install -dv "$PKGDIR"/usr/share/man/man1
cp -v "$SRCDIR"/src/sdk/documentation/manpages/sdk/*.1 "$PKGDIR"/usr/share/man/man1 # 9

abinfo "Installing shell completions ..."
# FIXME: nushell is supported, but have no system wide profile directory
mkdir -pv "$PKGDIR"/usr/share/bash-completion/completions
"$SRCDIR"/output/dotnet completions script bash > "$PKGDIR"/usr/share/bash-completion/completions/dotnet
mkdir -pv "$PKGDIR"/usr/share/fish/completions
"$SRCDIR"/output/dotnet completions script fish > "$PKGDIR"/usr/share/fish/completions/dotnet
mkdir -pv "$PKGDIR"/usr/share/powershell/completions
"$SRCDIR"/output/dotnet completions script pwsh > "$PKGDIR"/usr/share/powershell/completions/dotnet.ps1
mkdir -pv "$PKGDIR"/usr/share/zsh/site-functions
"$SRCDIR"/output/dotnet completions script zsh > "$PKGDIR"/usr/share/zsh/site-functions/_dotnet
