#!/bin/bash

version_armhf=4.4.3.242971445
version_arm64=4.4.3.242971445

if [ $arch == 32 ];then
  version_select="$version_armhf"
  url_arch=armv7l
elif [ $arch == 64 ];then
  version_select="$version_arm64"
  url_arch=aarch64
else
  error "Failed to detect OS CPU architecture! Something is very wrong."
fi

if package_installed musescore ;then
  status "First removing musescore package with APT..."
  sudo apt purge musescore -y --autoremove || exit 1
fi

enable_module fuse || exit 1

wget -O "/tmp/MuseScore.AppImage" "https://github.com/musescore/MuseScore/releases/download/v${version_select%.*}/MuseScore-Studio-${version_select}-${url_arch}.AppImage" || exit 1

chmod +x "/tmp/MuseScore.AppImage"

#Use built-in install procedure
sudo /tmp/MuseScore.AppImage install || error "MuseScore.AppImage failed to run its own installation procedure. Please review errors above."
#this moves itself to /usr/local/bin/MuseScore.AppImage

#make terminal command
sudo rm -f /usr/local/bin/mscore
sudo ln -s /usr/local/bin/MuseScore.AppImage /usr/local/bin/mscore

#remove "Portable" from menu button
sudo sed -i 's/ Portable$//g' /usr/local/share/applications/org.musescore.MuseScore4portable.desktop
# update timestamp of top level icon directory to signal icon cache to be refreshed
sudo touch /usr/local/share/icons
true #in case touch fails
