$ErrorActionPreference = "Stop"

$branch = git rev-parse --abbrev-ref HEAD

# Move to the git root if we arn't already there
git rev-parse --show-toplevel | Set-Location

# get the server jar from the install directory ( in future support pre-release )
$server_jar = resolve-path "$env:APPDATA/Hytale/install/$branch/package/game/latest/Server/HytaleServer.jar"

# clear the source directory
Get-ChildItem -Path "src" | Remove-Item -Recurse -Force -Confirm:$false

# Decompile hytale
java -jar "_decomp/vineflower-1.11.2-slim.jar" --only="com/hypixel" $server_jar "./src"

# Extract the version before deleting the meta inf file
$version = (Get-Content "src/META-INF/MANIFEST.MF" | Select-String "^Implementation-Version:\s*(.+)$").Matches.Groups[1].Value

echo "VERSION = $version"

# delete anything not in com hypixel
Get-ChildItem "src/" -Exclude @("com", "manifests.json") | Remove-Item -Recurse -Force -Confirm:$false
Get-ChildItem "src/com/" -Exclude @("hypixel") | Remove-Item -Recurse -Force -Confirm:$false

git add "./src"
git commit -am "$version"
