#!/bin/sh

dir=$1
arg=$2
arch=$(uname -m)

if [ "$arch" != "x86_64" ] && [ "$arch" != "aarch64" ]; then
	echo "CPU architecture not recognised (not x86_64 or aarch64). Aborting."
	exit 1
fi

# workaround if we're not renaming the artifact
if [ "$arch" = "aarch64" ]; then
	arch='arm64'
fi

if [ ! -d "${dir}" ]; then
	echo "${dir}} is not a directory or does not exist. Please provide an existing directory."
	exit 1
fi

localShare="$HOME/.local/share"
localShareIcons="${localShare}/icons/hicolor"

if [ "${arg}" = "remove" ]; then
	rm -v \
		"${localShareIcons}"/512x512/apps/org.jeffvli.feishin.png \
		"${localShareIcons}"/256x256/apps/org.jeffvli.feishin.png \
		"${localShareIcons}"/128x128/apps/org.jeffvli.feishin.png \
		"${localShareIcons}"/64x64/apps/org.jeffvli.feishin.png \
		"${localShareIcons}"/32x32/apps/org.jeffvli.feishin.png \
		"${localShare}"/applications/org.jeffvli.feishin.desktop \
		"${dir}./Feishin-linux-${arch}.AppImage"
	exit 0
fi

curl -L --create-dirs --write-out '%{filename_effective}\n' \
	-o "${dir}/Feishin-linux-${arch}.AppImage" "https://github.com/jeffvli/feishin/releases/latest/download/Feishin-linux-${arch}.AppImage" \
	-o "${localShareIcons}"/512x512/apps/org.jeffvli.feishin.png 'https://github.com/jeffvli/feishin/blob/development/assets/icons/512x512.png?raw=true' \
	-o "${localShareIcons}"/256x256/apps/org.jeffvli.feishin.png 'https://github.com/jeffvli/feishin/blob/development/assets/icons/256x256.png?raw=true' \
	-o "${localShareIcons}"/128x128/apps/org.jeffvli.feishin.png 'https://github.com/jeffvli/feishin/blob/development/assets/icons/128x128.png?raw=true' \
	-o "${localShareIcons}"/64x64/apps/org.jeffvli.feishin.png 'https://github.com/jeffvli/feishin/blob/development/assets/icons/64x64.png?raw=true' \
	-o "${localShareIcons}"/32x32/apps/org.jeffvli.feishin.png 'https://github.com/jeffvli/feishin/blob/development/assets/icons/32x32.png?raw=true'
chmod -v u+x "${dir}/Feishin-linux-${arch}.AppImage"

waylandFlags=''
if [ "${arg}" = "wayland-native" ]; then
	waylandFlags=' --enable-features=UseOzonePlatform,WaylandWindowDecorations --ozone-platform-hint=auto'
fi

# this is for Debian-based kernels and ALT respectively
# https://unix.stackexchange.com/a/303214/145722
sandboxFlag=''
if [ "$(sysctl kernel.unprivileged_userns_clone 2>/dev/null)" = "0" ] \
	|| [ "$(sysctl kernel.userns_restrict 2>/dev/null)" = "1" ]; then
	sandboxFlag=' --no-sandbox'
fi

mkdir -pv "${localShare}"/applications
cat > "${localShare}"/applications/org.jeffvli.feishin.desktop << EOL
[Desktop Entry]
Name=Feishin
GenericName=Music player
Exec=${dir}/Feishin-linux-${arch}.AppImage${sandboxFlag}${waylandFlags}
TryExec=${dir}/Feishin-linux-${arch}.AppImage
Terminal=false
Type=Application
Icon=org.jeffvli.feishin
StartupWMClass=feishin
SingleMainWindow=true
Categories=AudioVideo;Audio;Player;Music;
Keywords=Navidrome;Jellyfin;Subsonic;OpenSubsonic
Comment=A player for your self-hosted music server
EOL
