#!/bin/bash
version=7.4.6

rm -rf "$HOME/powershell"
install_packages libc6 libgcc1 libgssapi-krb5-2 libstdc++6 zlib1g libicu-dev libssl3 '|' libssl1.1 '|' libssl1.0.2 '|' libssl1.0.0 libunwind8 || exit 1
wget https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-linux-arm${arch}.tar.gz || exit 1
mkdir -p ~/powershell || error "Unable to create Powershell Directory!"
tar -xf ./powershell-${version}-linux-arm${arch}.tar.gz -C ~/powershell || error "Unable to extract / copy Powershell to directory!"
rm -f ./powershell-${version}-linux-arm${arch}.tar.gz || sudo rm -f ./powershell-${version}-linux-arm${arch}.tar.gz
# fixing broken upstream permissions https://github.com/PowerShell/PowerShell/issues/18495#issuecomment-2179538840
chmod +x "$HOME/powershell"/*
status 'Creating symlink...'
sudo "$HOME/powershell/pwsh" -command 'New-Item -ItemType SymbolicLink -Path "/usr/local/bin/pwsh" -Target "$PSHOME/pwsh" -Force' || error "Unable to link Powershell to /usr/local/bin!"

status 'Creating menu entry...'
echo "[Desktop Entry]
Version=${version}
Name=Microsoft PowerShell
GenericName=shell
Exec=pwsh %U
Terminal=true
X-MultipleArgs=false
Type=Application
Icon=$(dirname "$0")/icon-64.png
Categories=Utility
StartupNotify=true" > $HOME/.local/share/applications/powershell.desktop || error 'Failed to create menu entry!'
